Java 如何使用 Gradle 运行 JUnit 测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20707017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:43:40  来源:igfitidea点击:

How to run JUnit tests with Gradle?

javagradlejunit4build.gradle

提问by Q Zhao-Liu

Currently I have the following build.gradlefile:

目前我有以下build.gradle文件:

apply plugin: 'java'

sourceSets {
    main {
        java {
            srcDir 'src/model'
        }
    }
}

dependencies {
    compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
    runtime fileTree(dir: 'libs', include: '*.jar')
}    


This build.gradlefile is for my repository here. All of my main files are in src/model/and their respective tests are in test/model.


的build.gradle文件是我的仓库在这里。我的所有主要文件都在src/model/ 中,它们各自的测试在test/model 中

How do I add a JUnit 4 dependencycorrectly and then run those tests in the folders of tests/model?

如何正确添加 JUnit 4依赖项,然后在tests/model的文件夹中运行这些测试?

采纳答案by Oliver Charlesworth

How do I add a junit 4 dependency correctly?

如何正确添加junit 4依赖项?

Assuming you're resolving against a standard Maven (or equivalent) repo:

假设您正在解决标准 Maven(或等效)存储库:

dependencies {
    ...
    testCompile "junit:junit:4.11"  // Or whatever version
}

Run those tests in the folders of tests/model?

在测试/模型的文件夹中运行这些测试?

You define your test source set the same way:

您以相同的方式定义测试源集:

sourceSets {
    ...

    test {
        java {
            srcDirs = ["test/model"]  // Note @Peter's comment below
        }
    }
}

Then invoke the tests as:

然后调用测试为:

./gradlew test

EDIT: If you are using JUnit 5 instead, there are more steps to complete, you should follow this tutorial.

编辑:如果您使用的是 JUnit 5,则需要完成更多步骤,您应该按照本教程进行操作

回答by DivDiff

If you set up your project with the default gradle package structure, i.e.:

如果您使用默认的 gradle 包结构设置您的项目,即:

src/main/java
src/main/resources
src/test/java
src/test/resources

then you won't need to modify sourceSets to run your tests. Gradle will figure out that your test classes and resources are in src/test. You can then run as Oliver says above. One thing to note: Be careful when setting property files and running your test classes with both gradle and you IDE. I use Eclipse, and when running JUnit from it, Eclipse chooses one classpath (the bin directory) whereas gradle chooses another (the build directory). This can lead to confusion if you edit a resource file, and don't see your change reflected at test runtime.

那么你不需要修改 sourceSets 来运行你的测试。Gradle 会发现你的测试类和资源在 src/test 中。然后你就可以按照 Oliver 上面说的那样运行了。需要注意的一件事:在使用 gradle 和 IDE 设置属性文件和运行测试类时要小心。我使用 Eclipse,从它运行 JUnit 时,Eclipse 选择一个类路径(bin 目录),而 gradle 选择另一个(构建目录)。如果您编辑资源文件,并且在测试运行时看不到您的更改反映,这可能会导致混淆。

回答by nobar

If you created your project with Spring Initializr, everything should be configured correctly and all you need to do is run...

如果您使用Spring Initializr创建了您的项目,那么一切都应该正确配置,您需要做的就是运行...

./gradlew clean test --info
  • Drop --infoif you don't want to see test output.
  • Drop cleanif you don't want to rerun tests that have already passed since the last change.
  • --info如果您不想看到测试输出,请删除。
  • clean如果您不想重新运行自上次更改以来已经通过的测试,请删除。


Dependencies required in build.gradlefor testing in Spring Boot...

build.gradle在 Spring Boot中进行测试所需的依赖项...

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}


For some reason the test runner doesn't tell you this, but it produces an HTML report in build/reports/tests/test/index.html.

出于某种原因,测试运行程序不会告诉您这一点,但它会以 .html 格式生成 HTML 报告build/reports/tests/test/index.html