developer tip

모든 테스트가 최신 상태 일 때 Gradle 테스트를 실행하는 방법은 무엇입니까?

optionbox 2020. 8. 16. 20:10
반응형

모든 테스트가 최신 상태 일 때 Gradle 테스트를 실행하는 방법은 무엇입니까?


성적 스크립트를 설정했습니다. Gradle 빌드를 실행하면 모든 것이 작동하고 jUnit 테스트가 실행됩니다.

그 후 Gradle 테스트를 실행하면 다음이 표시됩니다.

C:\Users\..\..\Project>gradle test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE

을 수행하면 gradle cleanGradle 빌드가 작동합니다. 물론 ... 전체 프로젝트를 빌드하는 것이 아니라 테스트 만 재설정 할 수 있기를 원합니다. 어떻게해야합니까?


한 가지 옵션은 명령 줄--rerun-tasks 에서 플래그를 사용하는 것 입니다. 이렇게하면 모든 테스트 작업과 종속 된 모든 작업이 다시 실행됩니다.

테스트를 다시 실행하는 데 관심이있는 경우 또 다른 옵션은 테스트를 실행하기 전에 gradle이 테스트 결과를 정리하도록하는 것입니다. 이 작업을 사용하여 수행 할 수 있습니다 cleanTest.

일부 배경-Java 플러그인은 다른 작업 각각에 대해 깨끗한 작업을 정의합니다. 문서 에 따르면 :

cleanTaskName- 지정된 작업에서 만든 파일을 삭제합니다. cleanJar는 jar 작업에 의해 생성 된 JAR 파일을 삭제하고 cleanTest는 테스트 작업에 의해 생성 된 테스트 결과를 삭제합니다.

따라서 테스트를 다시 실행하기 위해 필요한 것은 cleanTest작업 도 실행하는 것입니다. 즉,
gradle cleanTest test


다른 옵션은 build.gradle에 다음을 추가하는 것입니다.

test.outputs.upToDateWhen {false}

gradle test --rerun-tasks

모든 작업 최적화가 무시되도록 지정합니다.

출처 : https://gradle.org/docs/current/userguide/gradle_command_line.html


다음은 명령 줄을 수정하지 않으려는 경우 "build.gradle"파일을 사용하는 솔루션입니다.

test {
    dependsOn 'cleanTest'
    //Your previous task details (if any)
}

그리고 여기에 출력이 있습니다. 이전 출력에서 ​​2 가지 변경 사항을 확인합니다.

1) 새로운 'cleanTest'작업이 출력에 나타납니다.

2) 'test'는 항상 정리되므로 (즉 'UP-TO-DATE'는 아님) 매번 실행됩니다.

$ gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:jar
:bootRepackage
:assemble
:cleanTest
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
:check
:build

이것은 최근 Gradle의 블로그 게시물 Stop rerunning your tests 에서 주제였습니다 . 저자는 사용 예를 보여줍니다 outputs.upToDateWhen { false }그것이 잘못된 이유를 설명 :

이것은 실제로 재실행을 강제하지 않습니다.

이 스 니펫의 작성자는 "항상 내 테스트를 다시 실행"이라고 말하고 싶었습니다. 하지만이 스 니펫이하는 일은 아닙니다. 작업이 만료 된 것으로 표시하기 만하여 Gradle 이 출력 다시 생성 하도록합니다. 하지만 여기에 빌드 캐시가 활성화 된 경우 Gradle이 출력을 다시 생성하기 위해 작업을 실행할 필요가 없습니다. 캐시에서 항목을 찾고 결과를 테스트의 출력 디렉토리에 압축 해제합니다.

The same is true for this snippet:

test.dependsOn cleanTest

Gradle will unpack the test results from the build cache after the output has been cleaned, so nothing will be rerun. In short, these snippets are creating a very expensive no-op.

If you’re now thinking “Okay, I’ll deactivate the cache too”, let me tell you why you shouldn’t.

Then, the author goes on to explain why rerunning some tests is a waste of time:

The vast majority of your tests should be deterministic, i.e. given the same inputs they should produce the same result.

In the few cases where you do want to rerun tests where the code has not changed, you should model them as an input. Here are both examples from the blog post that show adding an input so the task will use it during its up-to-date checks.

task randomizedTest(type: Test) {
  systemProperty "random.testing.seed", new Random().nextInt()
}

task systemIntegrationTest(type: Test) {
  inputs.property "integration.date", LocalDate.now()
}

I recommend reading the entire blog post.


Also, having to add --rerun-tasks is really redundant. Never happens. Create a --no-rerun-tasks and make --rerun-tasks default when cleanTask


I think this is a valid question given that it is possible in Gradle to run this command test, and what happens is that nothing happens!

But I would question the need ever to do this, as Jolta said in his comment: if no code has changed why do you need to re-test? If you have doubts about third-party input I'd say you need to cater for this in your app code. If you have worries that your code might be "flaky", i.e. able to pass all tests first time but not a second (or 100th time), don't you need to think about why you have these doubts, and address them?

Personally I think this is a (very minor) design fault in Gradle: if everything is completely up-to-date, rather than going "BUILD SUCCESSFUL" it should say "NO CHANGE SINCE LAST SUCCESSFUL BUILD: NOTHING DONE".

참고URL : https://stackoverflow.com/questions/29427020/how-to-run-gradle-test-when-all-tests-are-up-to-date

반응형