This is an simple tutorial on how to use the Python pytest
testing tool as a testrunner
for CTEST unit tests.
CTEST is a C unit test framework. It is self contained in a single header file but also contains an integrated test runner.
It goes way beyond other macro based C unit tests frameworks while still providing a test collector and runner without external programs. It uses a large amount of C magic to do this but the end result is a lightweight yet fully functional C unit test framework.
It provides most of the functionality of GoogleTest or Unity but without the overhead of a large support framework or external scaffolding.
It also has nice clear output. Here is a example from the sample unit test in the CTEST repo:
CTEST has its own test runner and, as already stated, it is standalone and doesn't need external scaffolding apart from a simple main()
.
Nevertheless, the example shown here used Pytest
as a test runner for CTEST. This offers a small amount of additional functionality to CTEST and is presented mainly as a proof of concept.
Pytest
is a really nice Python testing tool. It has great documentation, clean code, lots of tests, a large but clear set of options for running tests and collecting results and best of all it is easily extensible.
Pytest supports the creation of hooks in a file called conftest.py
. This allows you to define, in Python, code to find test files, run them, and collect the results. The nice thing about this is that you can create a hook layer between pytest and the CTEST unit tests without changing code in either one.
Here is the same, unmodified, CTEST test case run under pytest:
Pytest allows a large range of filter and commandline options such as the following to give a minimal test output:
The pytest hooks are contained in the conftest.py
program and are explained as a general concept in Using pytest as a testrunner for C unit tests.