Data Driven Testing - ProtoTest/ProtoTest.Golem GitHub Wiki
MbUnit contains a number of data driven testing attributes. This allows us to repeat any [Test] for a wide range of data permutations. When using these attributes, remember each test will be repeated in its entirety, so make sure to return to the original state at the end of each test.
Code examples of data driven tests are in the /Tests/TestDDT.cs file.
####Rows If I want to repeat a test multiple times, and the data can be hard-coded into my class, I can use [Row(param1, param2, etc] attribute. The Row() method can include as many parameters as I want, and each is passed into the test method through a parameter.
[Row("Selenium","Selenium - Web Browser Automation")]
[Row("ProtoTest", "ProtoTest « Beyond Bugs")]
[Row("WebDriver", "Selenium - Web Browser Automation")]
[Test]
public void TestRowData(string term, string result)
{
OpenPage<GoogleHomePage>("http://www.google.com/").SearchFor(term).VerifyResult(result);
}
####Csv However if the test data set is very large, or if it needs to be maintained outside of the code we need to use a Csv or Xml file. The [CsvData] works the same as a Row, except that we pass in the location of the file. The test method must include a parameter for each column, and the test is automatically repeated once for each row in the file. Please note the file path is relative, this means that my .csv file must be copied to the output directory on build. Edit the file's properties and mark it to copy to output directory.
[CsvData(FilePath = ".\\Tests\\Data\\Data.csv", HasHeader = true)]
[Test]
public void TestCSV(string term, string result)
{
OpenPage<GoogleHomePage>("http://www.google.com/").SearchFor(term).VerifyResult(result);
}
####Xml
The XmlData attribute works very similarly to the CsvData. Our test method contains parameters, just as before. However because an Xml Document can have any format we want, we need to tell it what nodes to repeat on. We do this using an xpath expression. The tests will automatically repeat once for each item that matches the xpath. In addition, we have to tell it what parts of the Xml document get passed into each parameter. We do this using the @Bind attribute.
Using this Xml File :
<Searches>
<Search term="Selenium" result="Selenium - Web Browser Automation"/>
<Search term="prototest" result="ProtoTest « Beyond Bugs"/>
</Searches>
This test will repeat twice, once per Search node.
[XmlData("//Search",FilePath = ".\\Tests\\Data\\SearchData.xml")]
[Test]
public void TestXml([Bind("@term")]string search, [Bind("@result")]string result)
{
OpenPage<GoogleHomePage>("http://www.google.com/").SearchFor(search).VerifyResult(result);
}
####Factory The last option for DDT is to use a [Factory]. A Factory is another method, that returns a list of items. This list could be anything we want, including a complex object. If a factory is used on a test method, the test will be repeated once per item in the list. If a Factory is used on a class property the entire class will be repeated once per item.
In this example I define a class called Search. I use this just to store data.
public class Search
{
public string term;
public string result;
}
Now I define my test method, and tell it that i'm using a method called GetDataList(). The GetDataList method returns a list of Search classes, passed into the parameter, and the test is repeated once each.
[Test,Factory("GetDataList")]
public void TestFactory(Search search)
{
OpenPage<GoogleHomePage>("http://www.google.com/").SearchFor(search.term).VerifyResult(search.result);
}
Here is my Factory method that gets called. It returns a list of two Search classes.
public List<Search> GetDataList()
{
List<Search> searches = new List<Search>();
searches.Add(new Search() { term = "WebDriver", result = "Selenium WebDriver" });
searches.Add(new Search() { term = "Appium", result = "Appium: Mobile App Automation Made Awesome." });
return searches;
}