Pytest: Getting addresses of all tests

The usage isn't as you specify it. From the documentation: http://doc.pytest.org/en/latest/usage.html

pytest -k stringexpr  # only run tests with names that match the
                      # "string expression", e.g. "MyClass and not method"
                      # will select TestMyClass.test_something
                      # but not TestMyClass.test_method_simple

so what you need to pass to '-k' is a string contained in all the callable functions you want to check (you can use logical operator between these strings). For your example (assuming all defs are prefixed by a foo:::

pytest -k "foo::"

In conftest.py, you can override the 'collection' hooks to print information about collected test 'items'.

You may introduce your own command line option (like --collect-only). If this option is specified, print the test items (in whichever way you like) and exit.

Sample conftest.py below (tested locally):

import pytest

def pytest_addoption(parser):
    parser.addoption("--my_test_dump", action="store", default=None,
        help="Print test items in my custom format")

def pytest_collection_finish(session):
    if session.config.option.my_test_dump is not None:
        for item in session.items:
            print('{}::{}'.format(item.fspath, item.name))
        pytest.exit('Done!')

For more information on pytest hooks, see:

http://doc.pytest.org/en/latest/_modules/_pytest/hookspec.html

Tags:

Python

Pytest