File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ ###Gradle单元测试
2+
3+ 我们可以通过在Gradle添加Java插件来执行单元测试的任务,默认的,在项目中所有的测试都会被执行,如果我们只想测试其中一个类,我们可以使用Java系统属性` test.single ` 作为测试的名字,事实上,这个系统属性的模式是` taskName.single ` ,其中` taskName ` 是我们工程中单元测试类型的名称。以下将会看到我们如何构建单元测试
4+
5+ 1.创建Gradle工程,并在build.gradle配置文件中加入:
6+ ``` Java
7+
8+ // File: build.gradle
9+ apply plugin: ' java'
10+ repositories {
11+ mavenCentral()
12+ }
13+ dependencies {
14+ testCompile ' junit:junit:[4,)'
15+ }
16+ test {
17+ testLogging {
18+ // Show that tests are run in the command-line output
19+ events ' started' , ' passed'
20+ }
21+ }
22+ ```
23+ 2.第二步,我们创建一个测试类,每个测试类一个测试方法,这样子让我们可以在后面单独的调用他们
24+
25+ ``` Java
26+
27+ // File: src/test/java/com/mrhaki/gradle/SampleTest.java
28+ package com.mrhaki.gradle ;
29+
30+ import static org.junit.Assert.* ;
31+ import org.junit.* ;
32+
33+ public class SampleTest {
34+
35+ @Test public void sample () {
36+ assertEquals(" Gradle is gr8" , " Gradle is gr8" );
37+ }
38+
39+ }
40+
41+ // File: src/test/java/com/mrhaki/gradle/AnotherSampleTest.java
42+ package com.mrhaki.gradle ;
43+
44+ import static org.junit.Assert.* ;
45+ import org.junit.* ;
46+
47+ public class AnotherSampleTest {
48+
49+ @Test public void anotherSample () {
50+ assertEquals(" Gradle is great" , " Gradle is great" );
51+ }
52+ }
53+
54+ ```
You can’t perform that action at this time.
0 commit comments