1

I want to write tests to check the existance (and other stuff) of certain files that will be shipped with our project.

This is what I have right now:

[DeploymentItem("1.pdf")]
[DeploymentItem("2.pdf")]    
public class DoFilesExist
{
    List<string> _Files;

    public DoFilesExist()
    {
        _Files = new List<string>();
        _Files.Add("1.pdf");
        _Files.Add("2.pdf");
    }

    delegate void fileTest(string fileName);

    void Map(fileTest test)
    {
        foreach (string file in _Files)
        {
            test(file);
        }            
    }

    [TestMethod]
    public void TestExists()
    {
        Map( x => Assert.IsTrue(File.Exists(x), x + " doesn't exist") );
    }
}

As you can see, when I want to add another file to test, I have to add to the [DeploymentItem] and the _Files List

Is there a way to Dynamically Change the DeploymentItems? Or to grab from them during run time. I will probably end up having over 30 files here, and I do not want two lists.

Thanks in advance!

jason
  • 236,483
  • 35
  • 423
  • 525
Jeremiah
  • 5,386
  • 9
  • 38
  • 45

2 Answers2

4

It looks like you're mainly testing whether [DeploymentItem] works... after all - it isn't [DeploymentItem] that defines your actual deployment.

Personally, this is something I despise about MSTest; .NET projects already have a way to define deployment data - the project! By introducing a second method, it introduces both duplication and risk. One of the reasons I use NUnit instead of MSTest, even though I have a VSTS license </rant>.

Re the question; you could use reflection to look at the DeploymentItem markers, but I'm really not sure what this is testing, other than the test framework itself...

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

One possibility is to put your files into a subdirectory and then you can have the deployment item reference that e.g.

[DeploymentItem("files\"]
public class Test
{
}

You might need to mark each file with the CopyAlways as well - see the main question for details.

Paul Hatcher
  • 7,342
  • 1
  • 54
  • 51