pytest
μ€μΉ
python 3.7+ νμ
pip install -U pytest
μ€ν λ°©λ²
ν μ€νΈ 1κ° μ€ννκΈ°
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
μ΄λ€ ν μ€νΈ νμΌμ μ΄λ€ ν¨μλ₯Ό μννλ€κ° failνλμ§ μμΈκ³Ό μ½λ μ€ λ²νΈλ₯Ό μλ €μ€λ€.
pytestλ νμ¬ ν΄λμ κ·Έ νμ ν΄λ μμ λͺ¨λ νμΌμ κΈ°μ€μΌλ‘ μ΄λ¦μ΄ test_*.py
μ΄κ±°λ *_test.py
μ΄λ©΄ ν
μ€νΈ λμμΌλ‘ κ°μ£Όνλ€.
ν μ€νΈ μ¬λ¬ κ° μ€ννκΈ°
ν μ€νΈ ν΄λμ€λ‘ ν μ€νΈ μ¬λ¬ κ° μ€ννκΈ°
test_*.py
μ΄κ±°λ *_test.py
νμμ νμΌ λ΄μμ
- ν΄λμ€ μΈλΆμ
test*
νμ λ©μλ Test*
νμμ ν΄λμ€ λ΄λΆμμμtest*
νμ λ©μλ (__init__
ν¨μ μ μΈ)
λ ν μ€νΈ λμμ΄λ€.
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = "hello"
assert hasattr(x, "check")
class SEClass:
def test_two(self):
x = "hello"
assert hasattr(x, "check")
κ°κ°μ ν μ€νΈλ€μ λ 립μ μΌλ‘ μνλλ€.
ν΄λμ€ μμ€ λ³μλ μ£Όμνμ
- νμ΄μ¬μ ν΄λμ€ λ³μ
- cppμμμ static member variableκ³Ό λμΌν κ°λ μ΄λ€.
ν΄λμ€ μμ€μμ κ΄λ¦¬νλ λ³μλ ν μ€νΈ κ°μ 곡μ λλ―λ‘ λ€λ₯Έ ν μ€νΈμ μν₯μ μ€ μ μλ€.
class TestClassDemoInstance:
value = 0
def test_one(self):
self.value = 1
assert self.value == 1
def test_two(self):
assert self.value == 1
def test_three(self):
assert TestClassDemoInstance.value == 1
def test_influencer(self):
TestClassDemoInstance.value = 1
assert TestClassDemoInstance.value == 1
def test_fan(self):
assert TestClassDemoInstance.value == 1
ν μ€νΈ λμ μ§μ νκΈ°
νμΌ μ΄λ¦μΌλ‘ μ§μ νκΈ°
κ²½λ‘λ‘ μ§μ νκΈ°
λ¬Έμμ΄λ‘ νν°λ§νκΈ°
-k μ΅μ μ μ¬μ©νλ€.
not three and on
μΌ λ,
not three or on
μΌ λ,
ν΄λμ€λͺ , ν¨μλͺ λ±μΌλ‘ νν°λ§νκΈ°
ν μ€νΈ μ‘°μ©νκ² μ€ννκΈ°
-q, βquiet μ΅μ μ μ¬μ©νλ€.
Failνμ λλ -q μ΅μ μ΄ μ μ©λμ§ μλλ€.
Reference
PREVIOUS[E-Z] TDD Patterns
NEXTStudy Cs