Skip to content

Commit becaf8a

Browse files
authored
Add flag for expected test failures, so we can get rid of the nasty seperate pytest run and stuff. (#56)
1 parent 170b21d commit becaf8a

File tree

7 files changed

+48
-32
lines changed

7 files changed

+48
-32
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ jobs:
2626
pip install -U pip setuptools wheel
2727
pip install -r dev-requirements.txt
2828
- name: Run tests
29-
run: pytest --ignore-glob="*.shouldfail.yml"
30-
- name: Run test with expected failures
31-
run: pytest pytest_mypy_plugins/tests/*.shouldfail.yml 2>&1 | grep "5 failed"
29+
run: pytest
3230

3331
lint:
3432
runs-on: ubuntu-latest

pytest_mypy_plugins/collect.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def collect(self) -> Iterator["YamlTestItem"]:
101101
test_name = f"{test_name_prefix}{test_name_suffix}"
102102
main_file = File(path="main.py", content=pystache.render(raw_test["main"], params))
103103
test_files = [main_file] + parse_test_files(raw_test.get("files", []))
104+
expect_fail = raw_test.get("expect_fail", False)
104105
regex = raw_test.get("regex", False)
105106

106107
expected_output = []
@@ -130,6 +131,7 @@ def collect(self) -> Iterator["YamlTestItem"]:
130131
expected_output=expected_output,
131132
parsed_test_data=raw_test,
132133
mypy_config=additional_mypy_config,
134+
expect_fail=expect_fail,
133135
)
134136

135137
def _eval_skip(self, skip_if: str) -> bool:

pytest_mypy_plugins/item.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ def __init__(
130130
disable_cache: bool,
131131
mypy_config: str,
132132
parsed_test_data: Dict[str, Any],
133+
expect_fail: bool,
133134
) -> None:
134135
super().__init__(name, parent, config)
135136
self.files = files
136137
self.environment_variables = environment_variables
137138
self.disable_cache = disable_cache
139+
self.expect_fail = expect_fail
138140
self.expected_output = expected_output
139141
self.starting_lineno = starting_lineno
140142
self.additional_mypy_config = mypy_config
@@ -280,7 +282,15 @@ def runtest(self) -> None:
280282
for line in mypy_output.splitlines():
281283
output_line = replace_fpath_with_module_name(line, rootdir=execution_path)
282284
output_lines.append(output_line)
283-
assert_expected_matched_actual(expected=self.expected_output, actual=output_lines)
285+
try:
286+
assert_expected_matched_actual(expected=self.expected_output, actual=output_lines)
287+
except TypecheckAssertionError as e:
288+
if not self.expect_fail:
289+
raise e
290+
else:
291+
if self.expect_fail:
292+
raise TypecheckAssertionError("Expected failure, but test passed")
293+
284294
finally:
285295
temp_dir.cleanup()
286296
# remove created modules and all their dependants from cache

pytest_mypy_plugins/tests/test-regex_assertions.shouldfail.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

pytest_mypy_plugins/tests/test-regex_assertions.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@
2020
main: |
2121
a = 'hello'
2222
reveal_type(a) # NR: .*str.*
23+
24+
- case: rexex_but_not_turned_on
25+
expect_fail: yes
26+
main: |
27+
a = 'hello'
28+
reveal_type(a) # N: .*str.*
29+
30+
- case: rexex_but_turned_off
31+
expect_fail: yes
32+
regex: no
33+
main: |
34+
a = 'hello'
35+
reveal_type(a) # N: .*str.*
36+
37+
- case: regext_does_not_match
38+
expect_fail: yes
39+
regex: no
40+
main: |
41+
a = 'hello'
42+
reveal_type(a) # NR: .*banana.*

pytest_mypy_plugins/tests/test-simple-cases.shouldfail.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

pytest_mypy_plugins/tests/test-simple-cases.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,17 @@
8888
main: |
8989
a = 1
9090
a.lower() # E: "int" has no attribute "lower"
91+
92+
- case: fail_if_message_does_not_match
93+
expect_fail: yes
94+
main: |
95+
a = 'hello'
96+
reveal_type(a) # N: Some other message
97+
98+
- case: fail_if_message_from_outdoes_not_match
99+
expect_fail: yes
100+
main: |
101+
a = 'abc'
102+
reveal_type(a)
103+
out: |
104+
main:2: note: Some other message

0 commit comments

Comments
 (0)