开发者

Is there a cppunit equivalent to nunit's Category attribute for test cases?

开发者 https://www.devze.com 2023-04-12 05:26 出处:网络
I\'开发者_运维知识库d like an equivalent function to nUnit\'s Category attribute for test cases.

I'开发者_运维知识库d like an equivalent function to nUnit's Category attribute for test cases.

I have inherited a large number of C++ test cases, some of which are unit tests and some of which are longer-running integration tests, and I need to set up my continuous integration build process to ignore the integration test cases.

I would prefer to simply tag all the integration test cases and instruct cppunit to exclude them during CI builds.

Am I overlooking a feature of cppunit or is there an alternative way to achieve this?


There are no native test category attributes. CppUnit is a bit simpler than that. CppUnit doesn't even come with a command-line test runner for your app. You have to write your own simple main() function that executes a TestRunner.

Here's the canonical example.

#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main( int argc, char **argv)
{
  CppUnit::TextUi::TestRunner runner;
  CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
  runner.addTest( registry.makeTest() );
  bool wasSuccessful = runner.run( "", false );
  return wasSuccessful;
}

A TestSuite is a collection of TestCases. A TestRunner executes a collection of TestSuites. Notice that in this example it gets the TestSuites from the TestFactoryRegistry, which you populate by using a macro call to CPPUNIT_TEST_SUITE_REGISTRATION(MyTestSuite). But the TestCases are still your test classes.

You can certainly implement these attributes yourself, just as you would extend any class with a facade. Derive your new class from TestSuite. Add attributes to your tests that you could select on, then populate your TestRunner executing "just unit tests" or "just integration tests" or whatever you want.

For that matter, a TestRunner can select tests to execute based on name. If you named all your integration tests with a prefix like ITFoo, ITBar, etc., you could select all tests that begin with "IT".

There are dozens of ways to solve your problem, but you'll have to do it yourself. If you can write code worthy of unit testing, it shouldn't be a big deal for you. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消