I had been stopped writing blog for the past 3 months, it is really tough for me to keep up the momentum in writing blog in a timely manner. Well, until recently I join a new company and I suppose I will encounter some unfamiliar issue and new experience to share here more oftenly.
Back to the topic, I would like to share how to write data driven unit test with XML.
Do you ever encounter a scenario that you wish to test one test case with multiple different set of test data?
. But, today I want to share about doing the same thing with Visual Studio unit test.
What's does Microsoft.VisualStudio.TestTools.DataSource.XML do at the background is above XML will be parsed into a table, hence
<TimeOffTest_ApplyTimeOffTest> is a table name then its child elements are columns name then the element content are the row data. Therefore above XML describe 4 set of test data and the differences of the test data.
When you perform test run, it will perform 4 runs with different set of test data accordingly to the defined XML above.
Here is how the test result look like:
Common Problem
After you have finished coding, and you could not get the same result like above screenshot. Following are the common issues that you may encounter and here is the solution:
Known Issue: The unit test adapter failed to connect to the data source or read the data.
Resolution:
Go to your Solution Explorer, add new item to your solution, then select "Test Settings" from the left menu, then add a new test setting.
In the "Test Settings" window, select "Deployment" from the left menu, then check the "Enable Deployment" checkbox.
If problem still persist, go to Solution Explorer, locate your test data XML file and then open the file properties. Make sure the "Copy to Output Directory" property is NOT set to "Do not copy", make it either "Copy always" or "Copy if newer".
If problem still persist and you are using Visual Studio 2012, move your XML file to your project root directory. The problem is the |Data Directory| is not returning correct path. I notice some odd behavior between Visual Studio 2010 and 2012.
I have a folder call "TestData" that keep all my XML files in my test project well organized.
With Visual Studio 2010, no issue in detecting my XML file in the folder.
With Visual Studio 2012, I will get the error. However, there is a workaround for it, which is not to use |Data Directory| when specifying your test data file location. Instead, use real physical path. For example:
[DeploymentItem("\\TestData\\TimeOffTestData.xml"), TestMethod()]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"D:\\Projects\\DataDrivenUnitTest\\DataDrivenUnitTest.Test\\TestData\\TimeOffTestData.xml",
"TimeOffTest_ApplyTimeOffTest", DataAccessMethod.Sequential)]
public void ApplyTimeOffTest()
There is one more workaround for solving the problem with |DataDirectory| in Visual Studio 2012. You can set Copy to Output Directory value to "Do not copy" for the XML file. Then, open the unit test project properties, then set the post-build event command line: copy "$(ProjectDir)TestData" "$(ProjectDir)$(OutDir)"
So that, every time you build your unit test project, the command line above copy all the files in the TestData folder to your output directory without the TestData folder automatically.
You may download my unit test project to play around:
Download Here