Microsoft MSTest supports data driven tests where you can specify a data source and the test method will be executed for each row in the set , either in Sequential or Random mode based on your choice and need.
Since CodedUI is based on MSTest, we can build data driven UI tests as well. This enables interesting options to specify all data entry in a data source and not directly in your test method. This also enables functional testers to specify the test scenarios
Now, Different data sources can be used to drive the UITests. Some of them are
Comma Separated files (CSV)
- XML
- Excel (xls, xlsx)
- Test Case from Microsoft Test Manager (MTM)
- SQL Server
You can easily specify the data source as part of the test method declaration. Refer previous post for more clarity.
The Data Source attribute defines the data connection you want to use
Now, in your test method you can access the data row via the TestContext
For CSV,
The |DataDirectory| will resolve to the correct location at test as long as you deploy the csv file with your test. Data#csv is the table name you have to use with CSV
For SQL,
For XML,
FOr Excel,
If you have the actual connection available, so you can use ADO.NET to access additional sheets of data. E.g. let first sheet be the index into subsequent data sets or Query on named ranges
Deploying the required resources
Now, the question is how can you deploy your excel sheet with your tests? or how can you deploy assets required during the test run in general?
The answer lies within MSTest itself. MSTest has the option to specify deployment items. For this, you need to do the below things
- Need to Set deploy to true in test settings file
- Annotate the test class or method with DeploymentItem attribute
Add the assets to your build deployment
- Add a folder named assets to your solution
- Add files you need like excel sheets or pictures and set them to copy to output
Refer the DeploymentItem attribute to the assets folder
- Use test context Deployment location to refer to the deployed item you need