Hi Radim (and thanks to Michael in our team for noting this),
It’s actually your query that needs to be fixed. $FailedTests contains all data values, and you are counting this total number in the last step. You will need to rewrite your query to…
return count( for $FailedTests in $Document/.../data(@value) where $FailedTests = "Failed" return $FailedTests )
…or…
let $FailedTests := $Document/.../TestResults:Outcome[@value = "Failed"] return $FailedTests
Cheers Christian