Do DeploymentItem and TestCleanup conflict in unit tests?

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(){ /*test*/ } [TestMethod] [DeploymentItem("UnitTesting\testdata2.xml","mytestdata")] public void Test1(){ /*test*/ } 

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.

+4
source share
2 answers

The order of the attributes is as follows:

  • Methods marked with the AssemblyInitializeAttribute attribute.
  • Methods marked with the ClassInitializeAttribute attribute.
  • Methods marked with the TestInitializeAttribute attribute.
  • Methods marked with the TestMethodAttribute attribute.

Part of the problem is that Visual Studio runs the tests in a non-deterministic order (by default, but this can be changed ) and several times at a time. This means that you cannot delete the folder after each test.


In general, if you can avoid switching to disk for unit tests, it will be much better. In general, you do not want to have anything but code that can break your tests.

+5
source

I had a similar problem. In several tests, I need to remove the expanded item - all tests are transmitted at startup separately, but failed at startup in the playlist. My solution is ugly but simple: use a different folder for each test .

For instance:

  [TestMethod] [DeploymentItem("Resources\\DSC06247.JPG", "D1")] public void TestImageUploadWithRemoval() { // Arrange myDeployedImagePath = Path.Combine(TestContext.DeploymentDirectory, "D1", "DSC06247.JPG"); // Act ... } [TestMethod] [DeploymentItem("Resources\\DSC06247.JPG", "D2")] public void TestImageUploadWithoutRemoval() { // Arrange myDeployedImagePath = Path.Combine(TestContext.DeploymentDirectory, "D2", "DSC06247.JPG"); // Act... } 
+1
source

All Articles