Skip to content

Commit 5ab6436

Browse files
committed
Add a documentation page explaining how pytest node ids are built and
how dependencies must be referenced depending on the scope.
1 parent bab2346 commit 5ab6436

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

doc/examples/nodeid.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
$ pytest --verbose
2+
============================= test session starts ==============================
3+
platform linux -- Python 3.8.1, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
4+
cachedir: .pytest_cache
5+
rootdir: /home/user
6+
plugins: dependency-0.4.0
7+
collected 7 items
8+
9+
tests/test_nodeid.py::test_a PASSED [ 14%]
10+
tests/test_nodeid.py::test_b[7-True] PASSED [ 28%]
11+
tests/test_nodeid.py::test_b[0-False] PASSED [ 42%]
12+
tests/test_nodeid.py::test_b[-1-False] XFAIL [ 57%]
13+
tests/test_nodeid.py::TestClass::test_c PASSED [ 71%]
14+
tests/test_nodeid.py::TestClass::test_d[order] PASSED [ 85%]
15+
tests/test_nodeid.py::TestClass::test_d[disorder] PASSED [100%]
16+
17+
========================= 6 passed, 1 xfailed in 0.08s =========================

doc/examples/nodeid.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import random
2+
import pytest
3+
4+
def test_a():
5+
pass
6+
7+
@pytest.mark.parametrize("i,b", [
8+
(7, True),
9+
(0, False),
10+
pytest.param(-1, False, marks=pytest.mark.xfail(reason="nonsense"))
11+
])
12+
def test_b(i, b):
13+
assert bool(i) == b
14+
15+
ordered = list(range(10))
16+
unordered = random.sample(ordered, k=len(ordered))
17+
18+
class TestClass:
19+
20+
def test_c(self):
21+
pass
22+
23+
@pytest.mark.parametrize("l,ll", [(ordered, 10), (unordered, 10)],
24+
ids=["order", "disorder"])
25+
def test_d(self, l, ll):
26+
assert len(l) == ll

doc/src/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Content of the documentation
1515
install
1616
usage
1717
advanced
18+
names
1819
configuration
1920
changelog
2021
reference

doc/src/names.rst

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
.. _names:
2+
3+
Names
4+
=====
5+
6+
Dependencies of tests are referenced by name. The default name is the
7+
`node id`__ assigned to the test by pytest. This default may be
8+
overridden by an explicit `name` argument to the
9+
:func:`pytest.mark.dependency` marker. The references also depend on
10+
the scope.
11+
12+
.. __: https://docs.pytest.org/en/latest/example/markers.html#node-id
13+
14+
Node ids
15+
--------
16+
17+
The node ids in pytest are built of several components, separated by a
18+
double colon "::". For test functions, these components are the
19+
relative path of the test module and the name of the function. In the
20+
case of a method of a test class the components are the module path,
21+
the name of the class, and the name of the method. If the function or
22+
method is parameterized, the parameter values, separated by minus "-",
23+
in square brackets "[]" are appended to the node id. The
24+
representation of the parameter values in the node id may be
25+
overridden using the `ids` argument to the
26+
`pytest.mark.parametrize()`__ marker.
27+
28+
.. __: https://docs.pytest.org/en/latest/reference.html#pytest-mark-parametrize-ref
29+
30+
31+
One may check the node ids of all tests calling pytest with the
32+
`--verbose` command line option. As an example, consider the
33+
following test module:
34+
35+
.. literalinclude:: ../examples/nodeid.py
36+
37+
If this module is stored as `tests/test_nodeid.py`, the output will
38+
look like:
39+
40+
.. literalinclude:: ../examples/nodeid.out
41+
42+
References and scope
43+
--------------------
44+
45+
When referencing dependencies of tests, the names to be used in the
46+
`depends` argument to the :func:`pytest.mark.dependency` marker or the
47+
`other` argument to the :func:`pytest_dependency.depends` function
48+
depend on the scope as follows:
49+
50+
`session`
51+
The full node id must be used.
52+
`package`
53+
The full node id must be used.
54+
`module`
55+
The node id with the leading module path including the "::"
56+
separator removed must be used.
57+
`class`
58+
The node id with the module path and the class name including the
59+
"::" separator removed must be used.
60+
61+
That is, in the example above, when referencing `test_a` as a
62+
dependency, it must be referenced as `tests/test_nodeid.py::test_a` in
63+
session scope and as `test_a` in module scope. When referencing the
64+
first invocation of `test_d` as a dependency, it must be referenced as
65+
`tests/test_nodeid.py::TestClass::test_d[order]` in session scope, as
66+
`TestClass::test_d[order]` in module scope, and as `test_d[order]` in
67+
class scope.
68+
69+
If the name of the dependency has been set with an explicit `name`
70+
argument to the :func:`pytest.mark.dependency` marker, this name must
71+
always be used as is, regardless of the scope.
72+
73+
.. note::
74+
The module path in the node id is the path relative to the current
75+
working directory. This depends on the invocation of pytest. In
76+
the example above, if you change into the `tests` directory before
77+
invoking pytest, the module path in the node ids will be
78+
`test_nodeid.py`. If you use references in session scope, you'll
79+
need to make sure pytest is always invoked from the same working
80+
directory.

doc/src/usage.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Naming tests
4242
------------
4343

4444
Tests are referenced by their name in the `depends` argument. The
45-
default for this name is the node ID defined by pytest, that is the
45+
default for this name is the node id defined by pytest, that is the
4646
name of the test function, extended by the parameters if applicable.
47-
In some cases, it's not easy to predict the names of the node IDs.
47+
In some cases, it's not easy to predict the names of the node ids.
4848
For this reason, the name of the tests can be overridden by an
4949
explicit `name` argument to the marker. The names must be unique in
5050
the scope, which is currently the test module. The following example

0 commit comments

Comments
 (0)