How do I make Jenkins build fail when Maven unit tests fail?

后端 未结 3 2054
粉色の甜心
粉色の甜心 2020-12-08 13:06

I\'m using Jenkins, Maven 3.1, and Java 1.6. I have the following Maven job set up in Jenkins with the following goals and options ...

clean install -U -P c         


        
3条回答
  •  北海茫月
    2020-12-08 13:40

    Another hack that can be useful is to use groovy post-build to check and set test result.

    e.g. this groovy gets build result, appends useful things to build description and sets build result to UNSTABLE in the case that there are no pass or fail but all tests skipped.

    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)
    

提交回复
热议问题