0
0
PyTesttesting~10 mins

@pytest.fixture decorator - 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 using pytest.

PyTest
import pytest

@pytest.[1]()
def sample_data():
    return [1, 2, 3]
Drag options to blanks, or click blank then click option'
Atest
Bfixture
Csetup
Dparametrize
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.test instead of @pytest.fixture
Forgetting the parentheses after the decorator
Confusing fixture with parametrize
2fill in blank
medium

Complete the test function to use the fixture named 'sample_data'.

PyTest
def test_sum([1]):
    assert sum(sample_data) == 6
Drag options to blanks, or click blank then click option'
Afixture
Bdata
Csample_data
Dinput_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture
Not including the fixture as a parameter
Trying to call the fixture inside the test instead of using it as a parameter
3fill in blank
hard

Fix the error in the fixture definition by completing the decorator correctly.

PyTest
import pytest

@[1]
def config():
    return {'url': 'http://example.com'}
Drag options to blanks, or click blank then click option'
Apytest.setup
Bpytest.fixture
Cpytest.test
Dpytest.fixture()
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses in the decorator
Using '@pytest.test' instead of '@pytest.fixture()'
Using '@pytest.setup' which is not a valid decorator
4fill in blank
hard

Fill both blanks to create a fixture that runs once per module and a test that uses it.

PyTest
import pytest

@pytest.fixture(scope=[1])
def resource():
    return 'db_connection'

def test_resource([2]):
    assert resource == 'db_connection'
Drag options to blanks, or click blank then click option'
A'module'
Bresource
C'function'
Dconn
Attempts:
3 left
💡 Hint
Common Mistakes
Using scope 'function' which runs before every test
Using a different parameter name than the fixture
Forgetting quotes around the scope value
5fill in blank
hard

Fill all three blanks to create a fixture with autouse enabled and a test that uses it implicitly.

PyTest
import pytest

@pytest.fixture(autouse=[1])
def setup_env():
    print('Setup environment')


def test_example():
    assert True  # setup_env is used [2] without being [3] as a parameter
Drag options to blanks, or click blank then click option'
ATrue
Bimplicitly
Cpassed
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting autouse to False so fixture does not run automatically
Thinking the fixture must be passed as a parameter when autouse=True
Misunderstanding implicit usage of fixtures