C# 微软单元测试。是否可以从测试方法主体跳过测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11650228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:40:32  来源:igfitidea点击:

Microsoft unit testing. Is it possible to skip test from test method body?

c#unit-testingmstest

提问by DrunkCoder

So I have situation when I need skip current test from test method body. Simplest way is to write something like this in test method.

因此,当我需要从测试方法主体跳过当前测试时,我遇到了情况。最简单的方法是在测试方法中写这样的东西。

if (something) return;

But I have a lot complicated tests and I need a way to skip test from methods which I invoke in current test method body. Is it possible?

但是我有很多复杂的测试,我需要一种方法来跳过我在当前测试方法主体中调用的方法中的测试。是否可以?

采纳答案by Dan Puzey

Further to other answers (and as suggested): I'd suggest using Assert.Inconclusiveover Assert.Fail, since the original poster's situation is not an explicit failure case.

除了其他答案(并按照建议):我建议使用Assert.Inconclusiveover Assert.Fail,因为原始海报的情况不是明确的失败案例。

Using Inconclusiveas a result makes it clear that you don't know whether the test succeeded or failed - which is an important distinction. Not proving success doesn't always constitute failure!

使用Inconclusiveas 结果清楚地表明您不知道测试是成功还是失败 - 这是一个重要的区别。不证明成功并不总是意味着失败!

回答by Sergey Berezovskiy

You should not skip test this way. Better do one of following things:

您不应该以这种方式跳过测试。最好做以下事情之一:

  • mark test as ignored via [Ignore]attribute
  • throw NotImplementedExceptionfrom your test
  • write Assert.Fail()(otherwise you can forget to complete this test)
  • remove this test
  • 通过[Ignore]属性将测试标记为忽略
  • NotImplementedException从你的测试中抛出
  • Assert.Fail()(否则你可能会忘记完成这个测试)
  • 删除此测试

Also keep in mind, that your tests should not contain conditional logic. Instead you should create two tests - separate test for each code path (with name, which describes what conditions you are testing). So, instead of writing:

还要记住,您的测试不应包含条件逻辑。相反,您应该创建两个测试 - 每个代码路径的单独测试(名称,描述您正在测试的条件)。所以,而不是写:

[TestMethod]
public void TestFooBar()
{
   // Assert foo
   if (!bar)
      return;
   // Assert bar
}

Write two tests:

写两个测试:

[TestMethod]
public void TestFoo()
{
   // set bar == false
   // Assert foo
}

[Ignore] // you can ignore this test
[TestMethod]
public void TestBar()
{
   // set bar == true
   // Assert bar
}

回答by kmandew

You can ignore a test and leave it completely untouched in the code.

您可以忽略测试并在代码中完全不改动它。

[TestMethod()]
[Ignore()]    //ignores the test below
public void SomeTestCodeTest()
{
   //test code here

}

回答by Roman Gusiev

There is an Assert.Inconclusive()method which you can use to nicely skip the current test. Check the docs. Basically it will throw an exception and will show this method as Skipped.

有一种Assert.Inconclusive()方法可以用来很好地跳过当前的测试。检查文档。基本上它会抛出异常并将此方法显示为Skipped.

I used this as a programmatic check in my code inside the test method:

我在测试方法中的代码中使用它作为程序检查:

if (!IsEnvironmentConfigured())
{
   Assert.Inconclusive("Some message");
}

Here is the result:

结果如下:

! ShouldTestThisMethod [31ms]

Test Run Successful.
Total tests: 92
     Passed: 91
    Skipped: 1
 Total time: 1.2518 Seconds