开发者

C# using static variable as parameter to DeploymentItem

开发者 https://www.devze.com 2022-12-17 21:08 出处:网络
I want to use a static variable as the parameter to DeploymentItem on an MSTest unit test but it doesn\'t seem I\'m able to do so.There\'s an XSL file that needs to be copied along with the DLL file开

I want to use a static variable as the parameter to DeploymentItem on an MSTest unit test but it doesn't seem I'm able to do so. There's an XSL file that needs to be copied along with the DLL file开发者_开发技巧 when the unit test runs, and I defined the location as

private static string _xslPath = Path.Combine("MyProjectDir", "transform.xsl");

However, when I then do the following:

[TestMethod]
[DeploymentItem(DLL)]
[DeploymentItem(_xslPath)]
public void XmlToResultsTest() { }

I get this build error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Okay okay, fine, but it just seems so dirty to assemble the path myself:

[DeploymentItem(@"MyProjectDir\transform.xsl")]

Am I being overly picky here about wanting to use Path.Combine? Is there another alternative I'm missing? I suppose I could just put the XSL file in the root solution directory so I don't have to pass in the project directory as part of the path.


Attributes can only use constant strings, so no: you can't do this (you would have to use the pre-combined version, or literal concatenation - not Path.Combine). You could use the test-project deployment settings too (testrunconfig?), but frankly I prefer to use the NUnit approach of just marking the file (in the csproj, like normal) for deployment. I have yet to figure out why MS added a separate way of defining this...


This should work:

[TestClass]
[DeploymentItem(TestParams.ConfigFileName)]
public class MyTest
{
    private static class TestParams
    {
        public const string ConfigFileName = "TestConfig.xml";
    }
// ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消