{"id":16317679,"url":"https://github.com/zfletch/test-harness-c","last_synced_at":"2025-05-13T16:33:00.507Z","repository":{"id":15972636,"uuid":"18715449","full_name":"zfletch/test-harness-c","owner":"zfletch","description":"Test Harness","archived":false,"fork":false,"pushed_at":"2014-04-18T00:33:57.000Z","size":180,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-16T20:20:56.970Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zfletch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-12T21:45:12.000Z","updated_at":"2014-04-18T00:33:58.000Z","dependencies_parsed_at":"2022-08-04T07:15:16.960Z","dependency_job_id":null,"html_url":"https://github.com/zfletch/test-harness-c","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Ftest-harness-c","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Ftest-harness-c/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Ftest-harness-c/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfletch%2Ftest-harness-c/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zfletch","download_url":"https://codeload.github.com/zfletch/test-harness-c/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253981889,"owners_count":21994351,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-10T22:09:00.472Z","updated_at":"2025-05-13T16:33:00.478Z","avatar_url":"https://github.com/zfletch.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"Test Harness\n==============\n\nUnit testing framework for small C projects.\n\nI was working on a C project and I wanted to write a few unit tests.\nWhen I looked for C unit testing frameworks, the frameworks that I found\nlooked excellent but were all much more complicated than what I needed.\n\nI wrote this test harness to provide a very simple unit testing framework.\n\n\nUnit Test Functions\n--------------\n\nUnit tests are based on four functions:\n\n- `void affirm(TestResult* result, bool valid, const char* fail_text)`:\n\tif `valid` is false then the test fails with the message `fail_text`\n- `void onSuccess(TestResult* result, const char* success_text)`:\n\tif the test succeeds then the test uses `success_text` in the output\n- `void onFailure(TestResult* result, const char* fail_text)`:\n\tif the test fails then the test uses `fail_text` in the output\n- `void report(TestResult* result, const char* str, ...)`:\n\tif verbose is set then the string passed in is printed\n\n\nTest Harness Functions\n--------------\n\nTest harnesses are built on the following three functions:\n\n- `bool doTest(char* name, TestFunc* test_funcs, size_t test_funcs_length, int seed, Verbosity verbose)`:\n\truns each of the tests passed in (`test_funcs`) and returns true if they\n\tall pass and false otherwise; the argument `verbose` determines what\n\tinformation is printed, if any\n- `void createTestHarness(TestHarness* harness, char* name, TestFunc* test_funcs, size_t test_funcs_length)`:\n\tgiven a pointer to a `TestHarness` (`harness`) and a list of tests (`test_funcs`), modifies the TestHarness so that it can be passed into `runTestHarness`\n- `bool runTestHarness(TestHarness* harness, int seed, Verbosity verbose)`:\n\tbasically the same as `doTest` except it also takes in a `TestHarness`, which\n\tallows the same test to be run multiple times in a loop with a different\n\tseed\n\n\nLevels of Verbosity\n--------------\n\nFinally there are four levels of `Verbosity` that can be passed into `doTest`\nand `runTestHarness`:\n\n- `NO_INFO`:\n\tprints nothing\n- `MINIMAL_INFO`:\n\tprints whether the `TestHarness` with the particular seed that is\n\tpassed in succeeds, fails, or dies\n- `BASIC_INFO`:\n\tprints whether each unit test within the `TestHarness` succeeds\n\tor fails along with when the `TestHarness` starts and ends\n- `ALL_INFO`:\n\tprints everything that `BASIC_INFO` prints in addition to\n\t`report` calls and data about when each unit test starts and ends\n\n\nSimple Example\n==============\n\n\t#include \u003cstdio.h\u003e\n\t#include \"../src/harness.h\"\n\n\tvoid test_modTen (TestResult* result, int seed)\n\t{\n\t\tsrand(seed);\n\n\t\tint num = rand() % 10;\n\n\t\treport(result, \"using number %d\", num);\n\t\taffirm(result, num \u003e= 0, \"modulo 10 must be greater or equal to 0\");\n\t\taffirm(result, num \u003c 10, \"modulo 10 must be less than 10\");\n\t}\n\n\tvoid test_minusTen (TestResult* result, int seed)\n\t{\n\t\tsrand(seed);\n\n\t\tint num = rand();;\n\t\tint num_minus_ten = num - 10;\n\n\t\tonSuccess(result, \"yes!\");\n\t\tonFailure(result, \"no!\");\n\n\t\taffirm(result, num \u003e num_minus_ten, \"subtracting ten from a number makes it smaller\");\n\t}\n\n\tvoid test_plusTen (TestResult* result, int seed)\n\t{\n\t\tsrand(seed);\n\n\t\tint num = rand();;\n\t\tint num_plus_ten = num + 10;\n\n\t\taffirm(result, num \u003e num_plus_ten, \"adding ten to a number makes it smaller\");\n\t}\n\n\tint main (int argc, char** argv)\n\t{\n\t\tint seed = 55;\n\t\tdoTest(\"Test some functions\",\n\t\t\t\t(TestFunc[]) {\n\t\t\t\t\t{ test_modTen, \"modTen\" },\n\t\t\t\t\t{ test_minusTen, \"minusTen\" },\n\t\t\t\t\t{ test_plusTen, \"plusTen\" },\n\t\t\t\t},\n\t\t\t\t3,\n\t\t\t\tseed,\n\t\t\t\tALL_INFO);\n\n\t\treturn 0;\n\t}\n\nWhich produces the output:\n\n\t[start] \"Test some functions\" with seed 55\n\t  [start] \"modTen\"\n\t\t[report] using number 5\n\t  [success] \"modTen\"\n\t  [end]\n\t  [start] \"minusTen\"\n\t  [success] \"minusTen\" (yes!)\n\t  [end]\n\t  [start] \"plusTen\"\n\t  [FAILURE] \"plusTen\" (adding ten to a number makes it smaller)\n\t  [end]\n\t[FAILURE] \"Test some functions\" with seed 55\n\t[end]\n\nSee the `examples/` directory for some example programs. All of the functions\nmentioned above and all of the different levels of `Verbosity` should be\nrepresented.\n\nThe examples can be compiled with `examples/compile_all.sh`.\nThey can be run with `examples/run_all.sh` or they can be run individually.\n\nNotes\n==============\n\nTested on OSX version 10.9.2 with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) and gcc-mp-4.8 (MacPorts gcc48 4.8.2\\_0) 4.8.2\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfletch%2Ftest-harness-c","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzfletch%2Ftest-harness-c","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfletch%2Ftest-harness-c/lists"}