Jenkins - How to find out which tests failed repeatedly?

后端 未结 2 772
青春惊慌失措
青春惊慌失措 2021-02-13 12:05

I am new to CI & Jenkins. I have a Java project which runs Testng based automated tests. The tests run regularly as a job in Jenkins. Sometimes, the job fails repeatedly for

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-13 12:25

    You can take the number of times a test failed from the build's object in a pipeline: you can make a small report and attach it to your build like so:

    @NonCPS
    def getRepeatedlyFailingTests(int timesFailedAtLeast = 2) {
      currentBuild.rawBuild.getAction(hudson.tasks.junit.TestResultAction.class)
        .getFailedTests()
        // Keep only tests that failed at least twice
        .findAll { it.age >= timesFailedAtLeast }
        .collect { [ "${it.className}.${it.name}".trim(), it.age ] }
    }
    
    def saveRepeatedlyFailingTestsReport() {
      def header = [ "test", "times-failed" ]
      def records = getRepeatedlyFailingTests()
      def report = "repeatedly-failing-tests.csv"
      writeCSV file: report, records: [ header ] + records, format: CSVFormat.EXCEL
      archiveArtifacts report
    }
    

    Call saveRepeatedlyFailingTestsReport after you gather your test results and you'll see the report in your latest build artifacts link.

提交回复
热议问题