Unit Testing Attribute Extensions
IMPORTANT: If you want to use the Attribute Extensions with NUnit, please make sure to setup NUnit extensions as described in the Using NUnit topic. Visual Studio Team Test does not require any additional steps.
What Are These Extensions?
Unit testing attribute extensions is one of the coolest features of the framework. They came about as we were reviewing automated unit test code for some of our customers and our own unit testing code for the framework. The things that became apparent after reviewing these automated tests were:
There is a lot of duplicated code across unit tests that perform element searches that are identical. Many tests perform actions against the same elements on the page but apply different test logic and test data. For example a test class for doing input validation for a segment of an application will require each unit test to first locate that input element on the page and then set the test data for that unit test method. This usually results in lots of duplicated code across unit tests for the same application.
Test code maintenance is a nightmare for large test beds. Element searches (i.e. Find.Byxx) are usually sprinkled all over the test bed and when a shared element changes, you end up having to go through all these tests, locate the Find.Byxx call and update it by hand. Doing global text Find/Replace searches end up being a very tricky and time consuming task.
To address these issues we can:
Move these searches into their own routines. Given the number of element searches within a test bed, that can get out of hand sometimes and it hinders a bit the legibility of the test code.
Use the FindParam serialization described under FindParams as External XML Data Sources. Although this is our recommended approach, we wanted to introduce an intermediate step that is agile, quick, and easy to understand that customers can step into to facilitate the complete move to external FindParam data sources.
These conclusions lead us to the introduction of unit testing attribute specific extensions to NUnit & Visual Studio Team Test.
Attribute Extensions are test fixture attributes that can be used to decorate unit test methods or classes to describe FindParam objects and dialog handling. These attributes are automatically understood and handled by the framework's automation infrastructure. These test fixture attributes live under the 'ArtOfTest.WebAii.TestAttributes' namespace:
FindParamAttribute: You use this attribute to describe how an element can be found on the page. You set this attribute on test methods or share it across all test methods of a particular test class by setting it on the test class that contains the test methods that will use the FindParam definitions.
DialogAttribute: You use this attribute on test methods to define the Win32 pop-up dialogs you want monitored and define how you want them handled. If you have common dialogs that pop-up across your test methods, you can simply set this attribute on the test class and the dialog will be monitored and handled for all test methods contained in that test class.
Examples
FindParamAttribute
Let's start by taking few examples for FindParamAttribute. Assume we have an input textbox that we commonly access through out all the test methods in our Foo test class. Well, we can share this methods across all the test methods in 'foo' by setting that FindParam using FindParamAttribute on the test class like this:
You can use 1-N FindParam attributes on your test class or test method. If you have elements that are shared across more than one test method, set these FindParam's on the test class, else simply set them on the test method. For example:
FindParamAttributes and TestRegions
FindParamAttributes can also be used with TestRegions when they are defined within an application. In this scenario, the FindParam needs to reflect an element search within a specific TestRegion. To tie a FindParamAttribute with a TestRegion, simply set the TestRegionId property of that FindParam on your test method or test class. Here is an example:
FindParamAttributes can also be used with TestRegions when they are defined within an application. In this scenario, the FindParam needs to reflect an element search within a specific TestRegion. To tie a FindParamAttribute with a TestRegion, simply set the TestRegionId property of that FindParam on your test method or test class. Here is an example:
DialogAttribute
Dialog attribute is used similar to the FindParamAttribute but instead of defining how an element should be found, it defines how a dialog should be handled in addition to the dialog types to handle. Again these attributes can be shared across test methods by setting them on a test class or can be directly set on the test method that will cause the dialog to pop up. Here are few examples of some of the common dialogs:
Note: All examples shown above should work with NUnit 2.4 and higher and Visual Studio Team Test. In the samples above we simply used Visual Studio Team Test.