class StudentData: """ """ def __init__(self): self.__data = None def connect(self, datafile): with open(datafile) as jsonfile: self.__data = json.load(jsonfile) def getdata(self, name): for stu in self.__data['students']: if stu['name'] == name: return stu def close(self): pass
from model import StudentData import pytest #需要安裝 進入:C:\Program Files (x86)\Thonny\Scripts 在開始CMD 運行 pip install pytest pip install pytest-html import pytest_html @pytest.fixture(scope='module') def db(): """ :return: """ print('*****SETUP*****') db = StudentData() db.connect('data.json') yield db print('******TEARDOWN******') db.close() def testScottData(db): """ :param db: :return: """ scottdata = db.getdata('Jason') assert scottdata['id'] == 1 assert scottdata['name'] == 'geovindu' assert scottdata['result'] == 'pass' def testMarkData(db): """ :param db: :return: """ markdata = db.getdata('Dau') assert markdata['id'] == 2 assert markdata['name'] == 'sibodu' assert markdata['result'] == 'fail' if __name__ == '__main__': """ """ # #pytest.main() pytest.main(['-v','geovindu.py','--html=geovindu.html']) #生成测试报告 ::bll
标签:__,python,self,db,assert,pytest,IDE,def From: https://www.cnblogs.com/geovindu/p/18470493