MSTest and Calling Exe with Sample Data Files

For various reasons I’m using MSTest for my unit tests and I have this console app which generates a PDF file from an XML file that needed some tests.

So, when moving the unit tests over to a TFS build server, having hard coded paths to test data files is not a good idea. The recommended way of doing it is to use the DeploymentItem attribute.

Also, when calling an exe-file which does Environment.Exit() and catching the exit code from the unit test is pretty straight forward by using Process.Start() and checking the ExitCode property.

A small sample:

[DeploymentItem(@"TestData\arbetsorder.xml", "TestData")]
[DeploymentItem(@"TestData\arbetsorderReport.rdlc", "TestData")]
public void GeneratorShouldReturnWithExitCode0()
    Process proc;
        proc = Process.Start("PdfGenerator.exe", "\"" + Directory.GetCurrentDirectory() + "/TestData/arbetsorderReport.rdlc\" \"" +
            Directory.GetCurrentDirectory() + "/arbetsorder.pdf\" \"" + Directory.GetCurrentDirectory() + "/TestData/arbetsorder.xml\"");
    catch (System.ComponentModel.Win32Exception ex)
        proc = null;

    Assert.AreEqual(0, proc.ExitCode, "Expected 0, got " + proc.ExitCode);

But, there’s a small caveat to get the DeploymentItem argument working properly! First you have to open up the test settings of your .testsettings file, select the Deployment settings and make sure the “Enable deployment” checkbox is checked:


That’s all you need to do. And note that you may actually have to exit and restart Visual Studio to make it work. I never got the files deployed until I did that restart.

No Comments