Skip to content

BenediktConze/tst

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

tst

Single-header C++20 testing framework with syntax and output similar to googletest.

Less feature-rich, but also only ~1% the size. And it doesn't trip up the LLVM TypeSanitizer (which is the reason I implemented this).

Usage

#include <cmath>
#include <stdexcept>
#include <vector>

#include "tst/tst.hpp"

// Simple unit test
// Automatically registers itself
TEST(SuiteName, TestName) {
    int i = 1;
    EXPECT_EQ(i, 1);
    EXPECT_TRUE(true);

    // Floating point comparisons based on ULP difference
    EXPECT_FLOAT_EQ(0.1f, std::nextafter(0.1f, 1.f));

    // Floating point comparisons based on absolute error
    EXPECT_NEAR(0.1, 0.1f, 0.000001);

    // Safe C-String comparisons
    EXPECT_STRCASEEQ("abc", "ABC");

    // Test if exceptions are thrown
    std::vector<int> v;
    EXPECT_THROW(i = v.at(0), std::out_of_range);
    ASSERT_NO_THROW(v.push_back(i));

    // Explicit failure
    ADD_FAILURE();
}

// Test fixtures
class MyFixture : tst::FixtureBase {
   public:
    void SetUp() override {
        /* Some common setup code */
        i = 3;
    }
    void TearDown() override { /* ... */ }

   protected:
    // Class members possible
    int i = 0;
};
TEST_F(MyFixture, FixtureTest1) {
    EXPECT_EQ(i, 3);
}

// Parametrized unit test
TEST_P(SuiteName, ParamTest, int i) {
    EXPECT_GE(i, 0);

    // Skipping tests is possible
    SKIP_TEST();
}
INSTANTIATE_TEST_SUITE_P(SuiteName, ParamTest, 0);
INSTANTIATE_TEST_SUITE_P(SuiteName, ParamTest, 1);

// Type-parametrized unit test
template <typename T>
struct MyTypedFixture : tst::FixtureBase {
    std::vector<T> value;
};

TYPED_TEST_SUITE(MyTypedFixture, int, double, char);
TYPED_TEST(MyTypedFixture, PushBack) {
    this->value.push_back(TypeParam{});
    EXPECT_LE(sizeof(TypeParam), 8);
}

int main() {
    // Call this once from anywhere
    GlobalTestManager::getInstance().runAllTests();
}

Roadmap (Maybe)

  • More expressive parametrized tests

Intentional Omissions

  • Death tests
  • Mocking and matchers

About

Single-header C++20 unit test framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages