49

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?

Shaun Wilson
  • 8,727
  • 3
  • 50
  • 48
DrunkCoder
  • 8,215
  • 3
  • 19
  • 14
  • 1
    My utterly instinctive response is to ask _why_? =) And you can always do an `Assert.Pass();` or `Assert.Fail();`. But otherwise... tests are there to be run. =) – J. Steen Jul 25 '12 at 12:58
  • Why skip the test? Shouldn't you either return Assert.Fail() or Assert.Inconclusive()? – glosrob Jul 25 '12 at 12:59
  • 8
    I'd suggest `Assert.Inconclusive` over `Assert.Fail`, since it's not an explicit failure case. `Inconclusive` makes it clearer that you *don't know* whether the test succeeded or failed. – Dan Puzey Jul 25 '12 at 13:02
  • 1
    `Assert.Inconclusive` is thing I`m looking for. Thanks. – DrunkCoder Jul 25 '12 at 13:09

4 Answers4

107

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

  • mark test as ignored via [Ignore] attribute
  • throw NotImplementedException from your test
  • write Assert.Fail() (otherwise you can forget to complete this test)
  • remove this test

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
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
63

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

Using Inconclusive as 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!

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • 5
    Many years later and MSTest -> TFS finally recognizes Assert.Inconclusive as "Other" instead of "Fail". See https://github.com/Microsoft/vstest/issues/525 for details if this is as important to you as it is to me :-). – Zephan Schroeder Dec 17 '18 at 05:16
19

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

}
kmandew
  • 207
  • 1
  • 3
2

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.

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
Roman Gusiev
  • 382
  • 3
  • 12