tests FEATURE check source code formatting as one of the tests
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d5dbd8..0567c34 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -170,7 +170,10 @@
 # source files to be covered by the 'format' target
 set(format_sources
     compat/*
-    src/*)
+    src/*.c
+    src/*.h
+    src/plugins_exts/*
+    src/plugins_types/*)
 #
 # options
 #
@@ -305,6 +308,11 @@
     find_package(CMocka 1.0.0)
 endif(ENABLE_BUILD_TESTS)
 
+if ("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
+    # enable before adding tests to let them detect that format checking is available - one of the tests is format checking
+    source_format_enable()
+endif()
+
 # tests
 if(ENABLE_VALGRIND_TESTS)
     set(ENABLE_BUILD_TESTS ON)
@@ -341,10 +349,9 @@
     libyang_abicheck()
 endif()
 
-# source code format
-if ("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
-	source_format(${format_sources})
-endif()
+# source code format target for Makefile
+# - add it after tests which may also update list of sources to format
+source_format(${format_sources})
 
 # clean cmake cache
 add_custom_target(cclean
diff --git a/CMakeModules/SourceFormat.cmake b/CMakeModules/SourceFormat.cmake
index d3655ad..76132a4 100644
--- a/CMakeModules/SourceFormat.cmake
+++ b/CMakeModules/SourceFormat.cmake
@@ -1,12 +1,22 @@
 # format source files with uncrustify
+
+# check that format checking is available - always use before SOURCE_FORMAT
+macro(SOURCE_FORMAT_ENABLE)
+    find_package(Uncrustify 0.71)
+    if(UNCRUSTIFY_FOUND)
+        set(SOURCE_FORMAT_ENABLED TRUE)
+    else()
+        set(SOURCE_FORMAT_ENABLED FALSE)
+    endif()
+endmacro()
+
 # files are expected to be a list and relative paths are resolved wtih respect to CMAKE_SOURCE DIR
 macro(SOURCE_FORMAT)
     if(NOT ${ARGC})
         message(FATAL_ERROR "source_format() needs a list of files to format!")
     endif()
 
-    find_package(Uncrustify 0.71)
-    if(UNCRUSTIFY_FOUND)
+    if(SOURCE_FORMAT_ENABLED)
         add_custom_target(format
                 COMMAND ${UNCRUSTIFY} -c ${CMAKE_SOURCE_DIR}/uncrustify.cfg --no-backup --replace ${ARGN}
                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
@@ -16,5 +26,7 @@
                 COMMAND ${UNCRUSTIFY} -c ${CMAKE_SOURCE_DIR}/uncrustify.cfg --check ${ARGN}
                 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
                 COMMENT "Checking format of the sources with ${UNCRUSTIFY} ...")
+
+        set(SOURCE_FORMAT_ENABLED TRUE)
     endif()
 endmacro()
diff --git a/tests/style/CMakeLists.txt b/tests/style/CMakeLists.txt
index f820b16..d291510 100644
--- a/tests/style/CMakeLists.txt
+++ b/tests/style/CMakeLists.txt
@@ -1,2 +1,6 @@
 add_test(NAME headers
 	COMMAND ${CMAKE_SOURCE_DIR}/tests/style/check_includes.sh ${CMAKE_SOURCE_DIR}/src/ ${CMAKE_SOURCE_DIR}/tools/lint/ ${CMAKE_SOURCE_DIR}/tools/re/)
+
+if (${SOURCE_FORMAT_ENABLED})
+	add_test(NAME format WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND make format-check)
+endif()