현재 다음과 같은 설정이 있습니다.
./
src/
mylib.py
tests/
__init__.py
conftest.py
test_mylib.py
mylib.py
포함 :
def add(x, y):
return x + y
conftest.py
포함 :
import attr
import pytest
@attr.s(slots=True)
class AdditionTestCase:
input1 = attr.ib()
input2 = attr.ib()
output_expected = attr.ib()
@pytest.fixture
def addition_test_case() -> AdditionTestCase:
# make up some data
return AdditionTestCase(1, 2, 3)
및 test_mylib.py
포함
from mylib import add
from tests.conftest import AdditionTestCase
def test_add(addition_test_case: AdditionTestCase):
input1 = addition_test_case.input1
input2 = addition_test_case.input2
output_expected = addition_test_case.output_expected
output_actual = add(input1, input2)
assert output_actual == output_expected
어떤 이유로 든 문제가 있습니까? 더 나은 방법이 있습니까? conftest.py
가져 오는 것이 나쁜 습관으로 간주되면 유형 정의를 외부로 이동할 수 있습니다.
일반적으로 이것은 추가 유형없이 매개 변수화를 사용하여 해결됩니다.
@pytest.mark.parametrize("left, right, expected_output", [
(1, 1, 2),
(2, 2, 4),
]
def test_add(left, right, expected_output):
output = add(left, right)
assert output == expected_output
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다