Skip to content

Commit

Permalink
Skip tests that depend on lxml if not installed (#12813)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ethanhs authored May 19, 2022
1 parent f71dba7 commit 7f4f5b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
from mypy.errors import CompileError
from mypy.semanal_main import core_modules

try:
import lxml # type: ignore
except ImportError:
lxml = None

import pytest

# List of files that contain test case descriptions.
typecheck_files = [
Expand Down Expand Up @@ -117,6 +123,8 @@ class TypeCheckSuite(DataSuite):
files = typecheck_files

def run_case(self, testcase: DataDrivenTestCase) -> None:
if lxml is None and os.path.basename(testcase.file) == 'check-reports.test':
pytest.skip("Cannot import lxml. Is it installed?")
incremental = ('incremental' in testcase.name.lower()
or 'incremental' in testcase.file
or 'serialize' in testcase.file)
Expand Down
9 changes: 9 additions & 0 deletions mypy/test/testcmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
assert_string_arrays_equal, normalize_error_messages, check_test_output_files
)

try:
import lxml # type: ignore
except ImportError:
lxml = None

import pytest

# Path to Python 3 interpreter
python3_path = sys.executable

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

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

Expand Down
10 changes: 10 additions & 0 deletions mypy/test/testreports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
from mypy.report import CoberturaPackage, get_line_rate


try:
import lxml # type: ignore
except ImportError:
lxml = None

import pytest


class CoberturaReportSuite(Suite):
@pytest.mark.skipif(lxml is None, reason="Cannot import lxml. Is it installed?")
def test_get_line_rate(self) -> None:
assert_equal('1.0', get_line_rate(0, 0))
assert_equal('0.3333', get_line_rate(1, 3))

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

Expand Down

0 comments on commit 7f4f5b8

Please sign in to comment.