59

What is the xUnit equivalent of the following MSTest code:

Assert.Inconclusive("Reason");

This gives a yellow test result instead of the usual green or red. I want to assert that the test could not be run due to certain conditions and that the test should be re-run after those conditions have been met.

Pang
  • 9,564
  • 146
  • 81
  • 122
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

3 Answers3

44

Best thing one can do until something is implemented in the library is to use Xunit.SkippableFact:

[SkippableFact]
public void SomeTest()
{
    var canRunTest = CheckSomething();
    Skip.IfNot(canRunTest);

    // Normal test code
}

This will at least make it show up as a yellow ignored test case in the list.

Credit goes to https://stackoverflow.com/a/35871507/537842

Pang
  • 9,564
  • 146
  • 81
  • 122
Anttu
  • 1,076
  • 1
  • 10
  • 21
  • Not available in .NET Core 2, .NET Standard 2, .NET Framework 4.7 – Kody Jan 08 '18 at 20:39
  • Could you give a reference on that because I'm using it in a xUnit project targeting `netcoreapp2.0`. – Anttu Jan 25 '18 at 09:03
  • Check your dependencies, it is not compiling for .NET Standard 2. It is compiling with .NET Framework 4.5. – Kody Jan 30 '18 at 00:05
  • That's what I did. And according to the NuGet gallery it's supported even down to .NET Standard 1.1. See dependencies at https://www.nuget.org/packages/Xunit.SkippableFact/ – Anttu Jan 30 '18 at 18:36
  • It is available in .NET Core 2 now. – Sergei G Jul 25 '18 at 19:47
19

One way is to use the Skip parameter within the Fact or Theory attributes.

[Fact(Skip = "It's not ready yet")]
public void ReplaceTokensUnfinished()
{
    var original = "";
    var expected = "";
    var tokenReplacer = new TokenReplacer();
    var result = tokenReplacer.ReplaceTokens(original, _tokens); // (_tokens is initialised in a constructor)
    Assert.Equal(result, expected);
}

Which gives this result when run:

enter image description here

Pang
  • 9,564
  • 146
  • 81
  • 122
testpattern
  • 2,382
  • 1
  • 25
  • 29
  • 24
    I don't like this. Wouldn't this mean any exception or failure would mean the test skipped? I want the ability to only be inconclusive under certain circumstances and fail otherwise... – Kody May 20 '16 at 20:42
  • 1
    That's correct, it skips the test. Sounds like it can't quite do what you need. xUnit is open source though, so you can add your functionality to it ;) – testpattern May 23 '16 at 15:24
  • 2
    Additionally, adding Skip to Theory skips all tests (for all "theories") rather than just the single run. – Kody May 23 '16 at 16:26
  • 15
    this is not the same. `Assert.Inconclusive()` is not only used when the test is not implemented yet, but also when there is an error in the Arrange section of the test. As of now, there is no replacement for this. See also [xUnit Issue #1070](https://github.com/xunit/xunit/issues/1070) – MovGP0 May 02 '17 at 20:02
-4

I normally do something like this:

throw new Exception("Inconclusive");

Yes, it shows as a failed test, but at least you can raise this in the test under certain inconclusive cases.

I've not used the SkippableFact feature mentioned in the other answer, but that sounds like a great solution to me.

Pang
  • 9,564
  • 146
  • 81
  • 122
SirClutzz
  • 95
  • 2
  • 4