项目作者: jumbatm

项目描述 :
An even smaller Catch2 subset - Test cases, assertions and sections only. Goodbye, long test compile times!
高级语言: C++
项目地址: git://github.com/jumbatm/catch-tiny.git
创建时间: 2018-04-27T00:27:24Z
项目社区:https://github.com/jumbatm/catch-tiny

开源协议:Boost Software License 1.0

下载


Catch Tiny - A light, header-only testing framework

based on Catch2.

If you only need TEST_CASE, SECTION and REQUIRE macros, simply replace catch.hpp.

Usage

Testing revolves around the macros described above.
A test is declared with

  1. TEST_CASE(const char *name)
  2. {
  3. // ...
  4. }

, where you can name your test. This name is thrown back at you if any assertions you make fail, so you know where to look. Inside these test cases, you can use

  1. REQUIRE(bool b);

to make assertions of your code. If these fail, you’ll see output like:

  1. In test case: "Internal pointer is set to null after class is destructed."
  2. Assertion failed: REQUIRE(ptr == nullptr) at sections.cpp:27

Just like with Catch2, make sure you define CATCH_CONFIG_MAIN before including catch.hpp in one file, like:

  1. #define CATCH_CONFIG_MAIN // Define this in ONE FILE ONLY.
  2. #include "catch.hpp"

Catch Tiny will provide a main function which runs all the tests compiled with it.

In its most basic form, a test might look something like this:

main.cpp

  1. #define CATCH_CONFIG_MAIN
  2. #include "catch.hpp"

and_test.cpp

  1. #define CATCH_CONFIG_MAIN
  2. #include "catch.hpp"
  3. template <bool B1, bool B2>
  4. struct And
  5. {
  6. static constexpr bool value = false;
  7. };
  8. template <>
  9. struct And<true, true>
  10. {
  11. static constexpr bool value = true;
  12. };
  13. TEST_CASE("And struct behaves like logical AND")
  14. {
  15. REQUIRE(And<true, true>::value);
  16. REQUIRE(And<false, true>::value == false);
  17. REQUIRE(And<true, false>::value == false);
  18. }

Compiling and running this, we get:

  1. 1 of 1 test cases passed.

Fantastic.

Sections (still in progress)

Issues:

  • Test cases across files cannot begin on the same line

If you have a number of individual tests you want to perform in a specific section, you can use

  1. // ...
  2. SECTION(const char *name)
  3. {
  4. // ...
  5. }
  6. // ...

within a TEST_CASE. Anything outside a SECTION in the test case gets executed before each section, so this is useful if your tests all have something they need initialised in the same way, like in the below example:

  1. #define CATCH_CONFIG_MAIN
  2. #include "catch.hpp"
  3. TEST_CASE("Sections execute preceding lines of code.")
  4. {
  5. int num = 0;
  6. SECTION("Increment once")
  7. {
  8. ++num;
  9. }
  10. SECTION("Increment again")
  11. {
  12. num += 2;
  13. }
  14. SECTION("Increment yet again")
  15. {
  16. num += 3;
  17. REQUIRE(num == 3);
  18. }
  19. }

This also outputs

  1. 1 of 1 test cases passed.

Also see:

  • Catch2, the original testing framework this is based off. Has a ton of features.
  • Catch Mini, another minimal subset of Catch2.