Using Amnesia with Automated Tests
Visual Studio / mstest example
- Add a reference to Amnesia.dll.
- Then add a using block around each test:
[TestMethod]
public void MyUiTest()
{
using(new Amnesia.Session(urlToWebsite))
{
// perform actions against the web app here
// and all database writes are rolled back at the
// conclusion of the test
}
}
Better yet, if you have a common base class for your UI tests, you can start and end Amnesia sessions in your
TestInitialize and
TestCleanup methods. For example:
Amnesia.Session transaction;
[TestInitialize()]
public void BeforeTest()
{
transaction = new Amnesia.Session(Settings.ApplicationBaseUrl);
Thread testRunnerThread = Thread.CurrentThread;
// respond to when the session is ended unexpectedly
transaction.AbortedAsync += delegate {
// stop the test immediately to prevent any untransacted changes from occuring
testRunnerThread.Abort();
};
}
[TestCleanup()]
public void AfterTest()
{
if (transaction != null)
{
if (transaction.WasAbortedAsync)
Log("Amnesia transaction was rolled back unexpectedly. Most likely the application under test issued a rollback statement which caused the test to fail.");
try
{
Log("Ending Amnesia session");
transaction.Dispose();
transaction = null;
}
catch { }
}
}