Giter Site home page Giter Site logo

mini-test's Introduction

Mini-Test : C++20 Unit Testing Framework

Lightweight and elegant single header unit testing framework for personal and small C++ 20 and above projects.

Mini-Test has a familiar interface emulating the Google Testing Framework. When it's time to move on to a more robust testing framework such as Google Test, only minimal refactoring is required. Mini-Test has no dependencies except the C++ standard library, and nothing to build - simply copy minitest.h into your project and include it. The entire testing framework is only about 200~ lines of code, the result is a mimic of a robust framework with none of the hassle.

Example of a unit test:

// NOTE: the braces{} are optional but recommended for better readability.
auto my_method() { return true; }
MINITEST(MyTest,MyTestCase){
  EXPECT_TRUE(my_method());
  EXPECT_TRUE_LOG(false,"error!"+ my_method()); // All checks and asserts have a log
                                                // version recieving a string.
  EXPECT_FALSE(false);
  EXPECT_EQ(1,1);
  EXPECT_NE(1,2);
  EXPECT_ANY_THROW([](){ throw std::runtime_error("error"); });
  EXPECT_NO_THROW([](){ throw std::runtime_error("error"); });
}
END_MINITEST;

Example of a fixture unit test:

struct MyFixture : minitest::Fixture {
  int my_foo = 0;
  void SetUp() override {
    std::cout << "Setting up the fixture\n";
  }

  void TearDown() override {
    std::cout << "Tearing down the fixture\n";
  }
};

// Example of a fixture unit test.
MINITEST_F(MyFixtureTest, MyFixtureTestCase, MyFixture) {
  EXPECT_TRUE(my_foo == 0);
}
END_MINITEST_F(MyFixtureTestCase);

Example of retrieving the test result/accessing result later at runtime:

FINISH_MINITESTS;  // Macro to finish the test suite, call before main function.

// Main Function is unaffected by the unit tests.
int main() {
  std::cout << "Hello World!\n";

  if (MINITESTS_RESULT)
    std::cout << "Testing was successful.\n";
  else
    std::cout << "A test case failed.\n";

  return MINITESTS_RESULT ? 0 : 1; // example of return result based on tests.
}

Example Output:

---------------------------------------------------------------------------------------------------------------------
[Begin Mini Test] MyTest [Case]MyTestCase
---------------------------------------------------------------------------------------------------------------------
[FAIL] Expected TRUE.
[FAIL] Expected FALSE.
[FAIL] Expected Equality with: >|1 Got: 2
[FAIL] Expected Inequality with>|1
[FAIL] Expected exception but got none.
[FAIL] Expected no exception but one was raised.
---------------------------------------------------------------------------------------------------------------------
[End Mini Test] MyTest [Case]MyTestCase
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
[Begin Mini Test] MyFixtureTest [Case]MyFixtureTestCase
---------------------------------------------------------------------------------------------------------------------
Setting up the fixture
[FAIL] Expected Equality with: >|0 Got: 1
[FAIL] Expected Inequality with>|0
[FAIL] Expected exception but got none.
[FAIL] Expected no exception but one was raised.
Tearing down the fixture
---------------------------------------------------------------------------------------------------------------------
[End Mini Test] MyFixtureTest [Case]MyFixtureTestCase
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
Failed Tests:
---------------------------------------------------------------------------------------------------------------------
[FAILURE DETECTED] Test: MyTest Case: MyTestCase On Check:[EXPECTATION FAILED]: EXPECT_TRUE
[FAILURE DETECTED] Test: MyTest Case: MyTestCase On Check:[EXPECTATION FAILED]: EXPECT_FALSE
[FAILURE DETECTED] Test: MyTest Case: MyTestCase On Check:[EXPECTATION FAILED]: EXPECT_EQ
[FAILURE DETECTED] Test: MyTest Case: MyTestCase On Check:[EXPECTATION FAILED]: EXPECT_NE
[FAILURE DETECTED] Test: MyTest Case: MyTestCase On Check:[EXPECTATION FAILED]: EXPECT_ANY_THROW
[FAILURE DETECTED] Test: MyTest Case: MyTestCase On Check:[EXPECTATION FAILED]: EXPECT_NO_THROW
[FAILURE DETECTED] Test: MyFixtureTestCase Case: MyFixtureTest On Check:[EXPECTATION FAILED]: EXPECT_EQ
[FAILURE DETECTED] Test: MyFixtureTestCase Case: MyFixtureTest On Check:[EXPECTATION FAILED]: EXPECT_NE
[FAILURE DETECTED] Test: MyFixtureTestCase Case: MyFixtureTest On Check:[EXPECTATION FAILED]: EXPECT_ANY_THROW
[FAILURE DETECTED] Test: MyFixtureTestCase Case: MyFixtureTest On Check:[EXPECTATION FAILED]: EXPECT_NO_THROW
---------------------------------------------------------------------------------------------------------------------
End of Failed Tests:
---------------------------------------------------------------------------------------------------------------------
Hello World!
A test case failed.

mini-test's People

Contributors

zhymet avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.