81

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;//would be nice not to have this
observable.Subscribe(_ =>
{
    if (real test)
        condition= true;//Assert.Pass()
});
StartObservable();
Assert.True(condition);//Assert.Fail()      
dodekja
  • 537
  • 11
  • 24
naeron84
  • 2,955
  • 3
  • 30
  • 37

4 Answers4

113

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • See the code sample (edit). It's note an option to just return or I could use goto... :D – naeron84 Jan 31 '13 at 17:57
  • @naeron84: I would just live with the version you've got, to be honest - if you need to test this for multiple conditions with observables, you could wrap it all up into a helper method pretty easily, which would be simpler to use than the version with Assert.Fail/Pass. – Jon Skeet Jan 31 '13 at 18:01
  • I guess this question should be closed then. – naeron84 Jan 31 '13 at 18:08
  • 20
    IMO its a bit rubbish that xUnit doesnt have Fail(), using Assert.True() will mean you will still get output like "Expected: True Actual: False" which is a little misleading. – bytedev Dec 15 '17 at 11:54
  • 3
    @nashwan: Agreed. There are quite a few NUnit features I still miss in xUnit. Oh well :( – Jon Skeet Dec 15 '17 at 11:55
  • 1
    @bytedev `throw new Xunit.Sdk.XunitException(message);` to get rid of `Expected: True Actual: False`. Check out [my answer](https://stackoverflow.com/a/51945814/818321) for more details. – Myk Aug 21 '18 at 09:43
  • Actually, I just noticed that if you add a message, you don't get the Expected/Actual string but the `XunitException` does make more sense. – Fabio Milheiro Apr 30 '20 at 10:29
67

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);
}
Myk
  • 1,520
  • 16
  • 19
6

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.

Tony Wall
  • 1,382
  • 20
  • 18
  • 3
    At least in xunit 2.3.1 there is a suitable exception -- `Xunit.Sdk.XunitException`. Probably it's available in earlier versions too -- I didn't check. Take a look at [my answer](https://stackoverflow.com/a/51945814/818321) for more details. – Myk Aug 21 '18 at 09:46
0

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

Ariwibawa
  • 627
  • 11
  • 23