How to run your grouped testNG tests using Gradle

If you go back and see PersonTest.

kt, you can see the test should pass since we are setting up the correct value for Person’s name and age in @BeforeMethod annotation.

So what went wrong here?> Task :runTests FAILEDGradle suite > Gradle test > PersonTest.

personAgeTest FAILED java.

lang.

AssertionError at PersonTest.

kt:24Gradle suite > Gradle test > PersonTest.

personNameTest FAILED java.

lang.

AssertionError at PersonTest.

kt:192 tests completed, 2 failedFAILURE: Build failed with an exception.

Let’s get into investigation mode and run the same command with –info CMD line switch to get more info.

/gradlew clean runTests -Dtag=person_test –infoHmm.

????, We can see the actual value for name and age is still the initialization value and also the println methods content is not printed.

This is basically a hint that the setup/teardown methods are not getting executed causing these failures.

Gradle suite > Gradle test > PersonTest.

personAgeTest FAILED java.

lang.

AssertionError: expected [23] but found [25] at org.

testng.

Assert.

fail(Assert.

java:96) at org.

testng.

Assert.

failNotEquals(Assert.

java:776) at org.

testng.

Assert.

assertEqualsImpl(Assert.

java:137) at org.

testng.

Assert.

assertEquals(Assert.

java:118) at org.

testng.

Assert.

assertEquals(Assert.

java:652) at org.

testng.

Assert.

assertEquals(Assert.

java:662) at PersonTest.

personAgeTest(PersonTest.

kt:24)Gradle suite > Gradle test > PersonTest.

personNameTest FAILED java.

lang.

AssertionError: expected [Rob] but found [John] at org.

testng.

Assert.

fail(Assert.

java:96) at org.

testng.

Assert.

failNotEquals(Assert.

java:776) at org.

testng.

Assert.

assertEqualsImpl(Assert.

java:137) at org.

testng.

Assert.

assertEquals(Assert.

java:118) at org.

testng.

Assert.

assertEquals(Assert.

java:453) at org.

testng.

Assert.

assertEquals(Assert.

java:463) at PersonTest.

personNameTest(PersonTest.

kt:19)2 tests completed, 2 failedThis was a really weird issue for me since it was initially beyond my comprehension why testNG and gradle are behaving this way.

After couple of hours of googling and running across multiple discussions on GitHub, Stack Overflow and gradle docs.

I finally came across the solution to fix this and get the behavior that we want.

In order for setup/teardown methods to work with above config we need to add alwaysRun = true in them.

If we follow TestNG documentation for alwaysRun we can see below:For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.

 For after methods (afterSuite, afterClass, …): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.

Here is the final code with the changes.

import org.

testng.

Assertimport org.

testng.

annotations.

AfterMethodimport org.

testng.

annotations.

BeforeMethodimport org.

testng.

annotations.

Testclass PersonTest { private var name = "Rob" private var age = 23 @BeforeMethod(alwaysRun = true) fun before() { println("Performing setup.

") name = "John" age = 25 } @Test(groups = ["person_test"]) fun personNameTest() { Assert.

assertEquals("John", name) } @Test(groups = ["person_test"]) fun personAgeTest() { Assert.

assertEquals(25, age) } @AfterMethod(alwaysRun = true) fun after() { println("Performing teardown.

") }}And surely gradle build and test passes (validated by the debugging messages printed from setup and teardown methods)Gradle suite > Gradle test > PersonTest STANDARD_OUT Performing setup.

Gradle suite > Gradle test STANDARD_OUT Performing teardown.

Performing setup.

Performing teardown.

Finished generating test XML results (0.

0 secs) into: .

/build/test-results/runTestsGenerating HTML test report.

Finished generating test html results (0.

003 secs) into: .

/build/reports/tests/runTests:runTests (Thread[Task worker for ':',5,main]) completed.

Took 0.

527 secs.

BUILD SUCCESSFUL in 1s3 actionable tasks: 3 executedHopefully if you have the same problem then this blog might help you save some time.

If you are interested more into what links was referred to for arriving at this solution you can refer below:Gradle docs — Test groupingGitHubThat’s it folks.

Until next time.

Happy coding.

If you liked this or feel someone else can also benefit from this, why not share it with a friend or colleague.

.

. More details

Leave a Reply