1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
"""Usage with test classes.
"""
import pytest
def test_class_simple(ctestdir):
"""Simple dependencies of test methods in a class.
test_a() deliberately fails, some other methods depend on it, some don't.
"""
ctestdir.makepyfile("""
import pytest
class TestClass(object):
@pytest.mark.dependency()
def test_a(self):
assert False
@pytest.mark.dependency()
def test_b(self):
pass
@pytest.mark.dependency(depends=["TestClass::test_a"])
def test_c(self):
pass
@pytest.mark.dependency(depends=["TestClass::test_b"])
def test_d(self):
pass
@pytest.mark.dependency(depends=["TestClass::test_b",
"TestClass::test_c"])
def test_e(self):
pass
""")
result = ctestdir.runpytest("--verbose")
result.assert_outcomes(passed=2, skipped=2, failed=1)
result.stdout.re_match_lines(r"""
.*::TestClass::test_a FAILED
.*::TestClass::test_b PASSED
.*::TestClass::test_c SKIPPED(?:\s+\(.*\))?
.*::TestClass::test_d PASSED
.*::TestClass::test_e SKIPPED(?:\s+\(.*\))?
""")
def test_class_simple_named(ctestdir):
"""Mostly the same as test_class_simple(), but name the test methods
now explicitely.
"""
ctestdir.makepyfile("""
import pytest
class TestClassNamed(object):
@pytest.mark.dependency(name="a")
def test_a(self):
assert False
@pytest.mark.dependency(name="b")
def test_b(self):
pass
@pytest.mark.dependency(name="c", depends=["a"])
def test_c(self):
pass
@pytest.mark.dependency(name="d", depends=["b"])
def test_d(self):
pass
@pytest.mark.dependency(name="e", depends=["b", "c"])
def test_e(self):
pass
""")
result = ctestdir.runpytest("--verbose")
result.assert_outcomes(passed=2, skipped=2, failed=1)
result.stdout.re_match_lines(r"""
.*::TestClassNamed::test_a FAILED
.*::TestClassNamed::test_b PASSED
.*::TestClassNamed::test_c SKIPPED(?:\s+\(.*\))?
.*::TestClassNamed::test_d PASSED
.*::TestClassNamed::test_e SKIPPED(?:\s+\(.*\))?
""")
def test_class_default_name(ctestdir):
"""Issue #6: for methods of test classes, the default name used to be
the method name. This could have caused conflicts if there is a
function having the same name outside the class. In the following
example, before fixing this issue, the method test_a() of class
TestClass would have shadowed the failure of function test_a().
Now the class name is prepended to the default test name, removing
this conflict.
"""
ctestdir.makepyfile("""
import pytest
@pytest.mark.dependency()
def test_a():
assert False
class TestClass(object):
@pytest.mark.dependency()
def test_a(self):
pass
@pytest.mark.dependency(depends=["test_a"])
def test_b():
pass
""")
result = ctestdir.runpytest("--verbose")
result.assert_outcomes(passed=1, skipped=1, failed=1)
result.stdout.re_match_lines(r"""
.*::test_a FAILED
.*::TestClass::test_a PASSED
.*::test_b SKIPPED(?:\s+\(.*\))?
""")
|