Database Cleanup with NHibernate and Cuke4Nuke

16 views
Skip to first unread message

kberridge

unread,
Jan 26, 2010, 4:58:45 PM1/26/10
to Cukes
I know that Cucumber has some kind of transactional fixtures which can
undo all the database changes made during the run of a scenario, but I
haven't found any examples of how this works or how to set it up. And
I don't have any idea if it works with Cuke4Nuke.

Specifically, my intention is to write Cucumber tests which will call
"Application Services" (in the DDD/Fowler sense) in the When steps.
Those services will be using NHibernate wrapped in a UnitOfWork object
to manage the transaction and make changes to the Model objects.
What I'm having a hard time figuring out is how I prevent the
transaction from being committed and instead roll it back when the
scenario completes.

Are there any resources that could help me figure out how I should be
writing code like this?

Thanks,
Kevin

Matt Wynne

unread,
Jan 27, 2010, 8:41:04 AM1/27/10
to cu...@googlegroups.com

Ruby on Rails has for a long time had a 'transactional fixtures' trick
which wraps each test case in a transaction, rolling that transaction
back at the end of the test case and thus leaving it pristine. This is
what the majority of Cucumber users probably use for keeping their
database clean. This trick works fine as long as your tests are in the
same process, and thus sharing the database connection with the SUT so
that they can both see the same transaction. When you use something
like selenium to drive a separate web server process, you don't have
this option.

Ben Mabey's database cleaner[1] is a libary for Ruby which offers two
strategies for cleaning databases between scenarios. You can use
transactions or you can use truncation, which spins over all the
tables in the database and empties them. Obviously this isn't as fast
as transactions, but if you're working out of process it's better than
nothing. I think this library is pretty coupled to Ruby / Rails
projects but it might be worth a look, at least for plagarism.

I'm not aware of any .NET tools which use this concept for maintaining
test databases but there may well be some - it's trivial to implement.
You could implement this using the Before/After hooks[2] in Cuke4Nuke
pretty easily I would have thought. Let us know how you get on :)

[1]http://github.com/bmabey/database_cleaner
[2]http://github.com/richardlawrence/Cuke4Nuke/blob/master/features/hooks.feature

cheers,
Matt

http://mattwynne.net
+447974 430184

Reply all
Reply to author
Forward
0 new messages