Recall & Review
beginner
What is a test class in pytest?A test class in pytest is a Python class that groups related test methods together. It helps organize tests logically without needing to inherit from any special base class.Click to reveal answer
beginner
How do you name test methods inside a pytest test class?
Test methods inside a pytest test class should start with the prefix <code>test_</code>. This naming lets pytest automatically find and run them.Click to reveal answer
intermediate
Can pytest test classes have setup and teardown methods? How?
Yes. You can define
setup_method(self, method) and teardown_method(self, method) inside the test class. These run before and after each test method respectively.Click to reveal answer
intermediate
Why use test classes instead of just functions in pytest?
Test classes help group related tests, share setup code, and improve readability. They make tests easier to maintain, especially when many tests share common setup.
Click to reveal answer
beginner
Show a simple pytest test class with two test methods and a setup method.class TestMathOperations:
def setup_method(self, method):
self.value = 10
def test_add(self):
assert self.value + 5 == 15
def test_subtract(self):
assert self.value - 3 == 7
Click to reveal answer
What prefix should test methods inside a pytest test class have?
✗ Incorrect
Pytest automatically discovers test methods that start with
test_.Which method runs before each test method in a pytest test class?
✗ Incorrect
setup_method runs before each test method in the class.Do pytest test classes need to inherit from a special base class?
✗ Incorrect
Pytest test classes do not require inheritance from any base class.
What is a main benefit of using test classes in pytest?
✗ Incorrect
Test classes help organize related tests and share setup code.
Which of these is a valid pytest test class name?
✗ Incorrect
Test classes should start with
Test to be easily recognized.Explain how to organize multiple related tests using pytest test classes.
Think about grouping and shared setup.
You got /4 concepts.
Describe the role of setup_method and teardown_method in pytest test classes.
Consider what happens before and after each test.
You got /4 concepts.