使 MSTest 尊重 [Conditional()] 属性?

发布于 2024-09-06 23:27:49 字数 663 浏览 3 评论 0原文

我正在使用 VS2010,我有以下方法调用:

[Conditional("DEBUG")]
public void VerboseLogging() { }

public void DoSomething() {
    VerboseLogging();
    Foo();
    Bar();
}

然后我对 DoSomething 方法进行单元测试,检查它是否发出正确的日志记录。

[Conditional("DEBUG"), TestMethod()]
public void EnsureVerboseLog() {
    DoSomething();
    VerifyVerboseLoggingCalled(); // <-- fail in release builds since VerboseLogging() calls get eliminated.
}

看来 MSTest 只看到 TestMethod 并执行它(生成失败的测试),即使我已经用 Conditional("DEBUG") 标记它并在发布模式下编译它。

那么,有没有办法根据 #if 之外的编译常量排除某些测试?

I'm using VS2010, I have the following method call:

[Conditional("DEBUG")]
public void VerboseLogging() { }

public void DoSomething() {
    VerboseLogging();
    Foo();
    Bar();
}

Then I have a unit test for the DoSomething method which checks that it emits proper logging.

[Conditional("DEBUG"), TestMethod()]
public void EnsureVerboseLog() {
    DoSomething();
    VerifyVerboseLoggingCalled(); // <-- fail in release builds since VerboseLogging() calls get eliminated.
}

It seems that MSTest only sees TestMethod and executes it (generating failed test) even though I've marked it with Conditional("DEBUG") and compile it in release mode.

So, is there a way to exclude certain tests depending on compilation constant other than #if?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

两人的回忆 2024-09-13 23:27:49

那么,有没有办法根据编译排除某些测试
常数除了#if

我会使用#if - 它是可读的。

[TestMethod]
#if !DEBUG
[Ignore]
#endif
public void AnyTest()
{
    // Will invoke for developer and not in test-server!
}

哈..

So, is there a way to exclude certain tests depending on compilation
constant other than #if?

I'd use the #if - it's readable.

[TestMethod]
#if !DEBUG
[Ignore]
#endif
public void AnyTest()
{
    // Will invoke for developer and not in test-server!
}

HTH..

小清晰的声音 2024-09-13 23:27:49

ConditionalAttribute 不影响方法是否编译到应用程序中。它控制是否将对方法的调用编译到应用程序中。

此示例中没有调用 EnsureVerboseLog。 MSTest 只是在集合中看到带有 TestMethod 属性的方法并正确执行它。为了防止 MSTest 运行该方法,您需要执行以下操作之一

  1. 不将其编译到您的应用程序中(可以通过 #if 进行)
  2. 不使用 TestMethod 属性对其进行注释

The ConditionalAttribute does not affect whether or not a method is compiled into an application. It controls whether or not calls to the method are compiled into the application.

There is no call to EnsureVerboseLog in this sample. MSTest just sees a method in the assemlby with the TestMethod attribute and correctly executes it. In order to prevent MSTest from running the method you will need to do one of the following

  1. Not compile it into your application (possible through #if's)
  2. Not annotate it with the TestMethod attribute
香草可樂 2024-09-13 23:27:49

如果您希望能够有条件地运行不同的测试,您可能需要使用[TestCategory] 属性:

[TestMethod]
[TestCategory("SomeCategory")]
public void SomethingWorks()
{
    ...
}

然后使用 filter 参数 以包含或排除类别:

dotnet test --filter "TestCategory=SomeCategory"
dotnet test --filter "TestCategory!=SomeCategory"

If you want to be able to conditionally run different tests, you will probably want to use the [TestCategory] attribute:

[TestMethod]
[TestCategory("SomeCategory")]
public void SomethingWorks()
{
    ...
}

And then use the filter parameter to include or exclude the categories:

dotnet test --filter "TestCategory=SomeCategory"
dotnet test --filter "TestCategory!=SomeCategory"
可爱咩 2024-09-13 23:27:49

解决方法是将方法的 Priority 属性设置为 -1。然后以“minpriority:0”作为参数运行 mstest

[TestMethod()]
[Priority(-1)]
public void Compute_Foo()
{
    This method will not be executed
}

A work around is to set the Priority attribute to -1 to your method. Then run mstest with "minpriority:0" as an argument.

[TestMethod()]
[Priority(-1)]
public void Compute_Foo()
{
    This method will not be executed
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文