CTestify is a testing framework for C programs, designed to be as fast and easy to use as possible. CTestify runs in two iterations: the first counts the tests, while the second actually executes them. It measures the test execution time and displays it upon completion. Additionally, it includes built-in protection against unexpected segmentation faults. If a test encounters a segfault, it gets a special mark [ SIGSEGV ] instead of [ FAILURE ]. If you need a fast and simple testing framework, choose CTestify! |
- Download main header ctestify.h
- Include it to your source file.
- Create entry point like this:
void test_main(){
// Write tests here
}
- To set test suite name use
SET_TEST_SUITE_NAME(TESTS);
without it tests wont work! 5. Now you can write some tests!
Maybe, you need to do something before or after tests. If so, you need to define TESTINGENVCTL before including framework.
#define TESTINGENVCTL
#include "ctestify.h"
Now you can write them. You should create two functions.
int TestingEnvironmentSetUp(){}
int TestingEnvironmentDestroy(){}
Functions return zero if successed, and nonzero if failed.
Currently tests support int (from int8_t to uint64_t), float, double, char* and void*. Char* means string, so it uses strcmp();
At the end of any test you can add M, and then pass string as additional argument. It will be your custom error message. For example:
EXPECT_EQM(EQTEST1, 5, 5, "Custom error message!");
Every EXPECT test has it's own ASSERT variant.
The most basic test, expects equality of two values.
Signature: EXPECT_EQ(test_name, value, expected)
EXPECT_EQ(EQTEST1, 5, 7); // Will fail
EXPECT_EQ(EQTEST2, 5, 5); // Good test
Expects value, not equal to second.
Signature: EXPECT_NEQ(test_name, value, expected)
EXPECT_NEQ(NEQTEST1, 5, 5); // Will fail
EXPECT_NEQ(NEQTEST2, 5, 7); // Good test
Expects value bigger than zero.
Signature: EXPECT_TRUE(test_name, value);
EXPECT_TRUE(TRUETEST1, 0); // Will fail
EXPECT_TRUE(TRUETEST2, 65536); // Good test
Expects zero value.
Signature: EXPECT_FALSE(test_name, value);
EXPECT_FALSE(FALSETEST1, 65536); // Will fail
EXPECT_FALSE(FALSETEST2, 0); // Good test
Expects value bigger than passed to "expected" argument.
Signature: EXPECT_BIGGER(test_name, value, expected);
EXPECT_BIGGER(EXBIG1, 4, 7); // Will fail
EXPECT_BIGGER(EXBIG2, 10, 7); // Good test
Expects value less than passed to "expected" argument.
Signature: EXPECT_LESS(test_name, value, expected);
EXPECT_LESS(EXBIG1, 10, 7); // Will fail
EXPECT_LESS(EXBIG2, 4, 7); // Good test
Expects value bigger or equal than passed to "expected" argument.
Signature: EXPECT_BIGGER(test_name, value, expected);
EXPECT_BIGGEROREQ(EXBIGOREQ1, 4, 7); // Will fail
EXPECT_BIGGER(EXBIGOREQ2, 10, 7); // Good test
Expects value less or equal than passed to "expected" argument.
Signature: EXPECT_LESS(test_name, value, expected);
EXPECT_LESSOREQ(EXLESSOREQ1, 10, 7); // Will fail
EXPECT_LESS(EXLESSOREQ2, 4, 7); // Good test
Expects nonnull pointer.
Signature: EXPECT_VALIDPTR(test_name, ptr);
EXPECT_VALIDPTR(VALIDPTR1, (void*)0); // Will fail
EXPECT_VALIDPTR(VALIDPTR2, (void*)0xFF0154297AC8); // Good test
Prints user messages during test run. Formatting supported! Also flushes buffer, so can be used in loops.
PRINT("INTERNAL MESSAGE %s %d", "TEST", 2);
- ASSERT_EQ
- ASSERT_TRUE
- ASSERT_FALSE
- ASSERT_BIGGER
- ASSERT_LESS
- ASSERT_BIGGEROREQ
- ASSERT_LESSOREQ
- ASSERT_VALIDPTR