https://github.com/pfultz2/prove
Lightweight C++ test framework
https://github.com/pfultz2/prove
c-plus-plus c-plus-plus-11 c-plus-plus-14 cpp cpp11 cpp14 test-framework unit-test unit-tests unittest
Last synced: 6 months ago
JSON representation
Lightweight C++ test framework
- Host: GitHub
- URL: https://github.com/pfultz2/prove
- Owner: pfultz2
- Created: 2015-01-06T05:20:28.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-02-07T00:16:52.000Z (about 8 years ago)
- Last Synced: 2024-12-28T09:20:29.057Z (over 1 year ago)
- Topics: c-plus-plus, c-plus-plus-11, c-plus-plus-14, cpp, cpp11, cpp14, test-framework, unit-test, unit-tests, unittest
- Language: C++
- Size: 21.5 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Prove
=====
Lightweight C++ test framework
Test cases
----------
A test case can be created by using the `prove::test_case` class with a `test()` method:
```cpp
struct my_case : prove::test_case
{
void test()
{
// Assertions
}
};
```
The library will construct the object and then run the `test()` method. The constructor and destructor can be used for setup and teardown of each test.
For convience the `PROVE_CASE` macro can be used to define a class with a `test()` method to reduce the boilerplate:
```cpp
PROVE_CASE(my_case)
{
// Assertions
}
```
Also, the `PROVE_CASE_CLASS` will define the class:
```cpp
PROVE_CASE_CLASS(my_case)
{
void test()
{
// Assertions
}
};
```
Also, the name can be left out of the macros, and it will choose a unique name.
Finally, to run all test cases, use `prove::run()`:
```cpp
int main()
{
prove::run();
}
```
Assertions
----------
The `this->check()` method can be used to check assertions:
```cpp
struct my_case : prove::test_case
{
void test()
{
this->check(true);
}
};
```
A custom message can be provided for the assertion as well:
```cpp
struct my_case : prove::test_case
{
void test()
{
this->check(true, "True is now false");
}
};
```
When checking expressions, it is useful to show the values that were used in the expression. The `PROVE_CHECK` macro can parse the expression and display the values that were used:
```cpp
PROVE_CASE()
{
int i = 5;
PROVE_CHECK(i == 5);
}
```
It will also display the source file and line number in the output.
Custom predicates
-----------------
The `prove::predicate_result` can be used to display a custom message. For example,
```cpp
prove::predicate_result equal(int x, int y)
{
prove::predicate_result r(x == y);
r << x << " == " << y << " failed";
return r;
}
struct my_case : prove::test_case
{
void test()
{
int i = 5;
this->check(equal(i, 5));
}
};
```
Template cases
--------------
Cases can be templated or be defined as nested classes in a template class. For example, when defining a template case:
```cpp
template
struct template_case : prove::test_case>
{
void test()
{
T i = 5;
PROVE_CHECK(i == 5);
}
};
```
Each template parameter can be used by the following:
```cpp
template struct template_case;
template struct template_case;
```
If there is many test case that are parameterized over the same template parameter, then each test case can be added as a nested class:
```cpp
template
struct template_suite
{
struct template_case1 : prove::test_case>
{
void test()
{
T i = 5;
PROVE_CHECK(i == 5);
}
};
struct template_case2 : prove::test_case>
{
void test()
{
T i = 3;
PROVE_CHECK(i == 3);
}
};
};
template struct template_suite;
template struct template_suite;
```