Where to put the common setUp code for differen testclasses?

I have several different test classes that require certain objects to be created before these tests run. Now I am wondering if I should put the object initialization code in a separate helper class or superclass.

Doing this will undoubtedly reduce the number of duplicate code in my test classes, but it will also make them less readable.

Is there a guide or template on how to work with common SetUp code for unit tests?

+3
source share
4 answers

, . . , , unit test ... .

+6

, ( ) / .

Refactoring , , .

, .

+1

, .

This is precisely the reason for the setUp () and Teardown () functions. You must prepare for testing using the setUp () function and perform tasks after the test in Teardown mode. If you have more than one test on the same object, you should consider Testsuperclass, in which you use setUp () to initiate this object.

Check How do I run the setUp () and tearDown () code once for all my tests?

0
source

I think there are two points here:

  • The trade-off between duplicate code and readability.
  • Each unit test case must be independent.
0
source

All Articles