Cannot find Assert.Fail and Assert.Pass or equivalent

后端 未结 4 986
太阳男子
太阳男子 2021-02-04 23:05

I used to use these in NUnit and they are really useful. Any idea how to do something like that?

EDIT, CODE SAMPLE:

        bool condition = false;//woul         


        
相关标签:
4条回答
  • 2021-02-04 23:28

    The documentation includes a comparison chart including this:

    Fail - xUnit.net alternative: Assert.True(false, "message")

    (It doesn't show Assert.Pass, and I've never used that myself, but I suspect the alternative is just to return from the test. Of course that doesn't help if you want to throw it in a nested method call. My suspicion is that it's not very frequently used in NUnit, hence its absence in the comparison chart.)

    0 讨论(0)
  • 2021-02-04 23:30

    Just throw an exception to satisfy both requirements (exiting a nested loop and an alternative to the missing Assert.Fail method). Only issue is there is no decent base exception (e.g. TestException) to use in order to avoid getting warnings about using the base Exception class, so something more directed like an InvalidOperationException is probably a good choice.

    0 讨论(0)
  • 2021-02-04 23:53

    An alternative to Assert.Fail("messsage") suggested by xUnit docs

    xUnit.net alternative: Assert.True(false, "message")

    has a downside – its output is

    message
    
    Expected: True
    Actual:   False
    

    To get rid of

    Expected: True
    Actual:   False
    

    don't call Assert.True(false, "message") throw Xunit.Sdk.XunitException instead.
    For example, create a helper method similar to this:

    public static class MyAssert
    {
        public static void Fail(string message)
            => throw new Xunit.Sdk.XunitException(message);
    }
    
    0 讨论(0)
  • 2021-02-04 23:54

    Another way to get Pass assert is to create new assert.

    public static class ExtraAssert
    {
        public static void Pass(this Microsoft.VisualStudio.TestTools.UnitTesting.Assert assert)
        {
            return;
        }
     }
    

    There is a catch however, because you could only access that method using 'That' keyword.

    Assert.That.Pass();
    

    ref: https://www.meziantou.net/mstest-v2-create-new-asserts.htm

    0 讨论(0)
提交回复
热议问题