Introduction to Testing in Python
Alexander Levin
Data Scientist
autouse=True
the fixture function is executing regardless of a requestIn case we need to apply certain environment preparations or modifications for all tests.
For example, when we want to guarantee, that all tests:
All such cases should be addressed with an "autouse" argument.
Example of an "autoused" fixture:
import pytest
import pandas as pd
# Autoused fixture
@pytest.fixture(autouse=True)
def set_pd_options():
pd.set_option('display.max_columns', 5000)
# Test function
def test_pd_options():
assert pd.get_option('display.max_columns') == 5000
Incorrect example of an "autoused" fixture:
import pytest
import pandas as pd
# Wrong autoused fixture
@pytest.fixture(autouse=True)
def wrong_fixture():
return [1,2,3,4,5]
# Test function
def test_type():
assert type(wrong_fixture) == list
Corrected example of the fixture:
import pytest
import pandas as pd
# Wrong autoused fixture
@pytest.fixture
def correct_fixture():
return [1,2,3,4,5]
# Test function
def test_type(correct_fixture):
assert type(correct_fixture) == list
Output of the example:
autouse
: An optional boolean argument of a fixture decorator@pytest.fixture(autouse=True)
autouse=True
the fixture function is executing regardless of a requestIntroduction to Testing in Python