Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nUnit Test interdependencies/hierarchies

Is there a way to give nUnit a hierarchy of tests, so that later test aren't run if earlier test fail?

Suppose I have a set of data that is expected to be processed and ordered, and I’ve got a UT that tests that the ordering is done, but also a bunch other tests that test the processing and which assume that the ordering has been successful and will hence fail spuriously if that’s false.

(And suppose that breaking this dependency is not feasible - I know that is the ideal solution!)

Then it would be nice to tell nUnit … "If the ordering test has failed, then don’t even bother with these ones (throw inconclusive, maybe?) ‘cos we know they ain’t gonna work."

Obviously I could do it manually, but that's going to add a lot of essentially irrelevant cruft to the tests (we have nice short tests, so a "call the head test and catch Exceptions" block would double the length of some tests)

I'm hoping that nUnit might JustDoThis already, but I can’t see it anywhere?

like image 438
Brondahl Avatar asked Mar 19 '23 10:03

Brondahl


2 Answers

As mentioned in your question, the most 'proper' fix is to break the dependency between the tests. If you really want to do this though, NUnit does support assumptions (which are a bit like assertions, but at the start of your test), which could be used to check for some precondition that affects the validity of your test. Searching for "NUnit Assume" brought up the following StackOverflow question, which looks similar to this one: How do I ignore a test based on another test in NUnit?

like image 180
hgcummings Avatar answered Mar 28 '23 11:03

hgcummings


I believe hierarchy in unit tests is a bad idea. As you said your unit tests are small, nice and independent of course. And it should stay the same. Adding some dependencies breaks it.

To offer some workaround, I would suggest to use the Category attribute combined with the /stoponerror option. You could group tests by its importance and run it by this categories. And use stoponerror option will stop executing the rest of tests when the first fails. I'm not sure if this also works in integration with Visual Studio but you can achieve this with NUnit command line tool. See this

like image 43
Jan Barta Avatar answered Mar 28 '23 12:03

Jan Barta