Testing API

Ark uses gtest for all of its testing. There is a handy macro available to add your unit test and set it up to run under make check:

add_unit_test(
    my_unit_test
    my_unit_test_1.cc
    my_unit_test_2.cc)

target_link_libraries(
    my_unit_test
    PRIVATE GTest::gtest GTest::gtest_main)

This will automatically output your test into build/tests. If you want to run make check the build system will ensure all unit tests are build before running the tests. The make test cmake command will just run the tests without first making sure they are up to date.

Beyond that, there are a few helper APIs for testing that are useful:

  • ark::testing::ScopedDirectory - for getting a location to write data to
  • EXPECT_EXCEPTION_MESSAGE_CONTAINS - for testing exception text
  • chrono.hh - for printing std::chrono timestamps correctly

Testing Directories

If you are emitting files during your unit tests, you should use the ScopedDirectory API. This will provide you with a unique location to write files out to that is cleaned up when your test ends.

These files are written out to /tmp/ark_test/<guid/... by default.

For example:

ark::testing::ScopedDirectory directory;

auto path = directory.path() / "my_file.txt";

core::write_string_to_file(path, "Hello world!");

Once the directory object falls out of scope, the entire path is removed from disk.

It optionally takes one parameter, a user_path, which allows you to make a subdirectory with your name in it, rather than a unique path. You most likely don’t need to use this during normal operation, but it can be used to potentially make debugging a bit easier, and to further namespace your files.

There is also an API get_testing_root_directory which will return the root where all scoped directories are created (typically /tmp/ark_test).

Exception Messages

Sometimes just checking for an exception type is not enough. The macro EXPECT_EXCEPTION_MESSAGE_CONTAINS will automatically fail if you either don’t throw an exception, or do throw an exception but it doesn’t contain the expected text.

For example:

EXPECT_EXCEPTION_MESSAGE_CONTAINS(my_throwing_function(), "expected text");