TestNG - @BeforeMethod for specific methods

I use Spring Test with TestNG to test our DAOs, and I wanted to run a specific text device script before certain methods, allowing the changes to be discarded after each method so that the tests were free to do anything with the device data.

Initially, I thought that β€œgroups” were suitable for him, but I already realized that they were not intended for this (see this question: TestNG BeforeMethod with groups ).

Is there a way to configure the method @BeforeMethodto run only up to a specific @Tests? The only thing I see is workarounds:

  • Define a typical installation method and call at the beginning of each method @Test;
  • Move the method @BeforeMethodto a new class (top level or inner class) along with all the methods that depend on it.

None of them are perfect, I would like my tests to be naturally grouped and clean, and not separated due to lack of alternatives.

+4
source share
2 answers

You can add the @BeforeMethod parameter of type "java.lang.reflect.Method". TestNG will then enter the reflection information for the current test method, including the name of the method that you could use to switch.

If you add one more parameter "Object", you will also receive parameters for calling the test method.

TestNG 5.18.1 TestNG.

+9

. , , , , . . , , , . @Before. , , , , , . , , , , , . , , arent, , ?

+3

All Articles