Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip a BOOST unit test?

How can I skip a BOOST unit test? I would like to programatically skip some of my unit tests depending on, (for instance) the platform on which I am executing them. My current solution is:

#define REQUIRE_LINUX char * os_cpu = getenv("OS_CPU"); if ( os_cpu != "Linux-x86_64" ) return;

BOOST_AUTO_TEST_CASE(onlylinux) {
    REQUIRE_LINUX
    ...
    the rest of the test code.
}

(note that our build environment sets the variable OS_CPU). This seems ugly and error-prone, and also like the silent skips could cause users to be skipping tests without knowing about it.

How can I cleanly skip boost unit tests based on arbitrary logic?

like image 744
dbn Avatar asked Jun 04 '13 23:06

dbn


People also ask

How can I run unit test faster?

Run your tests in parallel I've already covered this in another blog post “PARALLELISM: NUNIT VS. XUNIT” that running your tests in parallel can significantly improve the speed of your test runs. Basically this means all your tests will run "next to each other" instead of "one by one".

How long is too long for a unit test?

Thus, a unit test suite used for TDD should run in less than 10 seconds. If it's slower, you'll be less productive because you'll constantly lose focus.

What is test adapter for boost test?

The Test Adapter for Boost. Test is a unit testing extension published by Microsoft and based on the existing Boost Unit Test Adapter (v1. 0.8) Visual Studio extension by Gunter Wirth's team from ETAS GmbH. This extension is developed in collaboration with the original project with the aim of improving Boost.


2 Answers

BOOST_AUTO_TEST_CASE(a_test_name,*boost::unit_test::disabled())

{
   ...
}
like image 125
Sergei Krivonos Avatar answered Oct 02 '22 16:10

Sergei Krivonos


Use enable_if / enable / precondition decorators.

boost::test_tools::assertion_result is_linux(boost::unit_test::test_unit_id)
{
  return isLinux;
}


BOOST_AUTO_TEST_SUITE(MyTestSuite)

BOOST_AUTO_TEST_CASE(MyTestCase,
                     * boost::unit_test::precondition(is_linux))
{...}

precondition is evaluated at runtime, enable, enable_if at compile time.

See: http://www.boost.org/doc/libs/1_61_0/libs/test/doc/html/boost_test/tests_organization/enabling.html

like image 29
Horus Avatar answered Oct 02 '22 14:10

Horus