2015-02-23 294 views
38

我正在使用Jenkins,Maven 3.1和Java 1.6。我有以下Maven的工作建立在詹金斯與以下目標和選項...當Maven單元測試失敗時,如何讓Jenkins構建失敗?

clean install -U -P cloudbees -P qa 
下面

是我的pom.xml萬無一失的配置...

<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.17</version> 
     <configuration> 
       <reuseForks>true</reuseForks> 
       <argLine>-Xmx2048m -XX:MaxPermSize=512M </argLine> 
       <skipTests>false</skipTests> 
     </configuration> 
</plugin> 

然而,當我的單元測試失敗後,Jenkins控制檯輸出仍然會顯示「BUILD SUCCESS」,並且構建標記爲「unstable」而不是徹底失敗。如何在Jenkins中配置事物(或Maven如果事實如此),這樣如果任何單元測試失敗,我的構建失敗(不會變得不穩定或通過)?

下面就是控制檯輸出說

17:08:04 MyProjectOrganizationControllerTest.testRecoverFromError » IllegalState Failed to... 
17:08:04 MyProjectOrganizationControllerTest.testVerifyDistrictListPopulated » IllegalState 
17:08:04 MyProjectOrganizationControllerTest.testUpdateSchool » IllegalState Failed to loa... 
17:08:04 MyProjectOrganizationControllerTest.testDeleteSchool » IllegalState Failed to loa... 
17:08:04 MyProjectOrganizationControllerTest.testVerifyOrgListPopulatedPrivateSchoolOrgType » IllegalState 
17:08:04 MyProjectOrganizationControllerTest.testSubmitMultipleParams » IllegalState Faile... 
17:08:04 
17:08:04 Tests run: 155, Failures: 0, Errors: 154, Skipped: 1 
17:08:04 
17:08:04 [ERROR] There are test failures. 
17:08:04 
17:08:04 Please refer to /scratch/jenkins/workspace/MyProject/MyProject/target/surefire-reports for the individual test results. 
17:08:04 [JENKINS] Recording test results 
17:08:07 log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter). 
17:08:07 log4j:WARN Please initialize the log4j system properly. 
17:08:14 [INFO] 
17:08:14 [INFO] --- maven-war-plugin:2.4:war (default-war) @ MyProject --- 
17:08:15 [INFO] Packaging webapp 
17:08:15 [INFO] Assembling webapp [MyProject] in [/scratch/jenkins/workspace/MyProject/MyProject/target/MyProject] 
17:08:15 [INFO] Processing war project 
17:08:15 [INFO] Copying webapp resources [/scratch/jenkins/workspace/MyProject/MyProject/src/main/webapp] 
17:08:15 [INFO] Webapp assembled in [662 msecs] 
17:08:15 [INFO] Building war: /scratch/jenkins/workspace/MyProject/MyProject/target/MyProject.war 
17:08:20 [INFO] 
17:08:20 [INFO] --- maven-failsafe-plugin:2.17:integration-test (default) @ MyProject --- 
17:08:20 [JENKINS] Recording test results 
17:08:25 [INFO] 
17:08:25 [INFO] --- maven-failsafe-plugin:2.17:verify (default) @ MyProject --- 
17:08:25 [INFO] Failsafe report directory: /scratch/jenkins/workspace/MyProject/MyProject/target/failsafe-reports 
17:08:25 [JENKINS] Recording test results[INFO] 
17:08:25 [INFO] --- maven-install-plugin:2.4:install (default-install) @ MyProject --- 
17:08:25 
17:08:25 [INFO] Installing /scratch/jenkins/workspace/MyProject/MyProject/target/MyProject.war to /home/jenkins/.m2/repository/org/mainco/subco/MyProject/76.0.0-SNAPSHOT/MyProject-76.0.0-SNAPSHOT.war 
17:08:25 [INFO] Installing /scratch/jenkins/workspace/MyProject/MyProject/pom.xml to /home/jenkins/.m2/repository/org/mainco/subco/MyProject/76.0.0-SNAPSHOT/MyProject-76.0.0-SNAPSHOT.pom 
17:08:26 Started calculate disk usage of build 
17:08:26 Finished Calculation of disk usage of build in 0 seconds 
17:08:26 Started calculate disk usage of workspace 
17:08:26 Finished Calculation of disk usage of workspace in 0 seconds 
17:08:26 [INFO] ------------------------------------------------------------------------ 
17:08:26 [INFO] BUILD SUCCESS 
17:08:26 [INFO] ------------------------------------------------------------------------ 
17:08:26 [INFO] Total time: 11:00.616s 
17:08:26 [INFO] Finished at: Mon Feb 23 17:08:26 UTC 2015 
17:08:27 [INFO] Final Memory: 90M/414M 
17:08:27 [INFO] ------------------------------------------------------------------------ 
17:08:27 Waiting for Jenkins to finish collecting data 
17:08:28 [JENKINS] Archiving /scratch/jenkins/workspace/MyProject/MyProject/pom.xml to org.mainco.subco/MyProject/76.0.0-SNAPSHOT/MyProject-76.0.0-SNAPSHOT.pom 
17:08:28 [JENKINS] Archiving /scratch/jenkins/workspace/MyProject/MyProject/target/MyProject.war to org.mainco.subco/MyProject/76.0.0- SNAPSHOT/MyProject-76.0.0-SNAPSHOT.war 
17:08:31 channel stopped 
17:08:31 Started calculate disk usage of build 
17:08:31 Finished Calculation of disk usage of build in 0 seconds 
17:08:31 Started calculate disk usage of workspace 
17:08:31 Finished Calculation of disk usage of workspace in 0 seconds 
17:08:31 Finished: UNSTABLE 

