Running test code in afterSuite problem

I’m trying to access a class that’s a part of the test suite in my afterSuite

afterSuite {

x.y.z.SomeClass.someStaticMethod() }

Gradle complains that property x doesn’t exist. I’m not sure which context this is executed in and why the classpath resultion doesn’t happen in that context. I’m fully qualifying the name.

Is there a way to access a class (that’s a part of the compile phase) in the afterSuite closure?

You cannot reference a class that is part of your testRuntime in the afterSuite closure. All tests are executed in a seperate JVM. Your testclasses are not part of the classpath of your build file that has the afterSuite closure defined. Can you explain what usecase you have for this? maybe we can find a workaround.

cheers, René

Rene, I need to execute cleanup code after all the tests are run. Basically, we’re doing selenium tests and I need to perform selenium driver cleanup. This custom code is contained inside a helper class.

JUnit doesn’t have a @BeforeSuite annotation like TestNG, otherwise I’d stick it in there.

Any ideas?

If the tests are run in a separate JVM. Can’t I add the build/classes/test directory to the classpath?

If the tests are run in a separate JVM. Can’t I add the build/classes/test directory to the classpath?

You mean adding them to your build classpath? You shouldn’t. A workaround can be to use the Project.javaexec method to execute code defined in your tests:

afterSuite{
    project.javaexec{
        main = "x.y.z.SomeClass"
        classpath = sourceSets.testRuntime.runtimeClasspath
    }
}

To get this snippet working, it is necessary to declare a public static void main(String… args) method in SomeClass. The simplier way would be to extract the static method someStaticMethod from your tests and add the method to your gradle build script.

hope that helped.

regards, René

Yeah, sorry, that won’t work. The method references an instance of selenium that’s created in the same JVM that runs the tests. So I really need to execute something inside the same JVM that executes the tests.

Any other ideas? Is there a way to execute something at the end of the whole test suite run, but within the same JVM as the tests are run?

Thanks.

So am I understanding this right, the beforeSuite and afterSuite are executed in a different JVM than the tests are run in? Wouldn’t it make more sense to execute those in the same JVM?

All of Gradle’s test-related callbacks are executed in the Gradle JVM, and that’s intended. If you need to execute code in the test JVM, you’ll need to use some feature of the test framework. For example, you could use a JUnit suite.