Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing lines-covered and lines-valid attributes #17738

Merged
merged 8 commits into from
Sep 15, 2024
2 changes: 2 additions & 0 deletions mypy/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ def on_finish(self) -> None:
self.root_package.covered_lines, self.root_package.total_lines
)
self.root.attrib["branch-rate"] = "0"
self.root.attrib["lines-covered"] = str(self.root_package.covered_lines)
self.root.attrib["lines-valid"] = str(self.root_package.total_lines)
sources = etree.SubElement(self.root, "sources")
source_element = etree.SubElement(sources, "source")
source_element.text = os.getcwd()
Expand Down
16 changes: 15 additions & 1 deletion mypy/test/testreports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from __future__ import annotations

import tempfile
import textwrap

from mypy.report import CoberturaPackage, get_line_rate
from mypy.report import CoberturaPackage, CoberturaXmlReporter, Reports, get_line_rate
from mypy.test.helpers import Suite, assert_equal

try:
Expand Down Expand Up @@ -53,3 +54,16 @@ def test_as_xml(self) -> None:
assert_equal(
expected_output, etree.tostring(cobertura_package.as_xml(), pretty_print=True)
)
with tempfile.TemporaryDirectory() as tempdir:
reports = Reports(data_dir=tempdir, report_dirs={"dummy_report": tempdir})

cobertura_reporter = CoberturaXmlReporter(reports, ".")
cobertura_reporter.root_package = cobertura_package
cobertura_reporter.on_finish()

# Convert the XML document to a string
xml_str = etree.tostring(cobertura_reporter.doc, pretty_print=True).decode("utf-8")

# Check that the required attributes are present
assert f'lines-covered="{cobertura_package.covered_lines}"' in xml_str
assert f'lines-valid="{cobertura_package.total_lines}"' in xml_str
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're testing a separate thing here. It's usually best to follow the AAA principle — checking one single thing per test: https://automationpanda.com/2020/07/07/arrange-act-assert-a-pattern-for-writing-good-tests/.
Hence, it's best to add new test cases unless you change the aspects of the existing ones. Just don't pile on extra logic. The tests must remain simple.

It's probably not a good idea to check substrings in XML text. No need to check if Python's XML writer works. The goal is to check that the reported produces the object having proper XML attributes. It's easier to check it against the object representation.

OTOH, looking closer, I think that the only change to really needed is changing that expected XML string on line 40. That test already checks everything needed, but you changed the produced output, so this test will keep failing until you adjust the expected value to match that new output shape.

Loading