[E-Z] pytest

pytest

μ„€μΉ˜

python 3.7+ ν•„μš”

pip install -U pytest

μ‹€ν–‰ 방법

ν…ŒμŠ€νŠΈ 1개 μ‹€ν–‰ν•˜κΈ°

def func(x):
    return x + 1
def test_answer():
    assert func(3) == 5

μ–΄λ–€ ν…ŒμŠ€νŠΈ 파일의 μ–΄λ–€ ν•¨μˆ˜λ₯Ό μˆ˜ν–‰ν•˜λ‹€κ°€ failν–ˆλŠ”μ§€ 원인과 μ½”λ“œ 쀄 번호λ₯Ό μ•Œλ €μ€€λ‹€.

Image

pytestλŠ” ν˜„μž¬ 폴더와 κ·Έ ν•˜μœ„ 폴더 μ•ˆμ˜ λͺ¨λ“  νŒŒμΌμ„ κΈ°μ€€μœΌλ‘œ 이름이 test_*.pyμ΄κ±°λ‚˜ *_test.py이면 ν…ŒμŠ€νŠΈ λŒ€μƒμœΌλ‘œ κ°„μ£Όν•œλ‹€.

ν…ŒμŠ€νŠΈ μ—¬λŸ¬ 개 μ‹€ν–‰ν•˜κΈ°

Image

ν…ŒμŠ€νŠΈ 클래슀둜 ν…ŒμŠ€νŠΈ μ—¬λŸ¬ 개 μ‹€ν–‰ν•˜κΈ°

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")

Image

각각의 ν…ŒμŠ€νŠΈλ“€μ€ λ…λ¦½μ μœΌλ‘œ μˆ˜ν–‰λœλ‹€.

클래슀 μˆ˜μ€€ λ³€μˆ˜λŠ” μ£Όμ˜ν•˜μž

파이썬의 클래슀 λ³€μˆ˜
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

Image

ν…ŒμŠ€νŠΈ λŒ€μƒ μ§€μ •ν•˜κΈ°

파일 μ΄λ¦„μœΌλ‘œ μ§€μ •ν•˜κΈ°

Image

경둜둜 μ§€μ •ν•˜κΈ°

Image

λ¬Έμžμ—΄λ‘œ ν•„ν„°λ§ν•˜κΈ°

-k μ˜΅μ…˜μ„ μ‚¬μš©ν•œλ‹€.

not three and on일 λ•Œ,

Image

not three or on일 λ•Œ,

Image

클래슀λͺ…, ν•¨μˆ˜λͺ… λ“±μœΌλ‘œ ν•„ν„°λ§ν•˜κΈ°

Image

Image

ν…ŒμŠ€νŠΈ μ‘°μš©ν•˜κ²Œ μ‹€ν–‰ν•˜κΈ°

-q, –quiet μ˜΅μ…˜μ„ μ‚¬μš©ν•œλ‹€.

Image

Failν–ˆμ„ λ•ŒλŠ” -q μ˜΅μ…˜μ΄ μ μš©λ˜μ§€ μ•ŠλŠ”λ‹€.

Image

Reference

pytest
pytest-builtin
API reference