0
0
PyTesttesting~10 mins

Fixture factories 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 fixture factory that returns a function creating user dictionaries.

PyTest
import pytest

@pytest.fixture
def user_factory():
    def create_user(name):
        return {'name': [1]
    return create_user
Drag options to blanks, or click blank then click option'
Auser
B'name'
Ccreate_user
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the key 'name' as the value instead of the variable name.
Using the function name instead of the parameter.
2fill in blank
medium

Complete the code to use the fixture factory in a test function.

PyTest
def test_user_creation(user_factory):
    user = user_factory([1])
    assert user['name'] == 'Alice'
Drag options to blanks, or click blank then click option'
A'Alice'
B'Bob'
CAlice
Duser_factory
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without quotes.
Passing a different string than 'Alice'.
3fill in blank
hard

Fix the error in the fixture factory to correctly accept an optional age parameter with default 30.

PyTest
import pytest

@pytest.fixture
def user_factory():
    def create_user(name, age=[1]):
        return {'name': name, 'age': age}
    return create_user
Drag options to blanks, or click blank then click option'
A30
B'30'
CNone
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string '30' instead of integer 30.
Using a variable name that is not defined.
4fill in blank
hard

Fill both blanks to create a fixture factory that returns a user with a default city and allows overriding it.

PyTest
import pytest

@pytest.fixture
def user_factory():
    def create_user(name, city=[1]):
        return {'name': name, 'city': [2]
    return create_user
Drag options to blanks, or click blank then click option'
A'New York'
Bcity
C'Los Angeles'
D'city'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the string 'city' as the dictionary value instead of the variable.
Not quoting the default city string.
5fill in blank
hard

Fill all three blanks to create a fixture factory that returns a user dictionary with name, age, and city, where age defaults to 25 and city defaults to 'Boston'.

PyTest
import pytest

@pytest.fixture
def user_factory():
    def create_user(name, age=[1], city=[2]):
        return {'name': name, 'age': [3], 'city': city}
    return create_user
Drag options to blanks, or click blank then click option'
A25
B'Boston'
Cage
D'25'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string '25' as default age instead of integer.
Using string 'age' in dictionary instead of variable.
Not quoting the city string.