How to run a subset of unit tests in CPPUnit, selecting them at runtime?

I use CppUnit as a unit test structure. Can I select a subset of test patterns to run at runtime?

Is there a filtering option in CppUnit to post this?

+4
source share
2 answers

The TestRunner :: run () method, which you most likely call in main (), actually has optional parameters: run (std :: string testName = "", bool doWait = false, bool doPrintResult = true, bool doPrintProgress = true ), testName must be the specific name of the test. You can request a specific test by name if you wish. You can also call runTest (Test *) for a specific test or runTestByName (testName).

But it looks like you want to become more sophisticated. Assuming you have registered all of your tests with the macros CPPUNIT_TEST_SUITE_REGISTRATION (), the static TestFactoryRegistry :: makeTest () method will return TestSuite for all registered tests.

The TestSuite object gives a vector through the getTests () method. You can scroll them by matching their names with a regular expression (either by index number or, if you want), and instead of calling TestRunner :: addTest (registry.makeTest ()) on the whole package, as most people do, you simply add which you are requesting.

You will need to write something to go through the tests and do the mapping, but other than that it should be dead simple. Perhaps a dozen lines of code, as well as parsing command line arguments. Use boost: regex and boost :: regex_match to make it easier for you if you have boost installed.

+3
source

If you use the test runner GUI for cppunit, you can simply check the tests you want to run.

If you cannot use the GUI test drive, check out this post - it describes a β€œcustom” way to determine which tests to run based on an XML document (the last message describes a more or less solution that I had at the end).

+1
source

Source: https://habr.com/ru/post/1309046/


All Articles