Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Why should you place your test files inside a tests/ folder in a pytest project?
easy
A. To avoid writing test functions starting with test_
B. To make the tests run faster automatically
C. Because pytest only runs tests in the root folder
D. To keep test code separate and organized from application code

Solution

  1. Step 1: Understand project organization

    Keeping tests in a separate tests/ folder helps keep the project clean and organized.
  2. Step 2: Recognize pytest behavior

    pytest can find tests anywhere but organizing them in tests/ is a best practice for clarity and maintenance.
  3. Final Answer:

    To keep test code separate and organized from application code -> Option D
  4. Quick Check:

    Organized tests = tests/ folder [OK]
Hint: Put tests in a separate folder named tests for clarity [OK]
Common Mistakes:
  • Thinking pytest only runs tests in root folder
  • Believing tests run faster in tests/ folder
  • Ignoring naming conventions for test functions
2. Which of the following is the correct way to name a test file so pytest can discover it automatically?
easy
A. test_example.py
B. mytests.py
C. example.py
D. TestExample.py

Solution

  1. Step 1: Recall pytest naming rules

    pytest discovers test files that start with test_ or end with _test.py.
  2. Step 2: Check each option

    test_example.py starts with test_, so pytest will find it automatically.
  3. Final Answer:

    test_example.py -> Option A
  4. Quick Check:

    File starts with test_ = discovered [OK]
Hint: Name test files starting with test_ for pytest discovery [OK]
Common Mistakes:
  • Naming files without test_ prefix
  • Using uppercase letters at start
  • Assuming pytest finds any .py file
3. Given this project structure:
project/
  app.py
  tests/
    test_math.py
    utils/
      test_utils.py
Which files will pytest run by default when you run pytest in the project root?
medium
A. Only tests/utils/test_utils.py
B. Only tests/test_math.py
C. Both tests/test_math.py and tests/utils/test_utils.py
D. No tests run because of subfolder

Solution

  1. Step 1: Understand pytest recursive discovery

    pytest looks inside the tests/ folder and all its subfolders for files starting with test_.
  2. Step 2: Identify test files

    Both test_math.py and test_utils.py match naming rules and are inside tests/ or its subfolder.
  3. Final Answer:

    Both tests/test_math.py and tests/utils/test_utils.py -> Option C
  4. Quick Check:

    pytest finds tests recursively in tests/ [OK]
Hint: pytest finds test files recursively inside tests/ folder [OK]
Common Mistakes:
  • Thinking pytest ignores subfolders
  • Assuming only root tests/ files run
  • Believing subfolders need special config
4. You have this test file named test_sample.py inside tests/ folder:
def sample_test():
    assert 1 + 1 == 2
When you run pytest, no tests are found. Why?
medium
A. Function name does not start with test_
B. File name should not start with test_
C. pytest requires tests to be inside root folder only
D. Missing import statement in test file

Solution

  1. Step 1: Check function naming convention

    pytest discovers test functions only if their names start with test_.
  2. Step 2: Analyze given function name

    The function is named sample_test, which does not start with test_, so pytest ignores it.
  3. Final Answer:

    Function name does not start with test_ -> Option A
  4. Quick Check:

    Test functions must start with test_ [OK]
Hint: Name test functions starting with test_ to be discovered [OK]
Common Mistakes:
  • Assuming file name alone is enough
  • Thinking pytest needs imports in test files
  • Believing tests run only in root folder
5. In a large pytest project, how can you organize tests to keep them manageable and easy to run selectively?
hard
A. Put all tests in one big file named test_all.py
B. Use subfolders inside tests/ and add pytest.ini to configure test discovery
C. Name test functions randomly and rely on pytest to find them
D. Avoid using any configuration files to keep it simple

Solution

  1. Step 1: Understand project scaling needs

    Large projects benefit from organizing tests in subfolders to separate features or modules.
  2. Step 2: Use configuration files

    Adding a pytest.ini file helps customize test discovery and running specific groups of tests easily.
  3. Final Answer:

    Use subfolders inside tests/ and add pytest.ini to configure test discovery -> Option B
  4. Quick Check:

    Subfolders + pytest.ini = manageable large projects [OK]
Hint: Organize tests in subfolders and use pytest.ini config [OK]
Common Mistakes:
  • Putting all tests in one file causing confusion
  • Ignoring naming conventions for test functions
  • Avoiding config files limits flexibility
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard