Introductie tot testen in Python
Alexander Levin
Data Scientist
autouse=True draait de fixture-functie altijd, ongeacht een requestAls je bepaalde omgevingsvoorbereiding of -wijzigingen nodig hebt voor alle tests.
Bijvoorbeeld, als je wilt garanderen dat alle tests:
Pak dit soort gevallen aan met het "autouse"-argument.
Voorbeeld van een "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
Onjuist voorbeeld van een "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
Gecorrigeerd fixture-voorbeeld:
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 van het voorbeeld:

autouse: Een optioneel booleaans argument van een fixture-decorator@pytest.fixture(autouse=True)autouse=True draait de fixture-functie altijd, ongeacht een requestIntroductie tot testen in Python