0
0
PyTesttesting~10 mins

Why PyTest is the most popular Python testing framework - Test Your Understanding

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

Complete the code to write a simple test function using PyTest.

PyTest
def test_addition():
    assert 1 + 1 == [1]
Drag options to blanks, or click blank then click option'
A1
B3
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong expected value in the assertion.
2fill in blank
medium

Complete the code to use a PyTest fixture for setup.

PyTest
import pytest

@pytest.fixture
def sample_data():
    return [1, 2, 3]

def test_sum([1]):
    assert sum([1]) == 6
Drag options to blanks, or click blank then click option'
Adata
Bsample_data
Cnumbers
Dinput_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture.
3fill in blank
hard

Fix the error in the PyTest test function to correctly check for an exception.

PyTest
import pytest

def test_zero_division():
    with pytest.raises([1]):
        1 / 0
Drag options to blanks, or click blank then click option'
AZeroDivisionError
BValueError
CTypeError
DIndexError
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong exception type in pytest.raises.
4fill in blank
hard

Fill both blanks to create a parameterized test that checks multiple inputs.

PyTest
import pytest

@pytest.mark.parametrize("input,expected")
def test_multiply([1], [2]):
    assert input * 2 == expected
Drag options to blanks, or click blank then click option'
Ainput
Bexpected
Cvalue
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that do not match the decorator.
5fill in blank
hard

Fill all three blanks to create a test that skips when a condition is met.

PyTest
import pytest

@pytest.mark.skipif([1], reason=[2])
def test_feature():
    assert [3] == 10
Drag options to blanks, or click blank then click option'
AFalse
B"Not supported on Windows"
C5 + 5
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using False as skip condition when test should skip.
Incorrect assertion expression.