python 단위 테스트 의 pytest 사용

전제 준비
1.전제:pytest 와 pytest-html 를 설치 해 야 합 니 다(html 테스트 보고서 생 성)
pip install pytest 와 pip install pytest-html 
설치 플러그 인:pip install 플러그 인 이름
2.명명 규범
 Pytest 유닛 테스트 의 클래스 이름과 방법 명 은 test 로 시작 해 야 합 니 다.실행 중 test 로 시작 하 는 클래스 와 방법 만 찾 을 수 있 습 니 다.unittest 보다 더욱 엄밀 합 니 다.
Pytest: setup, setup_class 와 teardown,teardownclass 함수(unittest 실행 효과 와 마찬가지)는 테스트 방법의 전말 에 실 행 됩 니 다.즉,테스트 함 수 를 한 번 실행 하면 setup 과 teardown 이 테스트 방법의 전말 에 실 행 됩 니 다.그러나 아무리 많은 테스트 함수 가 있어 도 setup 만 실 행 됩 니 다.class 와 teardownclass
2.pytest 자체 html 테스트 보고서 생 성
전제조건:pytest-html 모듈(python 자체 생 성 테스트 보고서 모듈)을 다운로드 해 야 합 니 다.
pip install pytest-html
pytest-html 를 설치 하지 않 으 면 다음 과 같이 보고 합 니 다:

사례:1)
pytest.main("모듈.py")[지정 모듈 을 실행 하면 모든 test 시작 클래스 와 테스트 용례 를 실행 합 니 다] 
 pytest.main("--html=./report.html","모듈.py")

import pytest
class Test():
    def test1(self):
        print("    1")
    def test1(self):
        print("    2")
if __name__ == '__main__':
    pytest.main(["--html=./report.html", "test_004.py"])
결과:


2)지정 모듈 지정 클래스 지정 용례,콜론 분할 을 실행 하고 테스트 보고 서 를 생 성 합 니 다.
pytest.main(['--html=./report.html','모듈.py::클래스:testa_001'])

import pytest
class Test():
    def test1(self):
        print("    1")
    def test2(self):
        print("    2")
if __name__ == '__main__':
    pytest.main(["--html=./report.html", "test_004.py::Test::test1"])
결과:

3)pytest.main()을 직접 실행 합 니 다.[현재 디 렉 터 리 에서 test 로 시작 하 는 파일 이나 test 로 끝 나 는 py 파일 을 자동 으로 찾 습 니 다.]
pytest.main([‘--html=./report.html'])
문장:pytst.main(['-x','-html=./report.html','t12Est000.py')
-x 테스트 용례 가 나타 나 면 테스트 를 종료 합 니 다.
-s:인쇄 내용 표시
3.pytest 실행 방식
점호
F 실패 표시 실패
E 는 용례 에 이상 오류 가 있 음 을 나타 낸다
매력  
Allure 는 경량급 이 고 매우 유연 한 오픈 소스 테스트 보고서 프레임 워 크 이다.TestNG,Pytest,JUint 등 대부분의 테스트 프레임 워 크 를 지원 합 니 다.그것 은 간단 하고 쓰기 쉬 워 서 집적 하기 쉽다  
1.Allure 가 자주 사용 하 는 몇 가지 특성
@allure.feature\#테스트 대상 제품 의 수 요 를 설명 하 는 데 사 용 됩 니 다.
@allure.story\#feature 를 설명 하 는 사용자 장면,즉 테스트 수요
with allure.step():\#테스트 절 차 를 설명 하 는 데 사용 되 며 보고서 에 출력 됩 니 다.
allure.attach\#테스트 보고서 에 추가 정 보 를 입력 하 는 데 사 용 됩 니 다.보통 테스트 데이터,캡 처 등 입 니 다.
사례 1:pytest 와 Allure 생 성 html 테스트 용례 rr.csv
2,3,5
5,6,11
readCsv

import csv  #   csv  
 
 
class ReadCsv():
    def read_csv(self):
        item = []  #        
        c = csv.reader(open("../dataDemo/rr.csv", "r"))  #   csv    
        for csv_i in c:
            item.append(csv_i)  #             
        return item
 
 
r = ReadCsv()
print(r.read_csv())
개발 코드:

class Cale():
    def jia(self,a,b):
        c=a+b
        return c
    def jian(self,a,b):
        c=a-b
        return c
    def cheng(self,a,b):
        c=a*b
        return c
    def chu(self,a,b):
        c=a/b
        return c
html 코드 생 성:

import pytest
from pytest01.readDemo.readCsv import ReadCsv
from pytest01.demo.cale import Cale
import os
import allure
r=ReadCsv()
cc=r.read_csv()
d=Cale()
class Test():
    @allure.story("        ")
    def test001(self):
        for i in cc:
            dd=d.jia(int(i[0]),int(i[1]))
            assert dd==int(i[2])
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'test_02.py'])
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    os.system(split)


python 유닛 테스트 의 pytest 사용 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 pytest 사용 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기