unbroken-dome / gradle-testsets-plugin

A plugin for the Gradle build system that allows specifying test sets (like integration or acceptance tests).
MIT License
230 stars 50 forks source link

How to run setup and teardown at the start and end of integration test-set? #91

Closed chitreshd closed 5 years ago

chitreshd commented 5 years ago

Hi

We are using the plugin to describe integration tests in our gradle project.

testSets {
    integrationTest {
        dirName = 'integration-test'
    }
}

Is there a way execute a setup ( example: IntegrationTest.setup() ) before running any test in integration-test and then cleanup ( example: IntegrationTest.cleanup() ) after running all tests.

tkrullmann commented 5 years ago

Hi,

the test tasks created by the plugin are just plain Gradle Test tasks, which don't have such a feature.

in most cases I would recommend setting up and tearing down the test together with your test code. If you want to do it in Gradle, you should do so in separate tasks and wire the task dependencies accordingly.

Example:

tasks {

    setupIntegrationTest {
        // ... configure task to do whatever
    }

    tearDownIntegrationTest {
        mustRunAfter integrationTest
        // ... configure task to do whatever
    }

    setupIntegrationTest.finalizedBy tearDownIntegrationTest
    integrationTest.dependsOn(setupIntegrationTest)
}

Sometimes this can be quite difficult to get right, especially if the teardown task has other task dependencies, or if some dynamic data generated by the setup task (e.g. a Docker container port) needs to be passed to the test.

chitreshd commented 5 years ago

that's a good suggestion. I am new to gradle, so let me try it. Thanks for your help.