When writing Unit tests, it is sometimes useful to use specific files for a specific Unit test. With help of the TestContext object in a Unit test class you can easily deploy files along with your Unit Tests.
Everytime a Unit test is executed, the file(s) is published to an output Directory. This file can then be used for your Unit test.
1: [TestMethod]
2: [DeploymentItem("SQLScripts\\Fill_DB_WithTestData.sql", "Fill")]
3: public void Select_Data_Returns_DataOrderedByDateDescending()
4: {
5: string fillFile = Path.Combine(TestContext.TestDeploymentDir, "Fill\Fill_DB_WithTestData.sql");
6: ExecuteFillScript(fillFile);
7: .........
8:
9: }
The files that you want to deploy must be set to “Copy Always” in the properties window.
Then it should work.
But!!!! In VS 2010 a setting is disabled where it was enabled in VS 2008. Deployment of files is now default switched off!
In order to switch it on go to Menu | Test | Edit Test Settings and select the active one (Test | Select Active Test Settings)
Click Deployment and Check the box!
Happy Testing !
Comments are closed.