前提:需要安装pytest和pytest-htm
pip install pytest 和 pip install pytest-html
pytest单元测试中的类名和方法名必须是以test开头的,执行中只能找到test开头的类方法,比unittest更加严谨
案例一
pytest.mian(["--html=./report.html","模块.py"])

案例二
运行指定模块指定类指定用例.冒号分割,并生成测试报告
pytest.main(["--html=./report.html","模块.py::类::test_a_001"])
pytest.mian(["--html=./report.html"])
pytest . main ([ '-x' , '--html=./report.html' , 't12est000.py' ])
-x 出现一条测试用例失败就退出测试
-v 丰富信息模式,输出更详细的用例执行信息
-s 显示print内容
-q 简化结果信息,不会显示每个用例的文件名
跳过
使用@pytest.mark.skip()跳过该用例
- import pytest
- @pytest.mark.skip()
- def test001(self):
- assert 2==2
. 点好,表示用例通过
F 表示失败 Failure
E 表示用例中存在异常 Error
- import csv #导入csv模块
- class ReadCsv():
- def read_csv(self):
- item=[] #定义一个空列表
- file=csv.reader(open("../data/shujucsv.csv","r")) #得到csv的文件对象
- for i in file:
- item.append(i) #将获得的数据添加到列表中
- return item
- r=ReadCsv()
- print(r.read_csv())
- from xml.dom import minidom
- class ReadXml():
- def read_xml(self,first,second):
- file=minidom.parse("../data/shujuxml.xml")
- firstnode=file.getElementsByTagName(first)[0]
- secondnode=firstnode.getElementsByTagName(second)[0].firstChild.data
- return secondnode
Allure 是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如 TestNG 、 Pytest、 JUint 等。它简单易用,易于集成。
首先配置allure的环境变量


验证allure是否配置成功

ps:若出现指令找不到则没有配置成功!!!!!!!!!!!!!!
pip install allure-pytest
@allure.feature 用于描述被测试产品需求
@allure.story 用于描述feature的用户场景,即测试需求
with allure.step(): 用于描述测试步骤,将会输出到报告中
allure.attach 用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图
案例
实现用户登录功能,场景为登录成功和登录失败
- import pytest,allure,os
- class TestClass():
- @allure.feature("登录功能")
- @allure.story("登陆成功")
- def test_success(self):
- assert 1==1
- @allure.feature("登录功能")
- @allure.story("登陆失败")
- def test_fail(self):
- assert 1==2
- if __name__ == '__main__':
- pytest.main(['--alluredir', 'report/result', 'test_allure.py']) # 生成json类型的测试报告
- split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' # 将测试报告转为html格式
- os.system(split) # system函数可以将字符串转化成命令在服务器上运行
pytest和allure效果展示
- import pytest,allure,os
- class TestShop():
- @allure.feature("购物车")
- @allure.story("产品展示")
- def testshow(self):
- with allure.step("查看吉利系列车信息"):
- allure.attach("博越","吉利")
- with allure.step("查看哈佛系列车信息"):
- allure.attach("H7","哈佛")
- if __name__ == '__main__':
- pytest.main(['--alluredir', 'report/result', 'testshow.py'])
- split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
- os.system(split)
案例:
- import pytest,allure,os
- class TestShop():
- @allure.feature("购物车")
- @allure.story("产品展示")
- def testshow(self):
- with allure.step("查看吉利系列车信息"):
- allure.attach("博越","吉利")
- with allure.step("查看哈佛系列车信息"):
- allure.attach("H7","哈佛")
- if __name__ == '__main__':
- pytest.main(['--alluredir', 'report/result', 'testshow.py'])
- split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
- os.system(split)

( ̄▽ ̄)~*------ ٩(๑❛ᴗ❛๑)۶谢谢阅读!!!!!!!!!!!!!