I have an application that has many unit tests in many classes. Many of the tests have DeploymentItem attributes to provide the required test data:
[TestMethod] [DeploymentItem("UnitTesting\testdata1.xml","mytestdata")] public void Test1(){ } [TestMethod] [DeploymentItem("UnitTesting\testdata2.xml","mytestdata")] public void Test1(){ }
When tests are run individually, they pass. When everything starts immediately (for example, when I select Run All Tests In Current Context), some tests fail because the DeploymentItem left after other tests cause the tests to capture incorrect data. (Or, the test incorrectly uses files intended for another test that is not already running.)
I found the attributes [TestCleanup] and [ClassCleanup] that seem to help. I added:
[TestCleanup] public void CleanUp(){ if(Directory.Exists("mytestdata")) Directory.Delete("mytestdata", true); }
The problem is that this runs after each test method, and it seems that it will remove DeploymentItems for tests that are not already running. [ClassCleanup] prevent this, but unfortunately, it will not work often enough to prevent the original problem.
From the MSDN documentation, it seems that DeploymentItem guarantees that the files will be there until the test is completed, but this is not more specific. I think I see the following problem:
- Deployment item for testing runs
- (do other things happen?)
- Test cleanup from previous test in progress
- The next test is performed.
- The test failed because the files were missing.
Does anyone know the order in which the various attributes of the test are executed? I searched, but did not find much.
I thought that each deployment item uses its own unique folder for data, but this becomes difficult as there are hundreds of tests to go through.
source share