diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-08-12 14:36:21 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-08-12 14:36:21 -0400 |
commit | 7939dbc4ae45aec2f5948cfb7feed0c4cebf9fa2 (patch) | |
tree | db2713e51f220183e72b2690e21432240e60f498 | |
parent | b967777cd8a32ebbde3a90b05dea368a96321ae0 (diff) | |
download | stasis-7939dbc4ae45aec2f5948cfb7feed0c4cebf9fa2.tar.gz |
Add test_junitxml.cmore-unit-tests
* Fix duplicate define value for JUNIT_RESULT_STATE_ERROR
* Add static junit test data
* Copy test data to current test directory
-rw-r--r-- | include/junitxml.h | 2 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/data/result.xml | 10 | ||||
-rw-r--r-- | tests/data/result_error.xml | 24 | ||||
-rw-r--r-- | tests/test_junitxml.c | 82 |
5 files changed, 119 insertions, 1 deletions
diff --git a/include/junitxml.h b/include/junitxml.h index 504b8e4..777ee27 100644 --- a/include/junitxml.h +++ b/include/junitxml.h @@ -6,7 +6,7 @@ #define JUNIT_RESULT_STATE_NONE 0 #define JUNIT_RESULT_STATE_FAILURE 1 #define JUNIT_RESULT_STATE_SKIPPED 2 -#define JUNIT_RESULT_STATE_ERROR 2 +#define JUNIT_RESULT_STATE_ERROR 3 /** * Represents a failed test case diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f06638e..62f58a9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,8 @@ set(nix_clang_cflags -Wno-unused-parameter -Wno-incompatible-pointer-types-disca set(win_msvc_cflags /Wall) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/data/generic.ini ${CMAKE_CURRENT_BINARY_DIR} COPYONLY) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/data/result.xml ${CMAKE_CURRENT_BINARY_DIR} COPYONLY) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/data/result_error.xml ${CMAKE_CURRENT_BINARY_DIR} COPYONLY) file(GLOB files "test_*.c") if (BASH_PROGRAM) diff --git a/tests/data/result.xml b/tests/data/result.xml new file mode 100644 index 0000000..81df70f --- /dev/null +++ b/tests/data/result.xml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="2" skipped="1" tests="4" time="0.022" timestamp="2024-08-12T13:52:02.624944-04:00" hostname="examplehost"><testcase classname="test_simple" name="test_skip" time="0.000"><skipped type="pytest.skip" message="unconditional skip">/example/test_simple.py:4: unconditional skip</skipped></testcase><testcase classname="test_simple" name="test_fail" time="0.000"><failure message="assert False">def test_fail(): + print("oh no, this test failed.", file=sys.stderr) +> assert False +E assert False + +test_simple.py:10: AssertionError</failure></testcase><testcase classname="test_simple" name="test_success" time="0.000" /><testcase classname="test_simple" name="test_error" time="0.000"><failure message="RuntimeError: uh oh, a runtime error">def test_error(): +> raise RuntimeError("uh oh, a runtime error") +E RuntimeError: uh oh, a runtime error + +test_simple.py:17: RuntimeError</failure></testcase></testsuite></testsuites> diff --git a/tests/data/result_error.xml b/tests/data/result_error.xml new file mode 100644 index 0000000..cbb7a86 --- /dev/null +++ b/tests/data/result_error.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="1" failures="0" skipped="0" tests="1" time="0.077" timestamp="2024-08-12T13:58:07.030762-04:00" hostname="examplehost"><testcase classname="" name="test_simple" time="0.000"><error message="collection failure">/example/miniforge3/envs/test/lib/python3.11/site-packages/_pytest/python.py:493: in importtestmodule + mod = import_path( +/example/miniforge3/envs/test/lib/python3.11/site-packages/_pytest/pathlib.py:582: in import_path + importlib.import_module(module_name) +/example/miniforge3/envs/test/lib/python3.11/importlib/__init__.py:126: in import_module + return _bootstrap._gcd_import(name[level:], package, level) +<frozen importlib._bootstrap>:1204: in _gcd_import + ??? +<frozen importlib._bootstrap>:1176: in _find_and_load + ??? +<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked + ??? +<frozen importlib._bootstrap>:690: in _load_unlocked + ??? +/example/miniforge3/envs/test/lib/python3.11/site-packages/_pytest/assertion/rewrite.py:165: in exec_module + source_stat, co = _rewrite_test(fn, self.config) +/example/miniforge3/envs/test/lib/python3.11/site-packages/_pytest/assertion/rewrite.py:345: in _rewrite_test + tree = ast.parse(source, filename=strfn) +/example/miniforge3/envs/test/lib/python3.11/ast.py:50: in parse + return compile(source, filename, mode, flags, +E File "/tmp/bleh/test_simple.py", line 17 +E syntax error here +E ^^^^^ +E SyntaxError: invalid syntax</error></testcase></testsuite></testsuites> diff --git a/tests/test_junitxml.c b/tests/test_junitxml.c new file mode 100644 index 0000000..9b2181e --- /dev/null +++ b/tests/test_junitxml.c @@ -0,0 +1,82 @@ +#include "testing.h" + +void test_junitxml_testsuite_read() { + struct JUNIT_Testsuite *testsuite; + STASIS_ASSERT_FATAL((testsuite = junitxml_testsuite_read("result.xml")) != NULL, "failed to load testsuite data"); + STASIS_ASSERT(testsuite->name != NULL, "Test suite must be named"); + STASIS_ASSERT(testsuite->skipped > 0, "missed skipped tests"); + STASIS_ASSERT(testsuite->failures > 0, "missed failed tests"); + STASIS_ASSERT(testsuite->errors == 0, "should not have errored tests"); + STASIS_ASSERT(testsuite->tests > 0, "missed tests"); + + for (size_t i = 0; i < testsuite->_tc_inuse; i++) { + struct JUNIT_Testcase *testcase = testsuite->testcase[i]; + STASIS_ASSERT_FATAL(testcase->name != NULL, "test case should not be NULL"); + STASIS_ASSERT(testcase->name != NULL, "name should not be NULL"); + STASIS_ASSERT(testcase->classname != NULL, "classname should not be NULL"); + + switch (testcase->tc_result_state_type) { + case JUNIT_RESULT_STATE_SKIPPED: + STASIS_ASSERT(testcase->result_state.skipped != NULL, "skipped state set, but data pointer was null"); + STASIS_ASSERT(testcase->result_state.skipped->message != NULL, "data pointer set, but message pointer was null"); + break; + case JUNIT_RESULT_STATE_FAILURE: + STASIS_ASSERT(testcase->result_state.failure != NULL, "failure state set, but data pointer was null"); + STASIS_ASSERT(testcase->result_state.failure->message != NULL, "data pointer set, but message pointer was null"); + break; + case JUNIT_RESULT_STATE_ERROR: + STASIS_ASSERT(testcase->result_state.error != NULL, "error state set, but data pointer was null"); + STASIS_ASSERT(testcase->result_state.error->message != NULL, "data pointer set, but message pointer was null"); + break; + case JUNIT_RESULT_STATE_NONE: + STASIS_ASSERT(testcase->result_state.failure == NULL, "success, but has an failure record"); + STASIS_ASSERT(testcase->result_state.skipped == NULL, "success, but has a skipped record "); + STASIS_ASSERT(testcase->result_state.error == NULL, "success, but has an error record"); + break; + default: + SYSERROR("unknown test case result type (%d)", testcase->tc_result_state_type); + break; + } + } + junitxml_testsuite_free(&testsuite); +} + +void test_junitxml_testsuite_read_error() { + struct JUNIT_Testsuite *testsuite; + STASIS_ASSERT_FATAL((testsuite = junitxml_testsuite_read("result_error.xml")) != NULL, "failed to load testsuite data"); + + STASIS_ASSERT(testsuite->name != NULL, "test suite must be named"); + STASIS_ASSERT(testsuite->skipped == 0, "should not have any skipped tests"); + STASIS_ASSERT(testsuite->failures == 0, "should not have any failures, only errors"); + STASIS_ASSERT(testsuite->errors > 0, "missed failed tests"); + STASIS_ASSERT(testsuite->tests > 0, "missed tests"); + STASIS_ASSERT(testsuite->timestamp != NULL, "Test suite must have a timestamp"); + + for (size_t i = 0; i < testsuite->_tc_inuse; i++) { + struct JUNIT_Testcase *testcase = testsuite->testcase[i]; + STASIS_ASSERT_FATAL(testcase->name != NULL, "test case should not be NULL"); + STASIS_ASSERT(testcase->name != NULL, "name should not be NULL"); + STASIS_ASSERT(testcase->classname != NULL, "classname should not be NULL"); + + switch (testcase->tc_result_state_type) { + case JUNIT_RESULT_STATE_ERROR: + STASIS_ASSERT(testcase->result_state.error != NULL, "error state set, but data pointer was null"); + STASIS_ASSERT(testcase->result_state.error->message != NULL, "data pointer set, but message pointer was null"); + break; + default: + SYSERROR("unexpected test case result type (%d)", testcase->tc_result_state_type); + break; + } + } + junitxml_testsuite_free(&testsuite); +} + +int main(int argc, char *argv[]) { + STASIS_TEST_BEGIN_MAIN(); + STASIS_TEST_FUNC *tests[] = { + test_junitxml_testsuite_read, + test_junitxml_testsuite_read_error, + }; + STASIS_TEST_RUN(tests); + STASIS_TEST_END_MAIN(); +}
\ No newline at end of file |