Where to use the reusable testing methods used in several grails test classes

I often have to configure the same test structures for different test cases. Therefore, I created the TestService class, which has several public methods for test classes.

I assume that this is not the best place to use them, since TestService will also be deployed, although it is not needed for production.

Where would you put these conventional testing methods? Is there a “best practice” for this?

+4
source share
3 answers

The way I do this is similar to the answers above. If I have a MyDomain class, I put the MyDomainTestHelper class in a test package. The helper has static methods that return the domain objects in question. Therefore I may have

static createTestMyDomain() {...} 

which creates an instance with reasonable defaults and

 static createTestMyDomain(String something) 

so i can specify something etc.

You should not put them in a “service” as you mentioned. But any way you can be dry is a good way.

+4
source

Why don't you put it next to your test classes? The usual way is to put all the test code in a directory separately from the source directory (for example, "test"). That is, not only the test cases themselves, but also auxiliary classes, such as utilities, breadboard implementations, etc.

Testing should be carried out in the same package as the class they tested (but in a different physical directory, as described above). The test utility class, which is used by several test cases, should be placed in some common utility package.

+1
source

I try to adhere to the DRY principle as much as possible when writing tests. This manifests itself mainly in two ways:

  • I usually use the TestData class, which contains all the data that I pass to my tests in static properties to make it easier to keep track of what the tests enter, and so that I never give different data when I intend to give the same thing. This is in the test project or in the class library that only test projects reference.

  • If I need that in many tests I need the same installation or break procedures, I often use inheritance - I create a base class that test devices can inherit, where all the common actions go.

Footnote: I have developed almost all of my development in C #, but I believe that these practices can be applied in any language using a testing framework.

+1
source

Source: https://habr.com/ru/post/1315666/


All Articles