Search code examples
c#mstestdeploymentitem

How can I get the name of the DeploymentItem?


I would like to get rid of some duplication in this code. Following the DRY principle.
As you can see, the name of the file/deploymentItem is repeated.

[TestMethod]
[DeploymentItem("TestData/TestExcel.xlsx")]      <-- 
public void GivenAnExcel_ConverToPDF()
{
    const string filename = "TestData/TestExcel.xlsx";     <-- 
    var result = pdfConverter.ConvertExcelDocument(filename);
    AssertIsPdf(result);
}
  • Is there a way to access the DeploymentItem programmatically without using the filename?
    Or
  • Can I get filename programmatically somehow?

No, I cannot use another test framework than mstest ;-)


Solution

  • You could simply do this:

    [TestClass]
    public class Test
    {
        const string filename = "TestData/TestExcel.xlsx";
    
        [TestMethod]
        [DeploymentItem(filename)] 
        public void GivenAnExcel_ConverToPDF()
        {
            var result = pdfConverter.ConvertExcelDocument(filename);
            AssertIsPdf(result);
        }
    }