xUnit Equivelant of MSTest's Assert.Inconclusive

元气小坏坏 提交于 2019-12-21 03:14:12

问题


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.


回答1:


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:




回答2:


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




回答3:


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 above, but that sounds like a great solution to me.



来源:https://stackoverflow.com/questions/34610133/xunit-equivelant-of-mstests-assert-inconclusive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!