Skip to content

Commit 7f4f5b8

Browse files
authored
Skip tests that depend on lxml if not installed (#12813)
Detect if lxml is importable in the test suite, if it is not, then skip the report tests which depend on it. This is useful for enabling CI on new Python versions that may not have lxml wheels yet. Closes #11662.
1 parent f71dba7 commit 7f4f5b8

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

mypy/test/testcheck.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
from mypy.errors import CompileError
2222
from mypy.semanal_main import core_modules
2323

24+
try:
25+
import lxml # type: ignore
26+
except ImportError:
27+
lxml = None
28+
29+
import pytest
2430

2531
# List of files that contain test case descriptions.
2632
typecheck_files = [
@@ -117,6 +123,8 @@ class TypeCheckSuite(DataSuite):
117123
files = typecheck_files
118124

119125
def run_case(self, testcase: DataDrivenTestCase) -> None:
126+
if lxml is None and os.path.basename(testcase.file) == 'check-reports.test':
127+
pytest.skip("Cannot import lxml. Is it installed?")
120128
incremental = ('incremental' in testcase.name.lower()
121129
or 'incremental' in testcase.file
122130
or 'serialize' in testcase.file)

mypy/test/testcmdline.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
assert_string_arrays_equal, normalize_error_messages, check_test_output_files
1919
)
2020

21+
try:
22+
import lxml # type: ignore
23+
except ImportError:
24+
lxml = None
25+
26+
import pytest
27+
2128
# Path to Python 3 interpreter
2229
python3_path = sys.executable
2330

@@ -35,6 +42,8 @@ class PythonCmdlineSuite(DataSuite):
3542
native_sep = True
3643

3744
def run_case(self, testcase: DataDrivenTestCase) -> None:
45+
if lxml is None and os.path.basename(testcase.file) == 'reports.test':
46+
pytest.skip("Cannot import lxml. Is it installed?")
3847
for step in [1] + sorted(testcase.output2):
3948
test_python_cmdline(testcase, step)
4049

mypy/test/testreports.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
from mypy.report import CoberturaPackage, get_line_rate
66

77

8+
try:
9+
import lxml # type: ignore
10+
except ImportError:
11+
lxml = None
12+
13+
import pytest
14+
15+
816
class CoberturaReportSuite(Suite):
17+
@pytest.mark.skipif(lxml is None, reason="Cannot import lxml. Is it installed?")
918
def test_get_line_rate(self) -> None:
1019
assert_equal('1.0', get_line_rate(0, 0))
1120
assert_equal('0.3333', get_line_rate(1, 3))
1221

22+
@pytest.mark.skipif(lxml is None, reason="Cannot import lxml. Is it installed?")
1323
def test_as_xml(self) -> None:
1424
import lxml.etree as etree # type: ignore
1525

0 commit comments

Comments
 (0)