Last active
February 5, 2023 08:02
-
-
Save drmalex07/13595d195c18adad4aa62420c7213a11 to your computer and use it in GitHub Desktop.
An example unit test with Boost.Test. #boost #c++ #unit-test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile with -DBOOST_TEST_NO_MAIN | |
// Link with -lboost_unit_test_framework | |
#define BOOST_TEST_DYN_LINK | |
#define BOOST_TEST_MODULE Test1 | |
#include <boost/test/unit_test.hpp> | |
#include <boost/test/parameterized_test.hpp> | |
#include <boost/bind.hpp> | |
// ... as before | |
// Define suite | |
using namespace boost::unit_test; | |
static bool init_unit_test_suite() | |
{ | |
callback1<const parameters&> test1m = bind(&tester::test1, &t1, _1); | |
framework::master_test_suite() | |
.add(BOOST_PARAM_TEST_CASE(&test1f, params.cbegin(), params.cend())); | |
framework::master_test_suite() | |
.add(BOOST_PARAM_TEST_CASE(test1m, params.cbegin(), params.cend())); | |
return true; | |
} | |
// Exported functions | |
void test_1(int argc, char** argv) | |
{ | |
unit_test_main(&init_unit_test_suite, argc, argv); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/test/included/unit_test.hpp> | |
#include <boost/test/parameterized_test.hpp> | |
using namespace boost::unit_test; | |
// A function-based test | |
void test1f(int i) | |
{ | |
BOOST_CHECK(i < 4); | |
} | |
// A method-based test | |
struct tester { | |
int n; | |
tester(int n): n(n) {} | |
void test1(int i) | |
{ | |
BOOST_CHECK(i < n); | |
} | |
}; | |
tester t1(4); | |
// Initialize suite | |
void init_unit_test_suite(int argc, char* argv[]) | |
{ | |
int params[] = { 1, 2, 3, 4, 5 }; | |
callback1<double> test1m = bind(&tester::test1, &t1, _1); | |
framework::master_test_suite() | |
.add(BOOST_PARAM_TEST_CASE(&test1f, params, params + 5)); | |
framework::master_test_suite() | |
.add(BOOST_PARAM_TEST_CASE(test1m, params, params + 5)); | |
return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/test/included/unit_test.hpp> | |
#include <boost/test/parameterized_test.hpp> | |
using namespace boost::unit_test; | |
struct parameters | |
{ | |
int u,v; | |
parameters(int u, int v): u(u), v(v) {} | |
}; | |
// A function-based test | |
void test1f(const parameters& p) | |
{ | |
BOOST_CHECK(p.u < 4); | |
} | |
// A method-based test | |
struct tester | |
{ | |
int n; | |
tester(int n): n(n) {} | |
void test1(const parameters& p) | |
{ | |
std::cerr << "p.u" << p.u << " p.v=" << p.v << std::endl; | |
BOOST_CHECK(p.u < n); | |
} | |
}; | |
tester t1(4); | |
// Parameter sets | |
std::array<parameters, 4> params = { | |
parameters(1, 5), | |
parameters(2, 7), | |
parameters(3, 6), | |
parameters(5, 9), | |
}; | |
// Initialize suite | |
void init_unit_test_suite(int argc, char* argv[]) | |
{ | |
callback1<const parameters&> test1m = bind(&tester::test1, &t1, _1); | |
framework::master_test_suite() | |
.add(BOOST_PARAM_TEST_CASE(&test1f, params.cbegin(), params.cend())); | |
framework::master_test_suite() | |
.add(BOOST_PARAM_TEST_CASE(test1m, params.cbegin(), params.cend())); | |
return; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BOOST_TEST_MODULE Test1 | |
#include <boost/test/included/unit_test.hpp> | |
BOOST_AUTO_TEST_CASE(test1a) | |
{ | |
int i = 1; | |
BOOST_CHECK(i > 4); | |
BOOST_CHECK(i > i * i); | |
} | |
BOOST_AUTO_TEST_CASE(test1b) | |
{ | |
int i = 5; | |
BOOST_CHECK(i > 4); | |
BOOST_CHECK(i > i * i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment