Assert.Inconclusive 和 IgnoreAttribute

发布于 2024-10-13 02:22:23 字数 530 浏览 9 评论 0 原文

在 MS Unit 测试框架中使用 Assert.Inconclusive 和 IgnoreAttribute 的正确方法是什么?

我们使用 Assert.Inconclusive 主要用于以下测试:

  • 尚未实现
  • 不知何故损坏或不完整 = 需要进一步关注
  • 当测试主体因任何原因被注释掉时

我们这样做是因为:

  • 不确定的测试可能包含消息
  • 我们希望在 TFS 的测试结果中看到此类测试。

我们的问题是,Inconclusive 测试在 TFS 和 Resharper 中都被标记为错误。如果我们使用 IgnoreAttribute 相反,我们将在 Resharper 中看到这些测试,但 MS Test runner 和 TFS 将完全忽略它们。在 TFS 和 MS Test runner 中使用 IgnoreAttribute 与注释整个测试相同,这是无用的。

What is the right way to use Assert.Inconclusive and IgnoreAttribute in MS Unit test framework?

We are using Assert.Inconclusive mainly for tests which are:

  • Not implemented yet
  • Somehow broken or incomplete = requires futher attention
  • When test body is for any reason commented out

We are doing this because:

  • Inconclusive test can have message
  • We want to see such tests in test results on TFS

Our problem is that Inconclusive tests are marked as error in both TFS and Resharper. If we use IgnoreAttribute instead we will see these tests in Resharper but MS Test runner and TFS will ignore them at all. Using IgnoreAttribute in TFS and MS Test runner is same like commenting whole test which is useless.

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

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

发布评论

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

