Testing with Gradle

Three types of Test

  • Unit test: testing the smallest unit of your code
  • Integration test: test an entire component or subsystem
  • Functional test: end-to-end functionality

Testing Java applications

To execute tests, you'll need to compile test code first. Optimally, you'll have a dedicated directory to hold the source code, and another to hold compiled test classes.

Project layout

src/test/java
src/test/resources
You can also find test results XML under build/test-results. Many test framework will transform them into reports, which can be found under build/reports/test. You need to add dependency if you want to use these frameworks, such as JUnit or TestNG.

Test configurations

There are two new configurations that can be used to declare dependencies for test code compilation or execution: testCompile and testRuntime.
An example declares a compile-time dependency JUnit.

dependencies {
  testCompile 'junit:junit:4.11'
}

Another configuration testRuntime is used for dependencies not needed for compilation but needed at runtime during execution.
Keep in mind that dependencies
assigned to test configurations don’t influence the classpath of your production code.
In other words, they’re not used for the compilation or packaging process.
However, the test configurations extend from specific configuration to handle dependencies which are needed for production code.

Test tasks

compileJava -> processResources -> classes -> jar -> compileTestJava -> processTestResources -> testClasses -> test

Tasks about test may be UP-TO-DATE to skip if there is no test source code.
And if you run 'gradle jar', the test tasks will also be skipped.

Test detection

How does Gradle figure out which classes to run?
Compiled test classes are in build/classes/test. Classes matching the following descriptions are inspected:

  1. Any class or superclass that extends junit.framework.TestCase or groovy.util.GroovyTestCase
  2. Any class or superclass annotated with @RunWith
  3. Any class or superclass that contains one method annotated with @Test

Unit test

You can use JUnit or TestNG, or Spock.

Using JUnit

Adding the dependency

project(':repository') {
   repositories {
      mavenCentral()
   }
   dependencies {
      compile project(':model')
      testCompile 'junit:junit:4.11'
   } 
}

'testCompile' means JUnit can be used to compile the test source files.

Writing unit tests

public class InMemoryToDoRepositoryTest {
   private ToDoRepository inMemoryToDoRepository;
   @Before
   public void setUp() {
   inMemoryToDoRepository = new InMemoryToDoRepository();
  }
  @Test
  public void insertToDoItem() {
   ToDoItem newToDoItem = new ToDoItem();
   newToDoItem.setName("Write unit tests");
   Long newId = inMemoryToDoRepository.insert(newToDoItem);
   ToDoItem persistedToDoItem = inMemoryToDoRepository.findById(newId);
   assertNotNull(persistedToDoItem);
   assertEquals(newToDoItem, persistedToDoItem);
  }
 }

Executing tests

The task test will first compile the production source, and then create the JAR file followed by test sources compilation and test execution.

Using other test frameworks

Using TestNG

You need to

  1. add dependency
  2. Specify that TestNG should be used to execute tests by calling the method test.useTestNG().
    The build script will look like this
apply plugin: 'groovy'
dependencies {
    compile project(':model')
    testCompile group: 'org.testng', name: 'testng', version: '6.8'
}
test.useTestNG()
repositories {
    mavenCentral()
}

JUnit tests will not run anymore.

Using both JUnit and TestNG

Defining Test Tasks
We can add a new task of class type "Test" named "TestNG"

task TestNG (type: Test) {
    useTestNG()
}
test.dependsOn TestNG

In this way, every time we run gradle test will run task TestNG firstly, which enables JUnit and TestNG running simultaneously.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市昭齐,隨后出現(xiàn)的幾起案子羡洛,更是在濱河造成了極大的恐慌峻呛,老刑警劉巖糟袁,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件调俘,死亡現(xiàn)場離奇詭異伶棒,居然都是意外死亡,警方通過查閱死者的電腦和手機彩库,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進(jìn)店門肤无,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人骇钦,你說我怎么就攤上這事宛渐。” “怎么了眯搭?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵窥翩,是天一觀的道長。 經(jīng)常有香客問我鳞仙,道長寇蚊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任棍好,我火速辦了婚禮仗岸,結(jié)果婚禮上允耿,老公的妹妹穿的比我還像新娘。我一直安慰自己爹梁,他們只是感情好右犹,可當(dāng)我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著姚垃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪盼忌。 梳的紋絲不亂的頭發(fā)上积糯,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天,我揣著相機與錄音谦纱,去河邊找鬼看成。 笑死,一個胖子當(dāng)著我的面吹牛跨嘉,可吹牛的內(nèi)容都是我干的川慌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼祠乃,長吁一口氣:“原來是場噩夢啊……” “哼梦重!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起亮瓷,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤琴拧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后嘱支,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蚓胸,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年除师,在試婚紗的時候發(fā)現(xiàn)自己被綠了沛膳。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡汛聚,死狀恐怖锹安,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情贞岭,我是刑警寧澤八毯,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站瞄桨,受9級特大地震影響话速,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芯侥,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一泊交、第九天 我趴在偏房一處隱蔽的房頂上張望乳讥。 院中可真熱鬧,春花似錦廓俭、人聲如沸云石。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽汹忠。三九已至,卻和暖如春雹熬,著一層夾襖步出監(jiān)牢的瞬間宽菜,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工竿报, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留铅乡,地道東北人。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓烈菌,卻偏偏與公主長得像阵幸,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子芽世,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理挚赊,服務(wù)發(fā)現(xiàn),斷路器捂襟,智...
    卡卡羅2017閱讀 134,696評論 18 139
  • 生活在北方內(nèi)陸的我咬腕,小的時候?qū)τ隰~的吃法只局限于兩種:燉和煎,無論是什么魚都只這兩種做法... 對#水煮魚# 最初...
    Echo的小宇宙閱讀 304評論 6 5
  • 已經(jīng)不在生活在擁有貴族的時代举反,文化的紐帶不應(yīng)成為人交往的瓶頸。但時常路過上海的文化走廊和藝術(shù)博物館扒吁,裊裊的人煙仿佛...
    簡書八閩閱讀 412評論 0 0
  • 今天第一次在安農(nóng)表演火鼻,一支學(xué)了很短時間的舞,雖然最后跳的沒有彩排和訓(xùn)練好雕崩,但是還是挺開心了魁索,可以休息一下了,而且其...
    皆為序幕閱讀 126評論 0 0
  • 這段時間,停播的節(jié)目有點多饶火。 如火如荼的中國綜藝市場被總局澆了一盆涼水鹏控,帶冰的致扯! 《金星秀》停了,《極限挑戰(zhàn)》停了...
    FAN間志閱讀 1,045評論 0 1