사용 사례에 따라 실질적인 예를 사용하여 frollo's answer을 빌드하려면 아래 코드를 고려하십시오. 원래 테스트 데이터 세트는 두 그룹으로 나뉩니다. 통과 할 것으로 예상되는 그룹과 실패 할 것으로 예상되는 그룹입니다. 이와 같은 단위 테스트는 한 가지 종류의 상황을 독립적으로 테스트해야하므로 각 테스트 사례를 작성하기가 쉬우 며 이해하기 쉽습니다.
import pytest
class TestMinMax:
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(30, 40, 30, 40),
])
def test_valid_examples(self, minimum, maximum, expected_min, expected_max):
assert minimum == expected_min
assert maximum == expected_max
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(13, 15, 34, 45),
("sd", 3, 34, 45),
])
def test_invalid_examples(self, minimum, maximum, expected_min, expected_max):
with pytest.raises(AssertionError):
assert minimum == expected_min
assert maximum == expected_max
if __name__ == '__main__':
pytest.main(args=[__file__])
출력
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-3.0.1, py-1.4.31, pluggy-0.3.1
rootdir: C:\Users\<<user>>\.PyCharmCE2016.3\config\scratches, inifile:
collected 3 items
scratch_3.py ...
========================== 3 passed in 0.02 seconds ===========================
[단정 예상 대한 예외 (http://pytest.org/2.2.4/assert.html#assertions-about-expected-exceptions) – Tagc
@Tagc :이 문제는 내가 반대 결과를 얻을 것이다. 통과, 실패, 합격. – George
네, 입력을 두 그룹 ("유효"및 "유효하지 않은"경우)으로 나누고 각 그룹을 개별적으로 테스트해야합니다 (아래에서 frollo가 제안한 것처럼). – Tagc