Unit Tests¶
Testing a Stage¶
If you wrote a new Stage or you added to it’s functionality, you should probably test if it behaves as expected in all the possible cases. To do this you can write a test using pytest and various tools from runner.py like in the following example.
Example¶
Example test tests/test_<name>.py:
import pytest
from kotekan import runner
# this is the equivalent of the pipeline config file for kotekan to run your test
params = {
"num_elements": 7,
"num_ev": 0,
"total_frames": 128,
"cadence": 10.0,
"mode": "default",
"variable_my_stage_needs": -1,
}
# this runs kotekan and yields the data you want to inspect
@pytest.fixture(scope="module")
def data(tmpdir_factory):
# keep all the data this test produces in a tmp directory
tmpdir = tmpdir_factory.mktemp("name_of_the_test_case")
# you can use FakeVisBuffer as a mock producer stage
# to produce a Kotekan buffer with fake data
fakevis_buffer = runner.FakeVisBuffer(
num_frames=params["total_frames"], mode=params["mode"]
)
# DumpVisBuffer can be used to dump data
# afterwards for asserting expectations
dump_buffer = runner.DumpVisBuffer(str(tmpdir))
# KotekanStageTester is used to run your
# kotekan stage with your config
# Replace stage_type with the name of your stage
test = runner.KotekanStageTester(
stage_type="stageUnderTest",
stage_config={},
buffers_in=fakevis_buffer,
buffers_out=dump_buffer,
global_config=params,
)
test.run()
# here the data that the stage under test
# put out is passed on to test the stage
yield dump_buffer.load()
# this is the actual test (give a name to it)
def test_<name>(data):
for frame in data:
assert frame.vis == {1, 0}
You can run your test with
pytest tests/test_<name>.py
Testing a C++ Function¶
If you want to test a single C++ Function, you can write a test using the Boost Unit Testing Framework as demonstrated by the following example.
Example¶
Write a test like this one (Replace
<name>with what you are testing):
Example test tests/boost/test_<name>.cpp:
#define BOOST_TEST_MODULE "test_<name>"
#include <boost/test/included/unit_<test>.hpp>
// include the code you want to test:
#include "<name>.hpp"
// Split your tests into test cases
BOOST_AUTO_TEST_CASE(_some_useful_test_case_name) {
BOOST_CHECK_EQUAL(function(1), 1);
BOOST_CHECK_EQUAL(function(6), 42);
BOOST_CHECK_EQUAL(function(26), 4861946401452);
}
Add
add_executable(test_<name> test_<name>.cpp)andtarget_link_libraries(test_<name> PRIVATE <all_used_libs>)to/tests/boost/CMakeLists.txt.Build kotekan with the cmake option
-DWITH_BOOST_TESTS=ONunder/kotekan/build.Make sure
pytest-cppis installed.Run your test with
pytest tests/boost/test_<name>.