測試插件-infinitest介紹

緣起

寫多了業務代碼,一些遺留系統裏處於基本沒有單測的狀態,所以最近對 TDD 的開發方式很感興趣,看了很多 TDD 介紹和實踐的書。git

TDD 對測試的執行次數有很高的要求,可是日常在 idea 裏面寫代碼運行測試所需時間較長,須要先 build 才能啓動測試,這就成了咱們踐行 TDD 的障礙,咱們須要一款對測試友好的插件來幫助咱們。github

infinitest 插件可讓咱們免除手動執行測試的尷尬,它檢測文件的 class 變更,當文件有變更時,能夠自動執行測試,開箱即用。infinitest 也可使用簡單的配置,讓咱們把 infinitest 和 Junit 相關特性結合起來,更顯方便正則表達式

安裝與使用

安裝很簡單:框架

Settings -> Plugins -> Browse repositories -> 查找infinitest -> Install -> Restart ideaide

添加到項目中:測試

Project Structure(ctrl+shift+alt+s) -> Modules -> 添加infinitestui

idea 默認不會自動 compile文件,咱們須要開啓一下idea

Settings -> Build, Execution, Deployment -> Compiler -> Build project automatically 打上勾插件

若是項目中只有簡單的測試,這樣就能夠了,若是咱們須要區分不一樣的測試,咱們須要本身添加文件來配置一下。code

配置

總的文檔目錄 infinitest docs

filters

這個配置可讓咱們指定須要執行哪些測試,不執行哪些測試,支持 Junit4 的 category 和 Junit5 的 tags

  • 添加配置文件

本身新建一個infinitest.filters, 將文件放在.iml同級目錄下便可。

  • 經過 class name,支持正則表達式
# Include tests that end in ITest or TestCase
include .*ITest, .*TestCase

# Exclude Inner Classes
exclude .*\$.*

# Include tests in package com.mycompany.mypackage and sub-packages
include com\.mycompany\.mypackage\..*

# Include tests in package com.mycompany.mypackage and not in sub-packages
include com\.mycompany\.mypackage\.[^.]*
  • 經過 Junit5 的 tags
# Include tests with "Fast" and "Regression" tags
includeGroups Fast, Regression

# Exclude tests with "Slow" tag
excludeGroups Slow
  • 經過 Junit4 的 category
# Include tests in FastTests and RegressionTests categories
includeGroups com.example.FastTests, com.example.RegressionTests

# Exclude SlowTests category
excludeGroups com.example.SlowTests

testNg 框架相關的不作介紹,具體能夠參見 infinitest filters

JVM options

新建infinitest.args文件,位置與filters的同樣,裏面每一行是一個虛擬機參數,這個配置會覆蓋 infinitest 的默認配置

最後

你們若是遇到什麼問題能夠先 Google 一下,若是沒有找到答案,能夠在 GitHub 上自行提問。

相關文章
相關標籤/搜索