0
0
PyTesttesting~20 mins

Test classes in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Test Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
ABoth tests fail
Btest_add passes, test_subtract fails
Ctest_add fails, test_subtract passes
DBoth tests pass
Attempts:
2 left
💡 Hint
setup_method runs before each test method to set self.value
assertion
intermediate
1: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]
Aassert items.count == 4
Bassert items.length == 4
Cassert len(items) == 4
Dassert items.size() == 4
Attempts:
2 left
💡 Hint
Use Python's built-in function to get length of a list
🔧 Debug
advanced
2: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
AAttributeError: 'TestCalc' object has no attribute 'num'
BAssertionError because 10 * 2 != 20
CNo error, test passes
DSyntaxError due to missing colon
Attempts:
2 left
💡 Hint
Check if the setup method is named correctly for pytest
framework
advanced
1: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?
AUse a setup_method(self) to initialize instance variables before each test
BDefine variables directly inside each test method
CUse __init__(self) method to initialize variables
DUse global variables outside the test class
Attempts:
2 left
💡 Hint
pytest calls setup_method automatically before each test method
🧠 Conceptual
expert
2:00remaining
Why use test classes in pytest instead of standalone test functions?
What is the main advantage of organizing tests inside classes in pytest?
ATo make tests run faster by parallel execution
BTo group related tests and share setup/teardown code easily
CTo avoid writing assert statements in tests
DTo automatically generate test reports without configuration
Attempts:
2 left
💡 Hint
Think about code reuse and organization