3

I'm using cppunit for the unit tests of my project.

And I m wondering if the cppunit launch each unit test in a new fork() system call because:

  • Using fork will allow to create a new address space in which to run each unit test
  • Using fork will allow to keep the unit tests running even if one of them causes a crash.

I defined a global variable and then I tried to change the global variable in the unit test and change that has no affect in the parent process but unfortunally I found that global variable is changed.

So by default cppunit does not support launching unit test with fork(). And I m wondering if there is macros in cppunit that could initiate unit test with fork() ?

My tests are defined in the following way:

class MYTest : public CppUnit::TestCase {

   CPPUNIT_TEST_SUITE( MYTest );
   CPPUNIT_TEST( getRetryValueTest );
   CPPUNIT_TEST( getRetryValueTest2 );
   CPPUNIT_TEST_SUITE_END(); 

   public:
      virtual void setUp();

      void getRetryValueTest();
      void getRetryValueTest2();
};
12
  • Typically this is only done for "death tests" where the expectation is the test will cause the application to terminate if an error occurs (think cascading exceptions). Can't give a definitive answer here since I'm not familiar with cppUnit. Apr 16, 2013 at 16:17
  • 4
    Seems like it would be easy enough to check for yourself. Write two tests, and have each print the current process ID. Do they both print the same values? Apr 16, 2013 at 16:20
  • That would be a good way to ensure that one test doesn't influence another, but sometimes you WANT tests to affect each other [without having to save state in a file, or some such]. I would have thought it ideal to have that as an option - although I don't know how cppunit works. Apr 16, 2013 at 16:26
  • @RobKennedy, you are right it's a claver idea, but the cppunit could support both and could have macros to initiate unit tests with fork. and if they exists I wich to know them
    – MOHAMED
    Apr 16, 2013 at 16:34
  • 7
    If tests influence each other then they are not correct unit tests... Apr 16, 2013 at 16:52

1 Answer 1

3

Cppunit does not use fork to separate tests. However tests in the same suite are separate objects so each test has its own set of variables and are therefore independent from each other and can't influence each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.