Skip to content

Commit 5978b37

Browse files
committed
Prepend the class name to the default test name for test class methods.
1 parent fa941ad commit 5978b37

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

doc/examples/testclass.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ def test_a(self):
1212
def test_b(self):
1313
pass
1414

15-
@pytest.mark.dependency(depends=["test_a"])
15+
@pytest.mark.dependency(depends=["TestClass::test_a"])
1616
def test_c(self):
1717
pass
1818

19-
@pytest.mark.dependency(depends=["test_b"])
19+
@pytest.mark.dependency(depends=["TestClass::test_b"])
2020
def test_d(self):
2121
pass
2222

23-
@pytest.mark.dependency(depends=["test_b", "test_c"])
23+
@pytest.mark.dependency(depends=["TestClass::test_b", "TestClass::test_c"])
2424
def test_e(self):
2525
pass
2626

doc/src/usage.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ works in the same manner as the previous examples respectively:
5959

6060
.. literalinclude:: ../examples/testclass.py
6161

62-
In `TestClass` the default names for the tests are used, which is the
63-
name of the respective method in this case, while in `TestClassNamed`
64-
these names are overridden an explicit name argument to the
65-
:func:`pytest.mark.dependency` marker.
62+
In `TestClass` the default names for the tests are used, which is
63+
build from the name of the class and the respective method in this
64+
case, while in `TestClassNamed` these names are overridden by an
65+
explicit name argument to the :func:`pytest.mark.dependency` marker.
6666

6767
Parametrized tests
6868
------------------

pytest_dependency.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ def __init__(self):
5252
def addResult(self, item, marker, rep):
5353
name = marker.kwargs.get('name')
5454
if not name:
55-
name = item.name
55+
if item.cls:
56+
name = "%s::%s" % (item.cls.__name__, item.name)
57+
else:
58+
name = item.name
5659
status = self.results.setdefault(name, DependencyItemStatus())
5760
status.addResult(rep)
5861

tests/test_03_class.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ def test_a(self):
2121
def test_b(self):
2222
pass
2323
24-
@pytest.mark.dependency(depends=["test_a"])
24+
@pytest.mark.dependency(depends=["TestClass::test_a"])
2525
def test_c(self):
2626
pass
2727
28-
@pytest.mark.dependency(depends=["test_b"])
28+
@pytest.mark.dependency(depends=["TestClass::test_b"])
2929
def test_d(self):
3030
pass
3131
32-
@pytest.mark.dependency(depends=["test_b", "test_c"])
32+
@pytest.mark.dependency(depends=["TestClass::test_b",
33+
"TestClass::test_c"])
3334
def test_e(self):
3435
pass
3536
""")
@@ -84,12 +85,15 @@ def test_e(self):
8485
""")
8586

8687

87-
@pytest.mark.xfail(reason="Issue #6")
8888
def test_class_default_name(ctestdir):
89-
"""For methods of test classes, the default name is the method name.
90-
This may cause conflicts if there is a function having the same
91-
name outside the class. Note how the method test_a() of class
92-
TestClass shadows the failure of function test_a().
89+
"""Issue #6: for methods of test classes, the default name used to be
90+
the method name. This could have caused conflicts if there is a
91+
function having the same name outside the class. In the following
92+
example, before fixing this issue, the method test_a() of class
93+
TestClass would have shadowed the failure of function test_a().
94+
95+
Now the class name is prepended to the default test name, removing
96+
this conflict.
9397
"""
9498
ctestdir.makepyfile("""
9599
import pytest

0 commit comments

Comments
 (0)