Интеграционные тесты в Gradle, использующие соглашения по именованию Maven? - PullRequest
0 голосов
/ 31 октября 2019

Исходя из Maven, я исследую Gradle как альтернативу. Технологии: Java 11, jUnit 5, Maven 3.6, Gradle 5.6.

Я застрял в настройке интеграционных тестов. В соответствии с принятыми по умолчанию соглашениями об именах для подключаемых модулей Maven Surefire и Failsafe мои тесты находятся в стандартном каталоге тестов и отличаются суффиксом: модульные тесты заканчиваются на Test.java, а интеграционные тесты заканчиваются на IT.java.

Isможно ли иметь такую ​​же настройку в Gradle? До сих пор я видел два способа:

  • использовать теги jUnit5 (что означает, что мне нужно было бы идти и отмечать каждый интеграционный тест)
  • использовать отдельные каталоги для модульных и интеграционных тестов

В идеале я хотел бы сохранить структуру папок как есть, поскольку она влияет на несколько репозиториев git.

1 Ответ

0 голосов
/ 01 ноября 2019

Хорошо, я думаю, что мне удалось заставить его работать с исходными наборами, благодаря отзывам Лукас и Слав .

Пожалуйста, дайте мне знать, если этоможно улучшить:

// define the dependencies of integrationTest, inherit from the unit test dependencies
configurations {
    integrationTestImplementation.extendsFrom(testImplementation)
    integrationTestRuntimeOnly.extendsFrom(testRuntimeOnly)
}

sourceSets {
    test {
        java {
            // exclude integration tests from the default test source set
            exclude "**/*IT.java"
        }
    }

    // new source set for integration tests
    integrationTest {
        // uses the main application code
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
        java {
            // the tests are at the same directory (or directories) as the unit tests
            srcDirs = sourceSets.test.java.srcDirs

            // exclude the unit tests from the integration tests source set
            exclude "**/*Test.java"
        }
        resources {
            // same resources directory (or directories) with the unit tests
            srcDirs = sourceSets.test.resources.srcDirs
        }
    }
}

// define the task for integration tests
task integrationTest(type: Test) {
    description = "Runs integration tests."
    group = "verification"
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test

    // I'm using jUnit 5
    useJUnitPlatform()
}

// make the check task depend on the integration tests
check.dependsOn integrationTest
...