0
0
PyTesttesting~20 mins

Project structure for tests in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pytest Project Structure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use a separate tests directory in pytest projects?

In pytest projects, why is it recommended to keep all test files inside a separate tests/ directory?

ATo organize tests separately from application code for clarity and easier test discovery.
BBecause pytest only runs tests inside a folder named <code>tests</code>.
CTo prevent tests from being included in the final application package automatically.
DTo make test files hidden from version control systems like Git.
Attempts:
2 left
💡 Hint

Think about how separating concerns helps in managing code.

Predict Output
intermediate
2:00remaining
What is the output when running pytest with this structure?

Given this project structure:

my_project/
  app.py
  tests/
    test_math.py
    test_utils.py

And test_math.py contains a passing test, what will pytest report when run from my_project/?

PyTest
def test_addition():
    assert 1 + 1 == 2
Apytest will not find any tests because they are inside a subfolder.
Bpytest will run tests but report failures due to missing __init__.py files.
Cpytest will raise an error because test files must be in the root folder.
Dpytest will find and run the test, reporting 1 passed test.
Attempts:
2 left
💡 Hint

Pytest automatically finds tests in subfolders named test_*.py.

locator
advanced
2:00remaining
Best locator for a test file in pytest project

Which filename is the best practice locator for a pytest test file that tests user authentication?

Atest_authentication.py
Bauthentication_test.py
CauthTests.py
DTestAuthentication.py
Attempts:
2 left
💡 Hint

Pytest looks for files starting with test_ or ending with _test.py.

assertion
advanced
2:00remaining
Which assertion correctly checks a list length in pytest?

You want to assert that a list items has exactly 3 elements in a pytest test. Which assertion is correct and best practice?

PyTest
items = [1, 2, 3]
Aassert items.count == 3
Bassert items.length == 3
Cassert len(items) == 3
Dassert items.size() == 3
Attempts:
2 left
💡 Hint

Remember how to get the length of a list in Python.

framework
expert
3:00remaining
How to structure pytest fixtures for reusable setup?

You want to create a reusable setup for database connection in pytest tests. Which project structure and fixture placement is best practice?

ADefine the fixture inside each test file that needs it to avoid global scope.
BPlace the fixture in <code>tests/conftest.py</code> so all tests can use it without imports.
CPut the fixture in the main application code folder to share with non-test code.
DCreate a separate <code>fixtures.py</code> file inside <code>tests/</code> and import it in each test file.
Attempts:
2 left
💡 Hint

Think about how pytest discovers fixtures automatically.