0
0
PyTesttesting~10 mins

Parametrized fixtures 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 create a parametrized fixture with pytest.

PyTest
import pytest

@pytest.fixture(params=[1, 2, 3])
def number(request):
    return request.[1]
Drag options to blanks, or click blank then click option'
Aitem
Bparam
Cvalue
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.params instead of request.param.
Trying to return request.value which does not exist.
2fill in blank
medium

Complete the test function to receive the parametrized fixture value.

PyTest
def test_is_positive([1]):
    assert [1] > 0
Drag options to blanks, or click blank then click option'
Anumber
Bparam
Crequest
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using param or request as parameter names instead of the fixture name.
Not including any parameter in the test function.
3fill in blank
hard

Fix the error in the fixture to correctly parametrize it with strings.

PyTest
import pytest

@pytest.fixture(params=[1])
def greeting(request):
    return request.param
Drag options to blanks, or click blank then click option'
A"hello, hi, hey"
B('hello', 'hi', 'hey')
C['hello', 'hi', 'hey']
Dhello, hi, hey
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single string instead of a list of strings.
Passing unquoted words without brackets.
4fill in blank
hard

Fill both blanks to parametrize a fixture with numbers and use it in a test.

PyTest
import pytest

@pytest.fixture(params=[1])
def num(request):
    return request.param

def test_even([2]):
    assert [2] % 2 == 0
Drag options to blanks, or click blank then click option'
A[2, 4, 6]
Bnum
Cnumber
D[1, 3, 5]
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd numbers in the params list.
Using a different parameter name in the test function than the fixture name.
5fill in blank
hard

Fill all three blanks to create a parametrized fixture with mixed types and test their types.

PyTest
import pytest

@pytest.fixture(params=[1])
def data(request):
    return request.param

def test_type([2]):
    assert isinstance([3], (int, str))
Drag options to blanks, or click blank then click option'
A[42, 'answer', 7]
Bdata
D[42, 7, 13]
Attempts:
3 left
💡 Hint
Common Mistakes
Using only integers in the params list.
Using different names for the test function parameter and the variable in the assertion.