diff options
-rw-r--r-- | include/cmocka.h | 11 | ||||
-rw-r--r-- | src/cmocka.c | 25 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 9 | ||||
-rw-r--r-- | tests/test_skip.c | 39 |
4 files changed, 81 insertions, 3 deletions
diff --git a/include/cmocka.h b/include/cmocka.h index 85e264f..a237142 100644 --- a/include/cmocka.h +++ b/include/cmocka.h @@ -1301,6 +1301,15 @@ void fail(void); #ifdef DOXYGEN /** + * @brief Forces the test to not be executed, but marked as skipped + */ +void skip(void); +#else +#define skip() _skip(__FILE__, __LINE__) +#endif + +#ifdef DOXYGEN +/** * @brief Forces the test to fail immediately and quit, printing the reason. * * @code @@ -1930,6 +1939,8 @@ void _test_free(void* const ptr, const char* file, const int line); void _fail(const char * const file, const int line); +void _skip(const char * const file, const int line); + int _run_test( const char * const function_name, const UnitTestFunction Function, void ** const volatile state, const UnitTestFunctionType function_type, diff --git a/src/cmocka.c b/src/cmocka.c index 5323fde..f8cd201 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -257,6 +257,7 @@ static CMOCKA_THREAD int global_running_test = 0; jmp_buf global_expect_assert_env; int global_expecting_assert = 0; const char *global_last_failed_assert = NULL; +static int global_skip_test; /* Keeps a map of the values that functions will have to return to provide */ /* mocked interfaces. */ @@ -359,6 +360,12 @@ static void exit_test(const int quit_application) } } +void _skip(const char * const file, const int line) +{ + cm_print_error(SOURCE_LOCATION_FORMAT ": Skipped!\n", file, line); + global_skip_test = 1; + exit_test(1); +} /* Initialize a SourceLocation structure. */ static void initialize_source_location(SourceLocation * const location) { @@ -1898,6 +1905,7 @@ static void cmprintf_group_finish_xml(const char *group_name, size_t total_executed, size_t total_failed, size_t total_errors, + size_t total_skipped, double total_runtime, struct CMUnitTestState *cm_tests) { @@ -1928,12 +1936,13 @@ static void cmprintf_group_finish_xml(const char *group_name, fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); fprintf(fp, "<testsuites>\n"); fprintf(fp, " <testsuite name=\"%s\" time=\"%.3f\" " - "tests=\"%u\" failures=\"%u\" errors=\"%u\" >\n", + "tests=\"%u\" failures=\"%u\" errors=\"%u\" skipped=\"%u\" >\n", group_name, total_runtime * 1000, /* miliseconds */ (unsigned)total_executed, (unsigned)total_failed, - (unsigned)total_errors); + (unsigned)total_errors, + (unsigned)total_skipped); for (i = 0; i < total_executed; i++) { struct CMUnitTestState *cmtest = &cm_tests[i]; @@ -2140,6 +2149,7 @@ static void cmprintf_group_finish(const char *group_name, size_t total_passed, size_t total_failed, size_t total_errors, + size_t total_skipped, double total_runtime, struct CMUnitTestState *cm_tests) { @@ -2163,6 +2173,7 @@ static void cmprintf_group_finish(const char *group_name, total_executed, total_failed, total_errors, + total_skipped, total_runtime, cm_tests); break; @@ -2410,7 +2421,12 @@ static int cmocka_run_one_tests(struct CMUnitTestState *test_state) if (rc == 0) { test_state->status = CM_TEST_PASSED; } else { - test_state->status = CM_TEST_FAILED; + if (global_skip_test) { + test_state->status = CM_TEST_SKIPPED; + global_skip_test = 0; /* Do not skip the next test */ + } else { + test_state->status = CM_TEST_FAILED; + } } rc = 0; } @@ -2450,6 +2466,7 @@ int _cmocka_run_group_tests(const char *group_name, size_t total_passed = 0; size_t total_executed = 0; size_t total_errors = 0; + size_t total_skipped = 0; double total_runtime = 0; size_t i; int rc; @@ -2511,6 +2528,7 @@ int _cmocka_run_group_tests(const char *group_name, test_number, cmtest->test->name, cmtest->error_message); + total_skipped++; break; case CM_TEST_FAILED: cmprintf(PRINTF_TEST_FAILURE, @@ -2555,6 +2573,7 @@ int _cmocka_run_group_tests(const char *group_name, total_passed, total_failed, total_errors, + total_skipped, total_runtime, cm_tests); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2525fe9..596fe51 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,6 +15,7 @@ set(CMOCKA_TESTS test_assert_macros_fail test_exception_handler test_basics + test_skip test_setup_fail) foreach(_CMOCKA_TEST ${CMOCKA_TESTS}) @@ -25,6 +26,14 @@ endforeach() # test_assert_macros_fail set_tests_properties( + test_skip + PROPERTIES + PASS_REGULAR_EXPRESSION + "\\[ SKIPPED \\] test_check_skip" +) + +# test_assert_macros_fail +set_tests_properties( test_assert_macros_fail PROPERTIES PASS_REGULAR_EXPRESSION diff --git a/tests/test_skip.c b/tests/test_skip.c new file mode 100644 index 0000000..127161a --- /dev/null +++ b/tests/test_skip.c @@ -0,0 +1,39 @@ +/* + * Copyright 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +/* A test case that does check if an int is equal. */ +static void test_check_skip(void **state) { + (void)state; /* unused */ + + skip(); + + assert_true(0); +} + + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_check_skip), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} + |