Is it possible to run test suite in doLast?


(Roman) #1

Hi All!

Is there any possibility ro run test suite in doLast?
For example we have a task called “test”, we are using TestNG and have ‘suites’ in doLast. As the result we want to see our test runnig in browser using Selenium.
Code:
test {

    print '\ntest'

    doLast {
        useTestNG() {
            print '\nhohoho\n'
            suites 'src/test/java/test.xml'
            print '\nhahaha\n'
        }
    }
}

In console output we can see only:
$ gradle test
> Configuring > 0/1 projects > root project > Resolving dependencies 'classpath’
test

:compileJava
:processResources
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test

hohoho


hahaha

BUILD SUCCESSFUL

Total time: 10.813 secs

and no info about suites, no browser.
In case without doLast - we see browser, information about our suite in console output.

The same with 2.14.1, 3.2, 3.2.1 versions, hope this is not a bug, looks like I do not know something)

Thanks for any help and suggestions!


(Lóránt Pintér) #2

The problem here is doLast() is called after the original action of the task (=test execution) has already executed. Changing the configuration of the task at this point has no effect anymore. Doing it in doFirst() is different, because you are changing the configuration right before the original task action is executed, and hence the changes can be taken into account during execution.

However, neither of these approaches should be used. Before the task is executed, Gradle inspects the inputs of the task and decides whether or not it’s up-to-date. Changing the inputs of the task during execution (like in a doFirst() clause) means the real inputs are not available when Gradle makes the decision, and you might end up with tasks being up-to-date that should be executed. It’s a chicken and egg problem.

Normally, you would configure the task in the body of the task declaration. For your example this should work fine:

test {
    useTestNG() {
        suites 'src/test/java/test.xml'
    }
}

If this is not suitable for some reason (e.g. the configuration depends on some data calculated by another task), you also have the option to use another task to configure test, like so:

configureTest {
    doLast {
        test {
            // Configure here
        }
    }
}

test {
    dependsOn configureTest
}

(Lóránt Pintér) #3

We are also planning to add validations that makes it impossible to modify task configuration once it has started executing to avoid problems like this one. We will add these in a backwards compatible way: for some time we’ll only warn about problematic tasks, and only fail the build in the next major release.


(Roman) #4

@lptr , thanks for your answer! I changed my build already, but will try your solution too! Thanks)