Question

Is there any way to give dependency to @BeforeMethod on @Test,because in my scenario different TestMethod have different setup and I need one setup depends on TestMethod. I add here some code snippet for better understanding

@BeforeMethod(groups = {"gp2"})
public void setUp1() {
    System.out.println("SetUp 1 is done");
}

@BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})
public void setUp2() {
    System.out.println("SetUp 2 is done");
}

@Test(timeOut = 1, groups = {"tgp1"})
public void testMethod() throws InterruptedException {
    Thread.sleep(2000);
    System.out.println("TestMethod() From Group1");
}

@Test(dependsOnMethods = {"testMethod"}, groups = {"tgp2"})
public void anotherTestMethod() {
    System.out.println("AnotherTestMethod()From Group1 and Group2");
}

OutPut

SetUp 1 is done SetUp 2 is done

but I need setUp1() should be executed not setUp2() because it is depends on tgp1 group.

Another thing I observe that,If I change dependencies from

@BeforeMethod(groups = {"gp1"}, dependsOnGroups = {"tgp1"})

to

@BeforeMethod(groups = {"gp1"}, dependsOnMethods = {"testMethod"}) 

then I got an Exception like

 setUp2() is depending on method public void testMethod() throws java.lang.InterruptedException, which is not annotated with @Test or not included.

I need execution should be on this steps

SetUp1---->testMethod1()------->SetUp2--------->testMethod2()

I need this because different TestMethods have different work and it have different SetUp().

Was it helpful?

Solution

In your case, you can simply call respective setUp method from respective Test method as you really don't have common @BeforeMethod for each Test.

Or you can separate these two tests into different Test classes each having a @BeforeMethod.

Or you can do conditional setup call based on test method name with one @BeforeMethod method as below. Here @BeforeMethod can declare a parameter of type java.lang.reflect.Method. This parameter will receive the test method that will be called once this @BeforeMethod finishes

@BeforeMethod
public void setUp(Method method) {
    if (method.getName().equals("testMethod")) {
        setUp1();
    } else if (method.getName().equals("anotherTestMethod")) {
        setUp2();
    }
}

public void setUp2() {
    System.out.println("SetUp 2 is done");
}

public void setUp1() {
    System.out.println("SetUp 1 is done");
}    

OTHER TIPS

The purpose of the @BeforeMethod is to run before each test method annotated with @Test and do some set-up work. Therefore it is somewhat confusing why you would want to create dependency that works the other way - execute the test method before set-up method. The usual approach would be following:

@BeforeMethod
public void setUp() {
}

@Test
public void testMethod1() {
}

@Test
public void testMethod2() {
}

Which most likely generate following execution list:

setUp()
testMethod1()
setUp()
testMethod2()

If there multiple @BeforeMethods all of them will be run before the @Test method.

Now if you want to run different groups, you would annotate different methods with different groups and specify, which groups to run. However, for that you will need to provide either testng.xml or specify which groups to execute.


EDIT (based on additional comment):

I would suggest one of the following approaches:

  1. Separate test methods into different classes and have @BeforeMethod in each class perform required setup. This especially makes sense if the tests are covering different areas.
  2. Define different groups and put corresponding test and set-up methods into same group. This option lets you mix and match set-up methods with test method.

Some things that are unclear from your question, but if relevant could benefit from suggested approaches:

  • Is there need for dependency?
  • Does the order or the tests matter?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top