4

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 ;-)

chaliasos
  • 9,659
  • 7
  • 50
  • 87
Peter Gfader
  • 7,673
  • 8
  • 55
  • 56
  • 1
    Thank you for this question @Peter! I had the same wish (getting rid of these duplications) but I was sure there is not any way :-) A constant in an attribute... *Thanks* @Schaliasos! – Elena Nov 14 '12 at 07:11

1 Answers1

4

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);
    }
}
chaliasos
  • 9,659
  • 7
  • 50
  • 87