본문 바로가기
🎪 놀고있네/Python

[Python] Pytest 사용해보기(Class)

by 냥장판 2024. 5. 1.
반응형

이전 포스팅에는 class 로 묶는 거 없이 테스트를 해보았다

2024.04.27 - [🎪 놀고있네/Python] - [Python] Pytest 설치하고 사용하기

 

Test Case 를 각 class 안에 정의해서 코드를 작성해봄

import pytest

# Test Case: 결과값 = 입력값 + 1
class TestClass0:   
    def inc(x):
        return x + 1

    def test_answer():
        assert TestClass0.inc(3) == 5

# Test Case: 결과값 = 두 입력값 더하기
class TestClass1:   
    def add(a,b):
        return a + b
    
    def test_answer():
        assert TestClass1.add(1,2) == 4
        
# Test Case: 두 글자 비교
class TestClass2:
    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")

 

 

세개의 Test Case 모두 Fail 로 만들어두어서 pytest 결과는 모두 Fail

 

 

class 안에 여러개 task 를 작성해서 수행할 수도 있음

# Test Case: 글자 비교
class TestClass2:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")

 

3개의 클래스에 4개의 테스트케이스로 코드를 수정해서 실행해보면

아래처럼 실행결과를 확인할 수 있음

 

반응형

댓글