C# 使用静态变量作为 DeploymentItem 的参数
我想在 MSTest 单元测试中使用静态变量作为 DeploymentItem
的参数,但我似乎无法这样做。当单元测试运行时,需要将一个 XSL 文件与 DLL 文件一起复制,我将位置定义为
private static string _xslPath = Path.Combine("MyProjectDir", "transform.xsl");
但是,当我执行以下操作时:
[TestMethod]
[DeploymentItem(DLL)]
[DeploymentItem(_xslPath)]
public void XmlToResultsTest() { }
我收到此构建错误:
属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式
好吧好吧,很好,但是我自己组装路径看起来很脏:
[DeploymentItem(@"MyProjectDir\transform.xsl")]
我在这里对于想要使用 是否过于挑剔了路径.组合
?我还缺少其他选择吗?我想我可以将 XSL 文件放在根解决方案目录中,这样我就不必将项目目录作为路径的一部分传递。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性只能使用常量字符串,所以不行:您不能这样做(您必须使用预组合版本或文字连接 - 而不是
Path.Combine
)。您也可以使用测试项目部署设置(testrunconfig?),但坦率地说,我更喜欢使用 NUnit 方法,仅标记文件(在 csproj 中,像平常一样)进行部署。我还没有弄清楚为什么微软添加了一种单独的方式来定义这个......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: