XML report does not have condition-coverage attribute for lines with a branch · Issue #81 · nedbat/coveragepy (original) (raw)

Originally reported by Kamil Kisiel (Bitbucket: kisielk, GitHub: kisielk)


I'm trying to get branch coverage working with Cobertura plugin for Hudson. However, it appears the XML output from coverage.py is not complete as the tags which have the branch="true" attribute don't have a condition-coverage to indicate what the coverage level of the branch is.

Apparently this is required by Cobertura (or at least the Hudson plugin) to correctly generate the coverage report for conditionals.

For reference, here's the code from the plugin which looks at branch coverage:

else if ("line".equals(qName)) {
            String hitsString = attributes.getValue("hits");
            String lineNumber = attributes.getValue("number");
            int denominator = 0;
            int numerator = 0;
            if (Boolean.parseBoolean(attributes.getValue("branch"))) {
                final String conditionCoverage = attributes.getValue("condition-coverage");
                if (conditionCoverage != null) {
                    // some cases in the wild have branch = true but no condition-coverage attribute

                    // should be of the format xxx% (yyy/zzz)
                    Matcher matcher = Pattern.compile("(\\d*)\\%\\s*\\((\\d*)/(\\d*)\\)").matcher(conditionCoverage);
                    if (matcher.matches()) {
                        assert matcher.groupCount() == 3;
                        final String numeratorStr = matcher.group(2);
                        final String denominatorStr = matcher.group(3);
                        try {
                            numerator = Integer.parseInt(numeratorStr);
                            denominator = Integer.parseInt(denominatorStr);
                            rootCoverage.updateMetric(CoverageMetric.CONDITIONAL, Ratio.create(numerator, denominator));
                        } catch (NumberFormatException e) {
                            // ignore
                        }
                    }
                }
            }