0
0
PyTesttesting~10 mins

Factory 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 define a simple pytest fixture that returns a user name.

PyTest
import pytest

@pytest.fixture
def [1]():
    return "Alice"
Drag options to blanks, or click blank then click option'
Auser_name
Busername
Cname
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using a name that is too generic or not descriptive.
Forgetting to add the @pytest.fixture decorator.
2fill in blank
medium

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

PyTest
def test_greeting([1]):
    assert [1] == "Alice"
Drag options to blanks, or click blank then click option'
Auser
Buser_name
Cusername
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture name.
Not including the fixture as a parameter in the test function.
3fill in blank
hard

Fix the error in the fixture that should return a dictionary with user info.

PyTest
@pytest.fixture
def user_info():
    return [1]"name": "Bob", "age": 30}
Drag options to blanks, or click blank then click option'
A{
B[
C(
D{"
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or parentheses instead of curly braces.
Missing the opening brace for the dictionary.
4fill in blank
hard

Fill both blanks to create a fixture that returns a list of user names and a test that uses it.

PyTest
@pytest.fixture
def user_list():
    return [1]"Alice", "Bob", "Charlie"[2]

def test_user_count(user_list):
    assert len(user_list) == 3
Drag options to blanks, or click blank then click option'
A[
B(
C]
D)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for lists.
Mismatching opening and closing brackets.
5fill in blank
hard

Complete the code to create a fixture that returns a dictionary with user details and a test that checks the user's age.

PyTest
@pytest.fixture
def user_data():
    return {"name": "Eve", [1]: 28}

def test_user_age(user_data):
    assert user_data["age"] == 28
Drag options to blanks, or click blank then click option'
A{
B"age"
C}
D[
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to quote dictionary keys.
Using square brackets instead of curly braces for dictionaries.
Not closing the dictionary properly.