- Newest
- Most votes
- Most comments
Some good and bad news.
Since CodeBuild does not natively have custom behaviour based on test/coverage results data (from the files), some custom scripting is required.
Install xq (since the result formats - Visual Studio TRX, Cobertura - are XML). After the files are exported out of Docker build, execute scripts during post_build phase to query the results and pass/fail (with exit code) according to conditions.
Failed tests
testfailcount=$(xq xunit.trx -x /TestRun/ResultSummary/Counters/@failed)
echo "$testfailcount tests failed."
if [ $testfailcount -gt 0 ]; then
exit $testfailcount
fi
Code coverage percentage
coverrequired=90
coverdecimal=$(xq Cobertura.xml -x /coverage/@line-rate)
coverpercent=$(awk -v dec="$coverdecimal" 'BEGIN { printf "%.*f", 0, dec * 100 }')
echo "$coverpercent% code coverage."
if [ $coverpercent -lt $coverrequired ]; then
echo "$coverrequired% coverage required."
exit 1
fi
The scripts do their job for their intended scope. HOWEVER because they fail CodeBuild, the report files are not finally ingested for report presentation in the CodeBuild web console. So in order to view the visual reports, CodeBuild has to pass even though it ought to fail.
UPDATE
CodeBuild can ingest report files even on failure. The "problem" was the post_build phase was set to ABORT on failure. Both build and post_build must be defaulted to continue so that they can transition forward to upload_artifacts and finalizing phases where report ingestion happens.
Example buildspec
build:
commands:
- docker buildx build --target export -o type=local,dest=buildreports .
post_build:
commands:
- chmod +x buildscripts/*
- cd buildreports
- ../buildscripts/testfailcount.sh
- ../buildscripts/coverpercent.sh
reports:
tests:
files:
- xunit.trx
base-directory: buildreports
file-format: VISUALSTUDIOTRX
coverage:
files:
- Cobertura.xml
base-directory: buildreports
file-format: COBERTURAXML
Hi, maybe this will help: https://stackoverflow.com/questions/72995682/how-to-fail-codebuild-if-testing-coverage-is-below-a-threshold
Relevant content
- asked 2 years ago
- Accepted Answerasked 6 months ago
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
His question was about how to make CodeBuild per se plain fail/stop, and not about how to peruse data from test/coverage results to determine if CodeBuild should pass/fail.