目前我有以下 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')
}
这个 build.gradle 文件用于我的存储库 here .我所有的主要文件都在 src/model/ 中,它们各自的测试都在 test/model 中。
如何正确添加 JUnit 4 依赖项,然后在 tests/model 的文件夹中运行这些测试?
最佳答案
How do I add a junit 4 dependency correctly?
假设您正在解决标准 Maven(或等效)存储库:
dependencies {
...
testCompile "junit:junit:4.11" // Or whatever version
}
Run those tests in the folders of tests/model?
您以相同的方式定义测试源集:
sourceSets {
...
test {
java {
srcDirs = ["test/model"] // Note @Peter's comment below
}
}
}
然后调用测试:
./gradlew test
编辑:如果您使用的是 JUnit 5,则需要完成更多步骤,您应该遵循 this tutorial .
关于java - 如何使用 Gradle 运行 JUnit 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20707017/