0
0
PyTesttesting~5 mins

Test classes in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Acheck_
Btest_
Cverify_
Drun_
Which method runs before each test method in a pytest test class?
Asetup_function
Bsetup_class
Csetup_module
Dsetup_method
Do pytest test classes need to inherit from a special base class?
AYes, from unittest.TestCase
BYes, from object
CNo, inheritance is not required
DYes, from pytest.BaseTest
What is a main benefit of using test classes in pytest?
AThey group related tests and share setup
BThey run tests faster
CThey automatically generate test data
DThey replace the need for fixtures
Which of these is a valid pytest test class name?
ATestCalculator
BcalculatorTests
Ctestclass
DMyTests
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.