jeudi 3 mars 2016

Unit testing: Is it bad practice to call public methods (for setup) while testing another method?

This is a general question, but it may help to know that I'm working with a system that encapsulates an RPC-style (remote procedure calling) controller, which is what I'm testing. It contains public methods that change the internal state of this controller. The state of this controller can be read using public methods.

Two of the methods in this class are:

  • CreateProgramSet(...), which creates a "set of programs" (changes the internal state of the RPC controller) and
  • GetStatus(), which retrieves the state of this RPC controller including program sets created with CreateProgramSet(...).

To test GetStatus(), I'm calling CreateProgramSet(...) (for setup) with some hard-coded parameters and then testing the result of GetStatus() against a hard-coded value.

To test CreateProgramSet(...), I'm doing exactly the same, except in this case GetStatus() is used to verify that CreateProgramSet(...) acted correctly.

The problem I see in this testing scenario is that now both tests are coupled to both methods, so if GetStatus() changes, both tests may fail. If CreateProgramSet(...) changes, both tests may fail. But most importantly, if both methods change the exact right way (without knowledge of each other's change), both tests may pass for the wrong reason. This is obviously an undesirable scenario. The only way I see out of this problem is testing the internals of the class, but that is frowned upon in testing.

Most examples that I found deal with methods that return a value, like an "add" method or something extremely simple which doesn't require a setup. In reality many classes get complex enough to require a setup to test the individual parts of the class. This is especially true for classes that can be in multiple states. And if a class does not store state, might as well make it a static class. Also most of the examples I found test output of data rather than change of state. Am I doing something wrong? This problem to me points to a code smell but I can't seem to figure out what I'm doing wrong.

This question is meant to be language agnostic, but if it makes a difference, I'm working with C# and NUnit.

Aucun commentaire:

Enregistrer un commentaire