Pytest操作方法

1.先写一个方法

class Calc():
def add(self,a,b):
c = a+b
return c
def jian(self,a,b):
c = a-b
return c
a = Calc()
c = a.jian(3,2)
print©
Pytest操作方法

2.验证方法是否正确

①正确的情况下

import pytest #导入pytest模块
from funcDemo.Calc import Calc #将上一步方法导入进来
class TestClass():#创建一个类
def setup(self):
print(‘setup…start’)
def setup_class(self):
print(‘setup_class…start’)
def test001(self):#调用加法
c = Calc()
a = c.add(1,2)
assert a == 3
def test002(self):#调用减法
c = Calc()
a = c.jian(2,3)
assert a == -1
def teardown(self):
print(‘teardown…end’)
def teardown_class(self):
print(‘teardown_class…end’)
if name == ‘main’:
pytest.main([’-s’,"–html=./report.html",“test1.py::TestClass::test002”])

demo:

Pytest操作方法
打印的结果:
Pytest操作方法

②错误的情况下

Pytest操作方法

运行结果:

会给你报出定义的那个类方法有错误
Pytest操作方法

③.只运行一个函数方法

Pytest操作方法

运行结果:

test001的错误结果不会保存, 只会运行test002的方法
Pytest操作方法

3.生成测试报告

Pytest操作方法

运行结果:

Pytest操作方法
Pytest操作方法