Search code examples
unit-testingnunit

Advantages of the Constraint Model over the Classic Model in NUnit?


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.


Solution

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