Erpel
Erpel

Reputation: 150

isolating database operations for integration tests

I am using NHibernate and ASP.NET/MVC. I use one session per request to handle database operations. For integration testing I am looking for a way to have each test run in an isolated mode that will not change the database and interfere with other tests running in parallel. Something like a transaction that can be rolled back at the end of the test. The main challenge is that each test can make multiple requests. If one request changes data the next request must be able to see these changes etc.

I tried binding the session to the auth cookie to create child sessions for the following requests of a test. But that does not work well, as neither sessions nor transactions are threadsafe in NHibernate. (it results in trying to open multiple DataReaders on the same connection) I also checked if TransactionScope could be a way, but could not figure out how to use it from multple threads/requests.

What could be a good way to make this happen?

Upvotes: 0

Views: 401

Answers (1)

Andrei Dragotoniu
Andrei Dragotoniu

Reputation: 6335

I typically do this by operating on different data.

for example, say I have an integration test which checks a basket total for an e-commerce website.

I would go and create a new user, activate it, add some items to a basket, calculate the total, delete all created data and assert on whatever I need.

so the flow is : create the data you need, operate on it, check it, delete it. This way all the tests can run in parallel and don't interfere with each other, plus the data is always cleaned up.

Upvotes: 3

Related Questions