Pytest typically searches for test modules and files in the current directory and its subdirectories. It looks for Python files with names starting with 'test_' or ending with '_test', as well as directories named 'test' containing test files. Pytest also allows specifying specific files or directories to test using command-line options and configuration files. Additionally, pytest can discover test files by searching for classes or functions marked with specific decorators such as @pytest.fixture or @pytest.mark.parametrize.
How does pytest handle test dependencies when finding test files?
pytest does not handle test dependencies automatically when finding test files. It is the responsibility of the test author to manage dependencies between tests. However, pytest provides fixtures that can be used to manage test dependencies. Fixtures allow for setup and teardown code to be shared across multiple tests, helping to manage test dependencies effectively.
How does pytest treat subdirectories containing test files?
Pytest treats subdirectories containing test files just like any other directory in the project. By default, pytest will recursively search for test files in all directories starting from the root directory of the project. It will collect and run all the test functions or classes found in these files.
You can also specify which directories to search for test files by passing a list of directories to the pytest command. For example, you can run pytest only on the tests in a specific subdirectory by using the following command:
1
|
pytest path/to/subdirectory
|
In this way, pytest allows you to organize your tests into separate subdirectories for better code organization and manageability.
How does pytest identify test fixtures during discovery?
Pytest identifies test fixtures during discovery by looking for functions that have been marked with the @pytest.fixture
decorator. These functions are considered fixtures and are used to set up and tear down the testing environment before and after running test functions. Pytest automatically identifies these fixtures and includes them in the test run.