回答

38

您可以添加-Dmaven.test.failure.ignore=falseMAVEN_OPTS如果您在詹金斯工作的Build部分點擊高級按鈕。

請參閱Maven Surefire Plugin - surefire:test供參考。

+2

但是我們是否真的必須將此標誌指定爲false?根據我的理解,如果我們不指定這個單位,那麼默認值只是「FALSE」。請建議。謝謝! – OverrockSTAR 2015-11-09 03:51:49

+9

Jenkins在默認情況下將此值指定爲true。看看https://issues.jenkins-ci.org/browse/JENKINS-24655 – Torsten 2015-11-09 10:47:44

+0

謝謝你,你是對的。它爲我工作! – OverrockSTAR 2015-11-20 18:01:25

5

使用Text Finder插件。配置它來尋找There are test failures和降級構建以FAILED

+2

不錯的破解,但破解不到。 – Alexey 2016-07-20 12:14:04

+0

除了被黑客攻擊之外,我發現它沒能將UNSTABLE降級爲失敗的版本。我認爲這可能與記錄的「重分類僅適用於返回總體退出狀態爲零的構建」有關。 – bdeniker 2016-09-06 10:48:32

0

另一個有用的黑客技術是使用groovy post-build來檢查和設置測試結果。

例如這個groovy獲取構建結果,附加有用的東西來構建描述,並在沒有通過或失敗的情況下將構建結果設置爲UNSTABLE,但所有測試均被跳過。

def currentBuild = Thread.currentThread().executable 
// must be run groovy post-build action AFTER harvest junit xml if you use junit xml test results 
testResult1 = currentBuild.testResultAction 

currentBuild.setDescription(currentBuild.getDescription() + "\n pass:"+testResult1.result.passCount.toString()+", fail:"+testResult1.result.failCount.toString()+", skip:"+testResult1.result.skipCount.toString()) 

// if no pass, no fail all skip then set result to unstable 
if (testResult1.result.passCount == 0 && testResult1.result.failCount == 0 && testResult1.result.skipCount > 0) { 
    currentBuild.result = hudson.model.Result.UNSTABLE 
} 

currentBuild.setDescription(currentBuild.getDescription() + "\n" + currentBuild.result.toString()) 

def ws = manager.build.workspace.getRemote() 
myFile = new File(ws + "/VERSION.txt") 
desc = myFile.readLines() 
currentBuild.setDescription(currentBuild.getDescription() + "\n" + desc)