如何使用Gradle运行JUnit测试? [英] How to run JUnit tests with Gradle?

查看:141
本文介绍了如何使用Gradle运行JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有下面的 build.gradle 文件:

  apply plugin:'java' 

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

依赖项{
编译文件('libs / mnist-tools.jar','libs / gson-2.2.4.jar')
runtime fileTree(dir:'libs',include :'* .jar')
}





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

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


>假设您正在解决一个标准的Maven(或同等)回购:

 依赖关系{
...
testCompilejunit:junit:4.11//或任何版本
}




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


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

  sourceSets {
...

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

然后调用测试:

  ./ gradlew test 

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


Currently I have the following build.gradle file:

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.gradle file is for my repository here. All of my main files are in src/model/ and their respective tests are in test/model.

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

解决方案

How do I add a junit 4 dependency correctly?

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

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.

这篇关于如何使用Gradle运行JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