Using Amnesia with Automated Tests

Visual Studio / mstest example

  1. Add a reference to Amnesia.dll.
  2. 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 { }
	}
}

Last edited Nov 23, 2010 at 2:39 PM by matthooper, version 5

Comments

No comments yet.