Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tests dependent on commonly used functionality with NUnit

I have some initialization code to use my API. The initialization may fail and I´d like to test it in an NUnit test.

After the initialization the API may be used. I´m testing the API too, but all my test methods will use the same, common, initialization code.

What I would ideally like is if this behavior:

  1. The Initialization test is run.
  2. The other tests are run if [1] succeeded.

In all cases where [1] will fail, so will all other tests. But the valuable information is that [1] fails. That's where I most likely will find the problem. It would be nice if the other tests could be marked with ? or something, indicating that they did not execute as functionality they depend on didn't pass the tests.

I know that tests should not be brittle. But I can't get around the fact that the initialization code is necessary for correct execution of other functionality.

This is a more general problem where some functionality depends on other functionality. Where the "other functionality" is far too commonly used to provide any real value by failing all tests depending on it. It would be better if the "other functionality" would be tested separately.

like image 646
Deleted Avatar asked Nov 05 '22 10:11

Deleted


2 Answers

OK here's how I would go about this...

Put the common initialization into a Setup method since its needed for all tests. If initialization throws an error, you'd see

  • all Tests in a suite failing (which I have been trained over time to recognize as a hint that maybe setup / teardown has thrown an exception).
  • the stacktrace for the failing tests containing the Setup method.

If this is too implicit for you, you may (although I wouldn't recommend it) add an empty test with a good name to the same suite. If that test shows up as green, you can be sure that Setup / common init code has succeeded.

[Test]
public void VerifySetup() {}

Update: Seem like you have a pretty niche requirement. I don't know of any mechanism in NUnit to specify such conditional execution of tests - e.g. Run Test2 thru 10 only if Test1 passes.

like image 192
Gishu Avatar answered Nov 22 '22 03:11

Gishu


I've been in contact with the NUnit developers. It's not possible at the moment without writing a pretty complex plugin. The feature will turn up somewhere in the 3.x code base but will not appear in 2.5. I will consider writing it, but not for the time being.

like image 31
Deleted Avatar answered Nov 22 '22 03:11

Deleted