26

Besides better readability (maybe?) and being able to chain constraints using & and |, what other advantages does the Constraint Model have over the Classic Model?

I'm a happy user of the Classic Model, and I'm in the process of deciding whether it's worth the effort to refactor old tests.

6 Answers 6

20

I have to say I'm a fan of the "classic" model. I find it easier to formulate my assertions for the most part, and I find them easier to read too. There's nothing in the code which isn't necessary - no "That" and "Is" which sounds fluent but doesn't lend itself to discoverability IMO.

That may well be due to familiarity as much as anything else, but I thought it would just be worth reassuring you that you're not the only one who finds the classic model perfectly reasonable :)

Having said that, when there's something which is easier to express using constraints, it makes sense to use it. This is particularly true when there's some condition which can be explicitly tested with a constraint, but where the classic model would just use Assert.IsTrue(condition). The key point is that the constraint is likely to be able to give you more information than the classic one for such cases.

As such I think it's a good idea to learn the constraint-based model, but I wouldn't go as far as converting any tests, and I wouldn't use it where you find the classic model to be simpler or more readable.

6
  • 2
    I'm confused why someone would want to use a model that conveys less precise information in the event of the test failing. If that were the case you could take it to the extreme and just use Assert.IsTrue(...) for everything :-|
    – bytedev
    Nov 17, 2017 at 15:30
  • 1
    @nashwan: I find when a test fails, I usually have enough information - and if I don't, it's easy to change the test anyway. So I'd rather write my code for readability in the more common case where I'm reading passing tests. The extra verbosity of Assert.That(foo, Is.Equal(...)) doesn't appeal. (Similarly, while many people hate the idea of multiple assertions in a test, I'm fine with that, if it's testing one logical flow. If one test fails that's almost always equivalent to more than one failing anyway...)
    – Jon Skeet
    Nov 17, 2017 at 15:33
  • 1
    I was thinking more about tests failing outside the IDE, i.e. in a CI where all people get out is the test name and the failure sentence. Though I have seen many times devs getting the actual/expected mixed up, which can cause confusion, as I mentioned in my answer below. Concerning the two models readability, I agree, it is very much in the eyes of the beholder (as you mentioned).
    – bytedev
    Nov 17, 2017 at 15:46
  • 1
    @nashwan: So if it fails in CI, I add more information and see where it fails again. If you try to make sure that you will never need more information than you get the first time a test fails, you'll waste 99% of your time.
    – Jon Skeet
    Nov 17, 2017 at 15:53
  • 2
    @ANewGuyInTown: Well, it's been over a decade since I tried to use it, but I always found it hard to find the methods that I wanted.
    – Jon Skeet
    Mar 1, 2022 at 8:24
15

I don't know that there are any other advantages besides readability. But that can be a big advantage. I find that simply starting every test with Assert.That( ... ) rather than having a handful of Assert functions makes it a million times easier to visually scan your asserts, since you no longer have to bother with the function name, just look at the arguments.

With NUnit 2.4, both syntaxes (class and constraint) use the exact same code underneath. There's no advantage behind the scenes one way or another. Unless you really have no better use for the time, I wouldn't bother rewriting tests.

11

The NUnit 3 documentation that introduces assertions and that compares the newer Constraint Model to the Classic Model includes the following example:

For example, the following code must use the constraint model. There is no real classic equivalent.

  int[] array = new int[] { 1, 2, 3 };
  Assert.That(array, Has.Exactly(1).EqualTo(3));
  Assert.That(array, Has.Exactly(2).GreaterThan(1));
  Assert.That(array, Has.Exactly(3).LessThan(100));

While the document states that there is no "real classic equivalent," one could use Classic Syntax with LINQ to write what I would consider equivalent tests:

Assert.AreEqual(1, array.Where(x => x == 3).Count());
Assert.AreEqual(2, array.Where(x => x > 1).Count());
Assert.AreEqual(3, array.Where(x => x < 100).Count());

Some might conclude that the Constraint Model tests that I lifted from the documentation are more readable than these Classic Model equivalents. But that is arguably subjective.


However, that is not the whole story. More important is the improvement in the error message that a failed Constraint Model test emits when a test fails. For instance, consider this Classic Model test that will fail:

int[] array = new int[] { 1, 2, 3 };
Assert.AreEqual(1, array.Where(x => x == 4).Count());

The AssertionException that is thrown by NUnit contains the following "terse" Message:

    Expected: 1
    But was:  0

In contrast, when expressing this test in the newer Constraint Model syntax:

Assert.That(array, Has.Exactly(1).EqualTo(4));

...NUnit returns the Message:

    Expected: exactly one item equal to 4
    But was:  < 1, 2, 3 >

I think that most would agree that this exception message is much more helpful than the one produced using NUnit's older Classic Model syntax.


Much thanks to @nashwan for helping me understand this important improvement in the error messaging introduced in the Constraint Model.

3
  • 1
    You're right arguing which is more readable is a waste of time as it is completely subjective. However the output of a failed test is very different. Compare the output if I were to test for a value 4 that does not exist - "Expected: exactly one item 4 - But was: < 1, 2, 3 >" (Constraint) or "Expected: 1 But was: 0 " (Classic). The Constraint Model clearly conveys more precise information than the Classic Model.
    – bytedev
    Nov 17, 2017 at 15:21
  • @nashwan Thanks very much for your feedback! I have incorporated your example into my answer. Your example certainly supports the argument that the newer Constraint Model syntax really is an improvement over the older Classic Model syntax.
    – DavidRR
    Nov 17, 2017 at 16:32
  • You're welcome. In fact the improvement the Constraint model provides over Simple model with regards to the output message on failure is why I rarely bother providing any message argument in code.
    – bytedev
    Nov 17, 2017 at 16:43
4

I personally prefer the Constraint Model Assert.That style and only use it now. I find this newer style more readable, and have determined that it is much less likely to get the "actual" and "expected" arguments mixed up. (Like you certainly can using the Classic Model Assert.AreEqual, etc., which I have seen many people do.) This obviously leads to broken tests that report incorrect results.

As an example, without checking ;-), which of these is correct?

Assert.AreEqual(actual, expected);
Assert.AreEqual(expected, actual);
2
  • I do find my self checking the expected order of these arguments from time-to-time. But that said, for AreEqual, does the order of these arguments really matter? Perhaps Assert.Greater(value1, value2) is a better example to illustrate this potential ambiguity in the Classic Model?
    – DavidRR
    Nov 17, 2017 at 14:50
  • 3
    @DavidRR yes the order does matter. Not whether the test fails or not (it will fail regardless), but because it will tell you the actual value it expected not the expected value (NUnit constructs a sentence from these values).
    – bytedev
    Nov 17, 2017 at 15:07
0

One main benefit of the constraint model over a classic model is that there are certain assertions that may be written in one line in which there are no real classic model equivalents.

For instance, these constraint model have no real classic model equivalents:

int[] array = new int[] { 1, 2, 3 };
Assert.That(array, Has.Exactly(1).EqualTo(3));
Assert.That(array, Has.Exactly(2).GreaterThan(1));
Assert.That(array, Has.Exactly(3).LessThan(100));

Source may be found here.

0

A minor addition to the points made in other answers. The constraint model has code analyzers that will do things like type checking. For example, I had a test of a small function to convert a single item into an IEnumerable:

[Test]
public void Test_AsSingleton()
{
    var result = "A".AsSingleton();

    Assert.AreEqual(
        new[] { "A" },
        result);

    Assert.AreNotEqual(
        "A",
        result);
}

When this was converted to the constraint model, it correctly identified that the second test is redundant because the types are incompatible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.