0
0
PyTesttesting~10 mins

Test classes in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a test class in pytest.

PyTest
class TestExample:
    def [1](self):
        assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Acheck_add
Baddition_test
Ctest_addition
Dverify_addition
Attempts:
3 left
💡 Hint
Common Mistakes
Not starting the method name with 'test_' so pytest skips the test.
Using a method name that does not follow pytest naming conventions.
2fill in blank
medium

Complete the code to use a setup method in a pytest test class.

PyTest
class TestSetup:
    def [1](self):
        self.value = 10

    def test_value(self):
        assert self.value == 10
Drag options to blanks, or click blank then click option'
Asetup_method
BsetUp
Csetup_class
Dsetup
Attempts:
3 left
💡 Hint
Common Mistakes
Using setup or setUp which are not recognized by pytest.
Using setup_class which runs once per class, not before each test.
3fill in blank
hard

Fix the error in the test class to correctly use a class-level setup in pytest.

PyTest
class TestClassSetup:
    @classmethod
    def [1](cls):
        cls.data = [1, 2, 3]

    def test_data_length(self):
        assert len(self.data) == 3
Drag options to blanks, or click blank then click option'
AsetupMethod
Bsetup_class
Csetup_class_method
Dsetup
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like setupMethod or setup.
Not using the @classmethod decorator.
4fill in blank
hard

Fill both blanks to correctly skip a test method conditionally in a pytest test class.

PyTest
import pytest

class TestSkip:
    @pytest.mark.[1](reason="Not ready")
    def [2](self):
        assert False
Drag options to blanks, or click blank then click option'
Askip
Bskipif
Ctest_skip
Dtest_skip_method
Attempts:
3 left
💡 Hint
Common Mistakes
Using skipif without a condition.
Naming the test method without the test_ prefix.
5fill in blank
hard

Fill all three blanks to correctly parametrize a test method inside a pytest test class.

PyTest
import pytest

class TestParam:
    @pytest.mark.[1]("input,expected", [(1, 2), (3, 4)])
    def [2](self, [3], expected):
        assert [3] + 1 == expected
Drag options to blanks, or click blank then click option'
Aparametrize
Btest_increment
Cinput
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong decorator names like parameterize.
Mismatch between parameter names in decorator and method signature.