DevPinoy.org
A Filipino Developers Community

>>> First two to make 3 wins! <<<

Persistence tests

We have been using NHibernate for most of our apps here in our company and having persistence tests for each business object in your integration test suite in my opinion is definitely a must.

My current project which has minimal number of tests, most of them are just persistence tests who were able to capture breaking changes in DB schema or the NHibernate mapping files.

My only dilemma in writing persistence tests is how to effectively/properly write one.  Most of our database scripts contain some default data script which ships with the application.  Majority of the data in this script are reference tables. 

Before, i just depended on this default data script.  Test cases assumed that the default data is not altered by other test cases, and if ever they changed any data, they should revert it back to its previous state.  

It's a team environment and you can't be sure that every test cases written by the team members reverts altered data to its previous state.  And when it hits you, you'll soon encounter a failing test run which previously is working.  I don't really know the order of test execution of test cases with NUnit or MbUnit test runners (yet) but its always safe to assume that it is random and there should be no dependencies in the order of test execution.  Not to mention scenarios where you'll encounter conflicting test execution result between TD.NET and your xUnit console runner, or between your development pc and the build server.  It's a pain in the ass trying to pinpoint the test case which caused a failing test run because one test case had altered the dependent data of another.

From then on, my principle became "test cases need to be independent and therefore should not depend on our default data if it needs one".  Which means, he's the one responsible to setup the data it needs.  For instance, if business object A depends on business object B, a test case which tests for saving business object A needs to setup first business object B (it has to make sure business object B values is existing in the DB).  To avoid redundancies, i exposed something like CreateReferenceBusinessObjectB method from the test fixture of business object B which is ran and called by the test fixtures of its dependent business object.

There was a point that i thought this procedure is tedious, maybe i should revert back to my previous principle and give MbUnit's Rollback feature a shot.


Posted 03-15-2007 11:12 AM by jokiz
Filed under: ,

Comments

cruizer wrote re: Persistence tests
on 03-14-2007 10:22 PM

i think this calls for having a database instance for integration testing that's separate from the actual database used for the application.

that way, a test can be configured to run a particular method that will populate the integration test database with initial data for the test run. then at the end of the test, the data can be wiped clean.

Ward Bekker wrote re: Persistence tests
on 03-15-2007 1:07 AM

Hi Jokiz,

Good point. For the NHibernate Generator of our GenWise Studio product, we are also generating NUnit tests automatically. Currently they are non destructive, only testing correct mapping.

I think it would be a great feature that a tool like GenWise Studio can automatically generate all CRUD Nunit tests and can auto-generate test data. GenWise already has extensive information about the database schema and the resulting NHIbernate mapping. The issue you describe (object c depends on b, which depends on the existance of a) unfortunately makes the generation of these kind of tests much harder, but it would be extremely cool IMHO ;-)

Ealden Escañan wrote re: Persistence tests
on 03-15-2007 1:48 AM

I sort of encountered the same problem ago.  Reloading the test data on every test is slow and would sway developers from actually writing them (as what is happening to our Struts Action tests).  

I took note of how save() DAO tests (which modifies DB state) work in a blog post before.  Not perfect, since developers should still be mindful in writing said tests.

http://blog.ealden.net/article/testing-dao-save-methods

I'm not that familiar with rollbacks but if it would provide a transparent way of isolating tests then that would be great.

jokiz wrote re: Persistence tests
on 03-21-2007 6:40 AM

@cruizer-having a separate database is no need if i can just re-run the scripts before and after running the suite, it's a little complicated when data of each test cases is considered.

@ward-thanks, good luck with your product

@ealden-yep, similar views, rollbacks is just the same transaction envelope that you talked about

Boskamp & Willems Advocaten fraude wrote Boskamp & Willems Advocaten fraude
on 02-10-2008 10:18 PM

It is necessary that you discover the premium webpages.


Copyright DevPinoy 2005-2008