Challenge - 5 Problems
Test Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest test class with setup method
What is the output when running this pytest test class?
PyTest
import pytest class TestExample: def setup_method(self): self.value = 5 def test_add(self): assert self.value + 3 == 8 def test_subtract(self): assert self.value - 2 == 3
Attempts:
2 left
💡 Hint
setup_method runs before each test method to set self.value
✗ Incorrect
The setup_method sets self.value to 5 before each test. test_add checks 5 + 3 == 8 which is True. test_subtract checks 5 - 2 == 3 which is True. So both tests pass.
❓ assertion
intermediate1:30remaining
Correct assertion to check list length in test class
Which assertion correctly checks that the list has exactly 4 items inside a pytest test method?
PyTest
class TestList: def test_length(self): items = [1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Use Python's built-in function to get length of a list
✗ Incorrect
len(items) returns the number of elements in the list. The other options use invalid syntax or methods not available on Python lists.
🔧 Debug
advanced2:00remaining
Identify the error in this pytest test class
What error will occur when running this pytest test class?
PyTest
class TestCalc: def setup(self): self.num = 10 def test_multiply(self): assert self.num * 2 == 20
Attempts:
2 left
💡 Hint
Check if the setup method is named correctly for pytest
✗ Incorrect
pytest expects setup_method(self) or setup_class(cls) for setup. The method named setup(self) is ignored, so self.num is never set. Accessing self.num causes AttributeError.
❓ framework
advanced1:30remaining
Best practice for sharing setup code in pytest test classes
Which approach is best to share setup code for all test methods in a pytest test class?
Attempts:
2 left
💡 Hint
pytest calls setup_method automatically before each test method
✗ Incorrect
setup_method(self) is the pytest convention to run setup code before each test method. __init__ is not recommended because pytest creates new instances differently. Defining variables inside each test repeats code. Global variables reduce test isolation.
🧠 Conceptual
expert2:00remaining
Why use test classes in pytest instead of standalone test functions?
What is the main advantage of organizing tests inside classes in pytest?
Attempts:
2 left
💡 Hint
Think about code reuse and organization
✗ Incorrect
Test classes help group related tests logically and allow sharing setup and teardown code via methods like setup_method. This improves code organization and reduces duplication. Other options are incorrect because classes do not affect speed, assert statements are always needed, and reports require separate configuration.