Create theWebTestClassInitializeandWebTestClassCleanupattributes
to mark methods that should run at the beginning and at the end of a test class.
One of the features of NUnit that I really loved was the fact they walked the class inheritance chain looking for their [TestSetup] attribute. This turned out to be really powerful because I could create a base test class with a [TestSetup] attribute over
the TestSetup() Method. That method did all the common stuff and then called an empty virtual method that individual tests could override.
I structure our test cases in a very specific manner. I do not test by functionality I test by Page. The idea is if you are a developer and you make a change to a page, you can come into our test framework and run all the tests for the page you just changed,
on your dev box BEFORE you checking. Kindof like an automated deck check.
What this means is we can use a TestSetup() type method to get all the data ready for each test on the page. For example If we are testing a ShoppingList we want to make sure its cleared before each test begins. And since we have all the ShoppingList tests
in one TestClass, we can isolate all the setup and teardown code need for a single page into its class.
The problem this creates is that there is now a lot of duplicate code in each of these test pages. Code that authenticates and logs into the site, code that navigates to the page being tested, and all the small stuff shared across every page.
Here is some PseudoCode that shows what I did in NUnit and WatiN.
[WebTestClass]
public class BaseTestClass
{
[WebTestClassInitialize]
public void TestSetup()
{
// Login to the test site and open the test page
TestSite.Authenticate();
StartPageAdapter.NavigateTo();
CustomTestSetup();
}
public virtual void CustomTestSetup() {}
}
// ------------------------------------
public class CustomerAccountsPageTests: BaseTestClass
{
public override CustomTestSetup()
{
// Reset Test Data before each test on this page
AutomationDataManager.ResetTestData();
}
[WebTestMethod]
[WebTestTag(WebTestCategoryTag.Smoke)]
public void ValidateCustomerAccount1()
{
// Do Test
Assert.IsTrue(SomeTest.Results);
}
}
A couple of important points.
The [WebTestClass] attribute is on the base class and not the derived class. So the LATF would need to be able to reflect to get that as well
The [WebTestMethod] attribute would still have to be on each test method.
And if that wasn't enough, I think it would be really cool to be able to have [WebTestMethods] in the base class as well. So a set of basic tests that are run on every TestClass.
This is not a high priority for us, I am hacking around this by using a TestSetup(); method call at the start of each test, but the Attributes would allow me to remove this call and have it done automatically .. it would be cleaner.
Michael Cowan
Aisle7
QA Manager (QA Lead)
michael.cowan@healthnotes.com
mcowan
Member
8 Points
42 Posts
WebTestClassInitialize Suggestion: Use reflection to look into base class for the attribute
Apr 05, 2009 06:49 PM|LINK
In the RoadMap you mentioned this:
Create the WebTestClassInitialize and WebTestClassCleanup attributes to mark methods that should run at the beginning and at the end of a test class.
One of the features of NUnit that I really loved was the fact they walked the class inheritance chain looking for their [TestSetup] attribute. This turned out to be really powerful because I could create a base test class with a [TestSetup] attribute over the TestSetup() Method. That method did all the common stuff and then called an empty virtual method that individual tests could override.
I structure our test cases in a very specific manner. I do not test by functionality I test by Page. The idea is if you are a developer and you make a change to a page, you can come into our test framework and run all the tests for the page you just changed, on your dev box BEFORE you checking. Kindof like an automated deck check.
What this means is we can use a TestSetup() type method to get all the data ready for each test on the page. For example If we are testing a ShoppingList we want to make sure its cleared before each test begins. And since we have all the ShoppingList tests in one TestClass, we can isolate all the setup and teardown code need for a single page into its class.
The problem this creates is that there is now a lot of duplicate code in each of these test pages. Code that authenticates and logs into the site, code that navigates to the page being tested, and all the small stuff shared across every page.
Here is some PseudoCode that shows what I did in NUnit and WatiN.
[WebTestClass] public class BaseTestClass { [WebTestClassInitialize] public void TestSetup() { // Login to the test site and open the test page TestSite.Authenticate(); StartPageAdapter.NavigateTo(); CustomTestSetup(); } public virtual void CustomTestSetup() {} } // ------------------------------------ public class CustomerAccountsPageTests: BaseTestClass { public override CustomTestSetup() { // Reset Test Data before each test on this page AutomationDataManager.ResetTestData(); } [WebTestMethod] [WebTestTag(WebTestCategoryTag.Smoke)] public void ValidateCustomerAccount1() { // Do Test Assert.IsTrue(SomeTest.Results); } }A couple of important points.
And if that wasn't enough, I think it would be really cool to be able to have [WebTestMethods] in the base class as well. So a set of basic tests that are run on every TestClass.
This is not a high priority for us, I am hacking around this by using a TestSetup(); method call at the start of each test, but the Attributes would allow me to remove this call and have it done automatically .. it would be cleaner.
Aisle7
QA Manager (QA Lead)
michael.cowan@healthnotes.com