Teamcity build to fail if number of failed tests is more than N% of total tests

Viewed 172

Is that possible to configure a Teamcity build to fail if number of failed tests is more than N% of the passed tests in this build?

I've only discovered an option to fail based on a metric comparing current build against some previous one: enter image description here But I need to fail a build based on fail/passed test ratio within the same build.

1 Answers

It can be done by using TeamCity REST API to get failed/passed values, Configuration Parameters and Service Messages for controlling build status.

  1. turn off 'fail if at least one test fails' in build 'failure conditions'
  2. add threshold(%), tc_url and tc_token as build parameters.
  3. add a new build step using the script below (or equivalent)
#!/bin/bash


# set threshold from tc parameters
threshold=%threshold%

# get test results from teamcity API, build# from %teamcity.build.id% param
curl -s -X GET "%tc_url%/app/rest/builds/%teamcity.build.id%/testOccurrences?locator=count:1,status:failed,status:passed" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer %tc_token%" \
    -o test_results.json & wait $! # wait for download to finish

# read
passed=`cat test_results.json | jq '.passed'`
failed=`cat test_results.json | jq '.failed'`

# calc
total=`expr $passed + $failed`
failPercent=`echo "scale=0; 100 * $failed / $total" | bc`

# fail build if failPercent > threshold
if [ $failPercent -gt $threshold ]; then
    # fail build using service message
    echo "##teamcity[buildStatus status='FAILURE' text='$failPercent% of tests failed (>$threshold%)']"
fi
Related