When I run pytest --collect-only
to get the list of my tests, I get them in a format like <Function: test_whatever>
. However, when I use pytest -k ...
to run a specific test, I need to input the "address" of the test in the format foo::test_whatever
. Is it possible to get a list of all the addresses of all the tests in the same format that -k
takes?
Run Multiple Tests From a Specific File and Multiple Files To run all the tests from all the files in the folder and subfolders we need to just run the pytest command. This will run all the filenames starting with test_ and the filenames ending with _test in that folder and subfolders under that folder.
The conftest.py file serves as a means of providing fixtures for an entire directory. Fixtures defined in a conftest.py can be used by any test in that package without needing to import them (pytest will automatically discover them).
The pytestconfig fixture is a shortcut to request. config, and is sometimes referred to in the pytest documentation as “the pytest config object.” To see how pytestconfig works, you'll look at how to add a custom command-line option and read the option value from within a test.
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
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::"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With