Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mathnogueira/ctest
Unit Test for C
https://github.com/mathnogueira/ctest
Last synced: 20 days ago
JSON representation
Unit Test for C
- Host: GitHub
- URL: https://github.com/mathnogueira/ctest
- Owner: mathnogueira
- Created: 2015-11-02T02:54:23.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-11-05T01:29:30.000Z (about 9 years ago)
- Last Synced: 2024-11-01T21:04:52.489Z (2 months ago)
- Language: C
- Size: 176 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#CTest :: Unit Testing for C
### v1.0.2CTest is a tool to automatize unit tests in applications that are built in C. It is easy to setup and to use. All the steps to install and use are described bellow.
## Installation
To start using CTest, you must clone it from this repository and compile it.
>$ git clone [email protected]:mathnogueira/CTest.git
>
> $ cd CTest
>
> $ makeIf everything works properly, you should have a new folder called **dist** inside the CTest directory. This is the shared object that you must link in your application in order to use CTest to test your code. PS: Do not forget to add the folder **include** in the include_path of your compiler.
## How to use
In order to make CTest to work, you got to have the tests main entry and other files that contain your test functions. Let's start with the main entry.
Let's call it as **test_main.c** and it is inside the directory **myapp/tests**.
```c
// test_main.c
#include
// You can include functions that are implemented elsewhere
// and add them to the test suite.
#includeint main(int argc, const char* argv[]) {
// Create a test suite
struct CTest_TestSuite* suite = CTest_Init();
// Define the output mode.
// Since v1.0.2, CTest supports two modes
// TEXT: Prints the test results in the terminal
// EXTERNAL: Enable another function to receive the results and
// do something with it.
CTest_SetMode(suite, EXTERNAL);
// If you choose EXTERNAL, you must use the following statement to
// tell CTest what function will receive the results
CTest_SetExternalListener(suite, myExternalFunction);
// Add a test to the suit.
// myTest is a function that is inside myapp/tests/mytest.h
// The third parameter is the name of the function (it will be printed in the output)
// You may add as many functions you like.
CTest_Add(suite, myTest, "myTest");
// Run all the tests
CTest_TestSuite_Run(suite);
// End of the main entry function.
return 0;
}
```Now, lets say we have a file called **mytest.h** in the same directory as **test_main.c**
```c
// mytest.h
#ifndef MYTEST_H
#define MYTEST_H#include
void myTest(struct CTest_Test*);
#endif
```
```c
// mytest.c
#includevoid myTest(struct CTest_Test* test) {
int meaningOfLife = 7 * 7;
if (meaningOfLife != 42)
// Something went wrong, so the test fails
CTest_Test_Fail(test, "The meaning of life should be 42");
else
// Test went ok.
CTest_Test_Success(test);
}
```
This test will output in the TEXT mode:
```
Test v1.0.2 -- Unit Test for CNumber of tests: 1 Number of failed tests: 1
>> myTest --> The meaning of life should be 42
```