In pytest projects, why is it recommended to keep all test files inside a separate tests/ directory?
Think about how separating concerns helps in managing code.
Keeping tests in a dedicated tests/ folder helps keep the project organized. It separates test code from application code, making it easier to find and run tests. Pytest can discover tests in any folder, but tests/ is a common convention.
Given this project structure:
my_project/
app.py
tests/
test_math.py
test_utils.pyAnd test_math.py contains a passing test, what will pytest report when run from my_project/?
def test_addition(): assert 1 + 1 == 2
Pytest automatically finds tests in subfolders named test_*.py.
Pytest recursively searches for test files matching test_*.py or *_test.py patterns. It will find test_math.py inside tests/ and run the test, reporting success.
Which filename is the best practice locator for a pytest test file that tests user authentication?
Pytest looks for files starting with test_ or ending with _test.py.
By convention, pytest test files start with test_ or end with _test.py. test_authentication.py follows this and uses lowercase with underscores, which is recommended.
You want to assert that a list items has exactly 3 elements in a pytest test. Which assertion is correct and best practice?
items = [1, 2, 3]
Remember how to get the length of a list in Python.
In Python, len() returns the number of elements in a list. The other options use invalid syntax or methods not available on lists.
You want to create a reusable setup for database connection in pytest tests. Which project structure and fixture placement is best practice?
Think about how pytest discovers fixtures automatically.
Placing fixtures in conftest.py inside the tests/ folder allows pytest to find them automatically for all tests in that folder and subfolders without explicit imports. This promotes reuse and cleaner test files.