Skip to content

karnkaul/dtest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dtest

Tiny test harness library in C++17.

Features

  1. Define multiple tests in single cpp files using TEST(name) { // body ... }
  2. Perform checks using EXPECT_X and ASSERT_X macros
  3. Multi-threaded execution of tests (can be disabled)
  4. Consolidated report per test
  5. Expression, filename, and line number per expectation / assertion failure
  6. Configurable test fail return code (1 by default)

Limitations

  1. No sub-tests / test cases / test groups
  2. Global / per-test environment

Usage

Requirements

  • CMake 3.3
  • C++17 compiler (and stdlib)

Steps

  1. Clone repo to appropriate subdirectory, say dtest
  2. Add library to project via: add_subdirectory(dtest)
  3. To use just the test engine, link to dtest::dtest: target_link_libraries(foo dtest::dtest), and in main() call dtests::run_tests(bool async)
  4. To also use the built-in main function, link to dtest::main: target_link_libraries(foo dtest::main)
  5. Use via: #include <dumb_test/dtest.hpp>

Examples

add_executable(foo dtest_demo.cpp)
target_link_libraries(foo PRIVATE dtest::main)
// dtest_demo.cpp
#include <dumb_test/dtest.hpp>

namespace {
TEST(foo_test) {
  int const arr[] = {0, 1, 2};
  ASSERT_EQ(sizeof(arr) / sizeof(int), 3U); // stops further execution if assertion fails
  EXPECT_EQ(arr[0], 0);                     // records test failure and continues execution
}

struct f {
  int val{};
};
bool operator==(f a, f b) { return a.val == b.val; }

TEST(fail_test) {
  char const* x = "hi";
  std::string_view const y = "hello";
  EXPECT_EQ(x, y);  // prints values
  EXPECT_EQ(&x, nullptr); // prints address and "nullptr"
  f const a{99};
  f const b{-42};
  EXPECT_EQ(a, b);  // prints failed expression (a == b)
}
} // namespace

Output:

[======] starting 2 tests
[ pass ] foo_test
[ FAIL ] fail_test
        expected: hi == hello (dtest_demo.cpp | 18)
        expected: 0x7fc089637948 == nullptr (dtest_demo.cpp | 19)
        expected: a == b  (dtest_demo.cpp | 22)
[======] completed 2 tests (0.3ms)
[ pass ] 1 / 2
[ FAIL ] 1 / 2

Configuration

These CMake variables can be modified for customization:

  1. DTEST_FAIL_RETURN_CODE: Return code on test failure (int)
  2. DTEST_MULTITHREADED : Whether dtest::main uses multiple threads (bool)

Contributing

Pull/merge requests are welcome.

Original repository

About

Tiny C++17 test harness library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages