|
| 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. |
0 commit comments