I want to implement integration tests of my Entity Framework driven repositories. The problem is how to rollback database state after tests are done. At the moment I'm planning to start transaction at test SetUp and roll it back at test TearDown. Are there any other solutions excepting manual database clearing?
Annotation Type Rollback@Rollback is a test annotation that is used to indicate whether a test-managed transaction should be rolled back after the test method has completed.
In most cases, integration testing doesn't require any specific tools. A QA team often runs these tests manually. Usually, it happens in parallel with the development – that's probably the most efficient timing.
We do this in our integration tests while using MSTest. We use the TransactionScope
and implement a test setup and teardown in a base class. This allows you to run all integration tests within a transaction. The base class looks much like this:
public class IntegrationTestsBase
{
private TransactionScope scope;
[TestInitialize]
public void Initialize()
{
this.scope = new TransactionScope();
}
[TestCleanup]
public void TestCleanup()
{
this.scope.Dispose();
}
}
Good luck.
I think you're on the right track....
Here's an example doing the same with Linq To SQL that you can tweek for yourself.
This link describes three options:
The post goes on to describe that transactions while the fastest are tied to a single session and can create some real problems/restrictions. Use if you can....
Rebuilding the DB is slow but definitely doable but using snapshots is fast and gets around the transaction restrictions.
If you have a need to have very high performance in your automated tests try this from the same blogger. He describes using MS Distributed Transaction Coordinator to eliminate the transactional restrictions of a single session.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With