评论(6

花辞树 2024-10-20 02:22:23

我还看到当前实施中的困境。

  • TRX 报告中包含不确定断言,但 mstest.exe(以及 vstest.console.exe)将返回 1(表示错误)执行后。
  • 具有Ignore属性的测试方法不会被报告为错误,但它们在TRX报告中完全隐藏

我个人的理解如下:

使用[Ignore]属性(暂时)禁用/跳过该方法:

[TestMethod]
[Ignore] // <== disabled through "Ignore" attribute
public void Test001()
{
   //execute some stuff ...
   Assert.IsTrue(...);

   //execute some stuff ...
   Assert.AreEqual(...);
}

不要滥用Inconclusive断言为此:

[TestMethod]
public void Test002()
{
    Assert.Inconclusive(); // <== misuse of "Inconclusive" to skip this test

    //execute some stuff ...
}

相反,应该有条件地使用Inconclusive:只有当我们无法判断要测试的组件是否按预期工作时。
例如,如果我们依赖的外部资源在测试执行时不可用:

[TestMethod]
public void Test003()
{
    //check if the server is running,
    //otherwise can can't test our local client component!
    if (!WebServiceAvailable())
    {
        Assert.Inconclusive(); // <== skip remaining code because the resource is not available
    }

    //execute some stuff ...
    Assert.AreEqual(...);

    //execute some stuff ...
    Assert.AreEqual(...);
}

_ _

结论:
禁用/跳过测试的逻辑方法是使用 [Ignore] 属性。
我清楚地看到 mstest.exe 当前的行为没有将任何被忽略的测试报告为应该修复的bug

请随意对以下错误报告进行投票:

i also see a dilemma in the current implementation.

  • Inconclusive assertions are included in the TRX report, but mstest.exe (and also vstest.console.exe) will return 1 (meaning error) after execution.
  • TestMethods with the Ignore attribute will not be reported as an error, but they are completely hidden from the TRX report.

my personal understanding is as follows:

use the [Ignore] attribute to (temporarily) disable / skip the method:

[TestMethod]
[Ignore] // <== disabled through "Ignore" attribute
public void Test001()
{
   //execute some stuff ...
   Assert.IsTrue(...);

   //execute some stuff ...
   Assert.AreEqual(...);
}

do not misuse the Inconclusive assertion for this purpose:

[TestMethod]
public void Test002()
{
    Assert.Inconclusive(); // <== misuse of "Inconclusive" to skip this test

    //execute some stuff ...
}

instead, Inconclusive should be used conditionally: only if we can't tell whether the component to be tested does work as expected or not.
for example in case an external resource we depend on is not available at time of test execution:

[TestMethod]
public void Test003()
{
    //check if the server is running,
    //otherwise can can't test our local client component!
    if (!WebServiceAvailable())
    {
        Assert.Inconclusive(); // <== skip remaining code because the resource is not available
    }

    //execute some stuff ...
    Assert.AreEqual(...);

    //execute some stuff ...
    Assert.AreEqual(...);
}

_ _

conclusion:
to disable / skip a test the logical way is to use the [Ignore] attribute.
i clearly see the current behaviour of mstest.exe not reporting on any ignored test as a bug that should be fixed.

feel free to up-vote the following bug reports:

零度° 2024-10-20 02:22:23

我喜欢认为您描述的 Inconclusive 是正确的用法。

尽管根据我的经验,“不确定”更像是警告而不是错误。事实上,它们在 TRX 中与错误分开报告:

<TestRun>
   <ResultSummary outcome="Inconclusive">
      <Counters total="1" 
          executed="0" error="0" failed="0" 
          timeout="0" aborted="0" inconclusive="1" 
          passedButRunAborted="0" notRunnable="0" 
          notExecuted="0" disconnected="0" 
          warning="0" passed="0" completed="0" 
          inProgress="0" pending="0" />

我通常从 运行 mstest 可执行文件。我的 msbuild 脚本中的任务,然后查看 TRX 内部以确定它是否应该使构建失败。

I like to think how you are describing Inconclusive is the proper usage.

Though in my experience, Inconclusive is treated more like a warning than an error. In fact, they are reported in the TRX separately from errors:

<TestRun>
   <ResultSummary outcome="Inconclusive">
      <Counters total="1" 
          executed="0" error="0" failed="0" 
          timeout="0" aborted="0" inconclusive="1" 
          passedButRunAborted="0" notRunnable="0" 
          notExecuted="0" disconnected="0" 
          warning="0" passed="0" completed="0" 
          inProgress="0" pending="0" />

I typically run the mstest executable from an <Exec> task in my msbuild script and then peek inside the TRX to determine whether it should fail the build.

五里雾 2024-10-20 02:22:23

传统上,在测试时,单元测试应该非常具体,以便功能和这些功能的测试之间存在(接近)一一对应的关系。

为了测试某些功能,首先需要将被测系统 (SUT) 置于某种状态。一旦达到该状态,测试就可以实际测试其预期的功能。现在,如果设置部分已经失败,那么这样的测试的状态应该是什么。它无法对被测功能的功能做出声明,因为测试从未达到执行该功能所需的状态。

在这种情况下,通常会给出不确定的结果,因为我们根本不知道该功能是否按预期工作,因此我们无法通过或失败。事实上,设置本身没有按预期工作应该通过单独的测试来解决。

所以想象一下,我想测试一个已经被“bar”ed 的“foo”在“qux”ed 时将返回 0。此测试不应测试“foo”是否可以被“禁止”,因此任何“禁止”失败都将返回不确定的结果,而未能响应“qux”将是正确的失败。

Classically, in when testing, unit tests should be very specific so that there is a (close to) one-to-one correspondence between features and tests for these features.

To test certain features, the system under test (SUT) first needs to be brought into a certain state. Once that state is reached, the test can actually test the feature that it is meant for. Now, what should be the status of such a test, if already the setup part fails. It cannot make a statement on the functioning of the feature under test, as the test never reached the status requires to exercise the feature.

In such cases, it is common to assign an inconclusive result, as we simply cannot know if the feature works as it should, and so we cannot pass or fail. The fact that the setup itself does not work as expected should be covered by a separate test.

So imagine, I want to test that a 'foo' that has been 'bar'ed will return a 0 when 'qux'ed. This test should not test if 'foo' can be 'bar'ed, so any failure to be 'bar'ed will return an inconclusive, whereas a failure to respond to 'qux' will be proper fail.

胡大本事 2024-10-20 02:22:23

我一直在对此进行一些研究,并在家中进行尝试。最终结果是,我相信 MSTest 的 [Ignore] 属性确实完全忽略了测试方法。我尝试查看 Visual Studio 中的设置以查看是否有覆盖,但找不到任何内容。

对此感到羞耻,因为看不到被忽略的测试是很糟糕的,因为您最终可能会认为自己有一套运行良好的 100 个 MSTest 测试,但事实证明,结果中缺少 50 个您从未知道的测试,因为[Ignore] 属性!呃。

I've been doing some research into this, as well as trying it out at home. The end result is that I believe the [Ignore] attribute for MSTest does indeed leave out the test method completely. I tried looking at settings in Visual Studio to see if there was an override, but could not find anything.

Shame about that, as not seeing the ignored tests is bad since you may end up thinking you have a suite of 100 MSTest tests running nicely, but it turns out that there are 50 which are missing from the results that you never knew about due to the [Ignore] attribute! Urgh.

拥抱没勇气 2024-10-20 02:22:23

根据 MSDN 文档:

  1. IgnoreAttribute
    (自 VS 2005 起)意味着“不应运行此测试
    请参阅 https: //msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.ignoreattribute(v=vs.80).aspx (存档版本在 WaybackMachine)。

  2. Assert.Inconclusive(自 VS 2005 起)意味着“表示无法证明某个断言是真是假。也用于表示尚未实现的断言。”请参阅 https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.inconclusive(v=vs.80).aspx (在 WaybackMachine 上存档)


我认为当您必须使用哪一个时,这些是非常明确的声明。

As of the MSDN docs:

  1. IgnoreAttribute
    (since VS 2005) means "this test should not be run"
    see https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.ignoreattribute(v=vs.80).aspx (Archive version on WaybackMachine).

  2. Assert.Inconclusive (since VS 2005) means "Indicates that an assertion cannot be proven true or false. Also used to indicate an assertion that has not yet been implemented." see https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.inconclusive(v=vs.80).aspx (Archive on WaybackMachine)

I think these are realy clear statements when you have to use which one.

梦途 2024-10-20 02:22:23

我也遇到了同样的问题,VS 将不确定的测试标记为失败。我发现 .runsettings 中有一个设置,您可以设置不将它们标记为失败。只需将设置 MapInconclusiveToFailed 设置为 False。这是示例用法的链接。

https://learn.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file ?branch=release-16.4&view=vs-2019#mstest-element

I was having the same issue with VS marking inconclusive tests as failed. I found there's a setting in .runsettings you can set to not mark them as failed. Just set the setting MapInconclusiveToFailed to False. Here's the link of the sample usage.

https://learn.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?branch=release-16.4&view=vs-2019#mstest-element

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文