Challenge - 5 Problems
PyTest Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this simple PyTest test?
Consider this PyTest test function. What will be the test result when you run it?
PyTest
def test_sum(): assert 2 + 3 == 5
Attempts:
2 left
💡 Hint
Check if the assertion condition is true or false.
✗ Incorrect
The assertion 2 + 3 == 5 is true, so the test passes.
❓ assertion
intermediateWhich assertion will correctly check if a list contains exactly 3 items?
You want to write a PyTest assertion to verify that the list
items = [1, 2, 3] has exactly 3 elements. Which option is correct?Attempts:
2 left
💡 Hint
Use the built-in function to get the number of elements in a list.
✗ Incorrect
The len() function returns the number of items in a list. The other options are invalid syntax or attributes.
🔧 Debug
advancedWhy does this PyTest test fail?
Look at this test code and find why it fails when run with PyTest.
PyTest
def test_divide(): result = 10 / 0 assert result == 0
Attempts:
2 left
💡 Hint
What happens when you divide by zero in Python?
✗ Incorrect
Dividing by zero raises a ZeroDivisionError before the assertion runs, causing the test to error out.
❓ framework
advancedWhich PyTest feature allows running setup code before each test function?
You want to run some code before every test function in a file. Which PyTest feature should you use?
Attempts:
2 left
💡 Hint
PyTest fixtures are designed for setup and teardown.
✗ Incorrect
Fixtures with function scope run setup code before each test function automatically.
🧠 Conceptual
expertWhat is the main advantage of using PyTest over basic assert statements in scripts?
Why do testers prefer PyTest framework instead of just writing assert statements in Python scripts?
Attempts:
2 left
💡 Hint
Think about what features a testing framework adds beyond simple asserts.
✗ Incorrect
PyTest offers features like detailed reports, setup/teardown fixtures, and automatic test discovery, which plain asserts do not provide.
