Wprowadzenie do testowania w Pythonie
Alexander Levin
Data Scientist
autouse=True, funkcja fixture wykonuje się niezależnie od żądaniaStosowany, gdy konieczne jest przygotowanie lub modyfikacja środowiska dla wszystkich testów.
Na przykład, gdy chcemy zagwarantować, że wszystkie testy:
Wszystkie takie przypadki należy obsłużyć za pomocą argumentu „autouse".
Przykład fixture z „autouse":
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
Niepoprawny przykład fixture z „autouse":
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
Poprawiony przykład 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
Wynik działania przykładu:

autouse: Opcjonalny argument logiczny dekoratora fixture@pytest.fixture(autouse=True)autouse=True – funkcja fixture wykonuje się niezależnie od żądaniaWprowadzenie do testowania w Pythonie