Nhập môn Kiểm thử trong Python
Alexander Levin
Data Scientist
autouse=True, fixture chạy dù không có requestDùng khi cần chuẩn bị/điều chỉnh môi trường cho tất cả các test.
Ví dụ, khi muốn đảm bảo tất cả test:
Các trường hợp này nên dùng tham số "autouse".
Ví dụ fixture "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
Ví dụ không đúng về fixture "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
Ví dụ đã sửa đúng:
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
Kết quả ví dụ:

autouse: Tham số boolean tùy chọn của decorator fixture@pytest.fixture(autouse=True)autouse=True, fixture luôn chạy dù không được yêu cầuNhập môn Kiểm thử trong Python