I am new to CppUnit and I am trying to figure out how to extend CppUnit to my needs:
1) I need to do database- and file-testing:
Database: I have a class that modifies a database. I want to write testcases that check if the changes the class did to the database are correct. So what I did is write a setup()-method that loads the database with predefined values. The teardown()-method dumps the changed database and compares the dump with a predefined result. It throws an assertion if the result-database (the database changed by the testcase) does not match the predefined result. Simple, but it works nice. I did the same for files, testing if the changes applied to them are correct.
The point is, I do not want to implement these setup()- and teardown()-methods in every fixture. I want something like a DatabaseFixture and a FileFixture, so the test-case-developer can inherit from these classes and are hidden from him.
So my question is, can I use the TestDecorator for that purpose? How does it work? Or do I have to have to change the cppunit-sources ?
2) I want to do the same as in 1) with mpatrol for memory-leakage tests. Has anyone done this ?
Any help appreciated,
Ray
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1) I'm not sure I understand correctly what you want, but it seems subclassing test fixture is what you want. Give a look to CPPUNIT_TEST_SUB_SUITE and the hierarchy example.
2) I don't know what mpatrol is, but you can use TestListener to instrument before and after test case execution.
Baptiste.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This sounds more like a general design issue rather than anything specific to C++. I agree that a DatabaseFixture class would be helpful. However, it's been a while since I've used cppunit, so I don't remember how different it is from JUnit. I believe that the setUp() and tearDown() functions are inherited from TestCase or one of its parents. You could make DatabaseFiexture inherit from TestCase and then all of your specific database tests can inherit from this DatabaseFixture class.
I don't agree with the decision to put assertions in the tearDown() function. From my understanding, tearDown() is simply for cleaning up. In this case, you would just remove the database, or at least restore it to its original condition. You should create another method for checking the changes. Besides, these changes will presumably be different between different tests, so you need to have different logic each time.
One other thing you might consider is grouping a bunch of testXxx() methods in the same TestCase class. These tests will all have a similar initial database configuration. This technique could avoid having a DatabaseFixture class by centralizing the code all in a single class rather than spanning it across a hierarchy.
I hope some of these ideas help you. Good luck and Keep Coding!
Layne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I am new to CppUnit and I am trying to figure out how to extend CppUnit to my needs:
1) I need to do database- and file-testing:
Database: I have a class that modifies a database. I want to write testcases that check if the changes the class did to the database are correct. So what I did is write a setup()-method that loads the database with predefined values. The teardown()-method dumps the changed database and compares the dump with a predefined result. It throws an assertion if the result-database (the database changed by the testcase) does not match the predefined result. Simple, but it works nice. I did the same for files, testing if the changes applied to them are correct.
The point is, I do not want to implement these setup()- and teardown()-methods in every fixture. I want something like a DatabaseFixture and a FileFixture, so the test-case-developer can inherit from these classes and are hidden from him.
So my question is, can I use the TestDecorator for that purpose? How does it work? Or do I have to have to change the cppunit-sources ?
2) I want to do the same as in 1) with mpatrol for memory-leakage tests. Has anyone done this ?
Any help appreciated,
Ray
1) I'm not sure I understand correctly what you want, but it seems subclassing test fixture is what you want. Give a look to CPPUNIT_TEST_SUB_SUITE and the hierarchy example.
2) I don't know what mpatrol is, but you can use TestListener to instrument before and after test case execution.
Baptiste.
This sounds more like a general design issue rather than anything specific to C++. I agree that a DatabaseFixture class would be helpful. However, it's been a while since I've used cppunit, so I don't remember how different it is from JUnit. I believe that the setUp() and tearDown() functions are inherited from TestCase or one of its parents. You could make DatabaseFiexture inherit from TestCase and then all of your specific database tests can inherit from this DatabaseFixture class.
I don't agree with the decision to put assertions in the tearDown() function. From my understanding, tearDown() is simply for cleaning up. In this case, you would just remove the database, or at least restore it to its original condition. You should create another method for checking the changes. Besides, these changes will presumably be different between different tests, so you need to have different logic each time.
One other thing you might consider is grouping a bunch of testXxx() methods in the same TestCase class. These tests will all have a similar initial database configuration. This technique could avoid having a DatabaseFixture class by centralizing the code all in a single class rather than spanning it across a hierarchy.
I hope some of these ideas help you. Good luck and Keep Coding!
Layne