Skip to content

Commit 9b5f8a7

Browse files
committed
harden test_simple_unittest
1 parent dfab640 commit 9b5f8a7

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

testing/test_unittest.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import gc
22

33
import pytest
4+
from _pytest.compat import TYPE_CHECKING
45
from _pytest.config import ExitCode
5-
from _pytest.pytester import Testdir
66

7+
if TYPE_CHECKING:
8+
from _pytest.pytester import Testdir
79

8-
def test_simple_unittest(testdir):
9-
testpath = testdir.makepyfile(
10+
11+
def test_simple_unittest(testdir: "Testdir") -> None:
12+
p1 = testdir.makepyfile(
1013
"""
1114
import unittest
1215
class MyTestCase(unittest.TestCase):
@@ -16,9 +19,34 @@ def test_failing(self):
1619
self.assertEqual('foo', 'bar')
1720
"""
1821
)
19-
reprec = testdir.inline_run(testpath)
20-
assert reprec.matchreport("testpassing").passed
21-
assert reprec.matchreport("test_failing").failed
22+
result = testdir.runpytest(p1, "-rap")
23+
result.stdout.fnmatch_lines(
24+
[
25+
"collected 2 items",
26+
"",
27+
"test_simple_unittest.py F. *",
28+
"",
29+
"=*= FAILURES =*=",
30+
"_*_ MyTestCase.test_failing _*_",
31+
"",
32+
"self = <test_simple_unittest.MyTestCase testMethod=test_failing>",
33+
"",
34+
" def test_failing(self):",
35+
"> self.assertEqual('foo', 'bar')",
36+
"E AssertionError: 'foo' != 'bar'",
37+
"E - foo",
38+
"E + bar",
39+
"",
40+
"test_simple_unittest.py:6: AssertionError: 'foo' != 'bar'...",
41+
"=*= short test summary info =*=",
42+
"FAILED test_simple_unittest.py:6::MyTestCase::test_failing"
43+
r" - AssertionError: 'foo' != 'bar'\n- foo\n+ bar",
44+
"PASSED test_simple_unittest.py::MyTestCase::testpassing",
45+
"=*= 1 failed, 1 passed in *s =*=",
46+
],
47+
consecutive=True,
48+
)
49+
assert result.ret == 1
2250

2351

2452
def test_runTest_method(testdir):
@@ -744,7 +772,7 @@ class Test(unittest.TestCase):
744772
assert result.ret == ExitCode.NO_TESTS_COLLECTED
745773

746774

747-
def test_order(testdir: Testdir) -> None:
775+
def test_order(testdir: "Testdir") -> None:
748776
"""Test for unittest's behavior to sort tests by default (via TestLoader).
749777
750778
This could be made configurable maybe."""

0 commit comments

Comments
 (0)