Challenge - 5 Problems
Advanced Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest fixture scope behavior
What will be the output when running this pytest code with two test functions using a fixture with module scope?
PyTest
import pytest @pytest.fixture(scope="module") def resource(): print("Setup resource") yield "data" print("Teardown resource") def test_one(resource): print(f"Test one got {resource}") assert resource == "data" def test_two(resource): print(f"Test two got {resource}") assert resource == "data"
Attempts:
2 left
💡 Hint
Think about how module scope affects fixture setup and teardown timing.
✗ Incorrect
A fixture with module scope runs setup once before any tests in the module and teardown after all tests finish. So the setup message appears once, then both tests run, then teardown happens once.
❓ assertion
intermediate1:30remaining
Choosing the correct assertion for floating point comparison
Which pytest assertion is best to compare two floating point numbers for equality within a tolerance?
Attempts:
2 left
💡 Hint
Floating point numbers can have tiny differences due to precision.
✗ Incorrect
Direct equality (==) can fail due to small precision errors. Using an absolute difference less than a small number is a reliable way to compare floats.
🔧 Debug
advanced2:00remaining
Identify the cause of flaky test in pytest
This pytest test sometimes fails randomly. What is the most likely cause?
PyTest
import pytest import random def test_random_behavior(): value = random.choice([1, 2, 3]) assert value != 2
Attempts:
2 left
💡 Hint
Think about what happens when random.choice picks 2.
✗ Incorrect
Since random.choice can pick 2, the assertion 'value != 2' can fail sometimes, causing flaky tests.
❓ framework
advanced1:30remaining
Best pytest pattern for testing multiple inputs cleanly
Which pytest feature allows running the same test function with different inputs without repeating code?
Attempts:
2 left
💡 Hint
Look for a way to run tests repeatedly with different data.
✗ Incorrect
The @pytest.mark.parametrize decorator runs the same test multiple times with different arguments, making tests concise and clear.
🧠 Conceptual
expert2:30remaining
Why advanced testing patterns reduce maintenance in real projects
Which statement best explains why advanced testing patterns like fixtures, parameterization, and mocking help handle real-world complexity?
Attempts:
2 left
💡 Hint
Think about how complex projects benefit from organized and isolated tests.
✗ Incorrect
Advanced patterns improve test quality by reusing setup code, testing multiple cases easily, and isolating external systems to avoid flaky tests and duplicated code.