0
0
PyTesttesting~10 mins

Conftest fixtures (shared across files) 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 in conftest.py that returns a simple string.

PyTest
import pytest

@pytest.fixture
def [1]():
    return "hello"
Drag options to blanks, or click blank then click option'
Agreeting
Bfixture
Ctest_data
Dsetup
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid Python names for the fixture function.
Forgetting to decorate the function with @pytest.fixture.
2fill in blank
medium

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

PyTest
def test_example([1]):
    assert [1] == "hello"
Drag options to blanks, or click blank then click option'
Adata
Bsetup
Cgreeting
Dfixture
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the fixture defined.
Not including the fixture as a parameter in the test function.
3fill in blank
hard

Fix the error in the fixture definition to make it shared across multiple test files.

PyTest
import pytest

@pytest.fixture
def [1]():
    return 42
Drag options to blanks, or click blank then click option'
Ashared_fixture
Bfixture
Cdata
Dshared_data
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the @pytest.fixture decorator.
Naming the fixture inconsistently.
4fill in blank
hard

Fill both blanks to correctly import pytest and define a fixture that returns a list.

PyTest
[1]

@pytest.fixture
def sample_list():
    return [2]
Drag options to blanks, or click blank then click option'
Aimport pytest
Bimport unittest
C[1, 2, 3]
D(1, 2, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using tuple instead of list for the return value.
Importing unittest instead of pytest.
5fill in blank
hard

Fill all three blanks to define a fixture with scope 'module' and use it in a test function.

PyTest
import pytest

@pytest.fixture(scope=[1])
def config():
    return {"url": "http://example.com"}

def test_url([2]):
    assert [3]["url"] == "http://example.com"
Drag options to blanks, or click blank then click option'
A"module"
Bconfig
D"function"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect scope string.
Mismatching fixture name in test function parameter.
Using wrong variable name inside the test.