How do I remove stale DeploymentItems in unit tests?
consider the following test class:
[TestClass]
public class ExampleTests
{
[TestMethod]
public void FileDoesNotExists()
{
Assert.IsFalse(System.IO.File.Exists("testfile.txt"));
}
[TestMethod]
[DeploymentItem("testfile.txt")]
public void FileExists()
{
Assert.IsTrue(System.IO.File.Exists("testfile.txt"));
}
}
Depending on the开发者_JS百科 order in which these tests are run they might pass but since there is no guarantee that they will run in the order that they are defined the real world behavior is non-deterministic... obviously this is bad.
I have looked around and experimented with the TestCleanupAttribute but haven't been able to find a reliable way to get both tests to pass but it seems like there should be an easy method to do this. Does such a method exist?
update: This appears to be impossible because the test framework assumes that DeploymentItems do not change once deployed and therefore only deploys each item once. In the short term I have changed the code under test to no longer require this behavior, longer term I'll most likely implement an attribute that doesn't make this assumption.
DeploymentItem
s are actually the files that you would normally deploy to the client's computer, so you're not meant to remove them after you deploy them!
You should create a 'Sandbox' directory for your unit tests and use the normal .NET filesystem methods like File.Delete
to do your file copy/access/delete stuff.
精彩评论