C# 使用静态变量作为 DeploymentItem 的参数

发布于 2024-08-19 06:42:10 字数 669 浏览 6 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-08-26 06:42:10

属性只能使用常量字符串,所以不行:您不能这样做(您必须使用预组合版本或文字连接 - 而不是 Path.Combine)。您也可以使用测试项目部署设置(testrunco​​nfig?),但坦率地说,我更喜欢使用 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...

紫瑟鸿黎 2024-08-26 06:42:10

这应该有效:

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

This should work:

[TestClass]
[DeploymentItem(TestParams.ConfigFileName)]
public class MyTest
{
    private static class TestParams
    {
        public const string ConfigFileName = "TestConfig.xml";
    }
// ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文