- pytest简介
- pytest和unittest区别
- pytest使用自带的python-html 生成测试报告
- 由于自带生成测试报告的东西不是界面过于简单和不太美观可以使用allure
- 使用之前得要allure的环境变量(选择allure-2.13.2bin)python需下载allure_pytest
- Allure常用的几个特性
- 读取文件(和unittest是一样的,拿到数据后就可以进行操作)
- 读取xml文件
- 读取csv文件
pytest命名规则
使用pytest之前需要安装一下pytest : pip install pytest
类名必须以Test开头,必须是大写开头,也可以_test()结尾,类中的方法也必须以test开头,执行时值匹配以app开头的类和方法
pytest和unittest区别共同点:
他们都有共同的方法setup(), teardown(), setupclass(), teardownclass()
执行顺序 setupclass() -> setup() ->teardown() -> teardownclass()
区别:
import pytest class TestClass: def test01(self): passpytest使用自带的python-html 生成测试报告
生成测试报告之前需要安装pytest-html: pip install pytest-html
import pytest
class TestClass():
def setUp(self):
print('setUp')
def test01(self):
print('test01')
@pytest.mark.skip() # 跳过该用例(函数)
def test02(self):
print('test02')
def tearDown(self):
print('tearDown')
if __name__ == '__main__':
# 运行指定模块下,运行所有test开头的类和测试用例
# -x出现一条测试用例失败就退出测试
# -s:显示print内容
pytest.main(['--html=./huuu_test/report.html','ohh.py','-s'])
结果:
补充
. 点号,表示用例通过
F 表示失败 Failure
E 表示用例中存在异常 Error
allure是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。
使用之前得要allure的环境变量(选择allure-2.13.2bin)python需下载allure_pytestpip install allure_pytest
使用前要下载的文件
链接:https://pan.baidu.com/s/1ndOyEHi_TQPQld8YLZgsjw
提取码:88ff
出现这个情况就是配置成功
@allure.feature # 用于描述被测试产品需求 @allure.story # 用于描述feature的用户场景,即测试需求 with allure.step(): # 用于描述测试步骤,将会输出到报告中 allure.attach # 用于向测试报告中输入一些附加的信息,通常是一些
demo
import os
import pytest
import allure
class TestClass005():
@allure.feature("登录") # 用于定义被测试的功能,被测产品的需求点
@allure.story("汽车系统") # 用于定义被测功能的用户场景,即子功能点
def test_login(self):
assert 1 == 1
@allure.feature("汽车") # 用于定义被测试的功能,被测产品的需求点
@allure.story("买入汽车") # 用于定义被测功能的用户场景,即子功能点
def test_cat(self):
with allure.step("查看红旗系列车信息"):
allure.attach('H8', '红旗')
allure.attach('带话筒', '红旗')
with allure.step("查看大众系列车信息"):
allure.attach("带字母", '大众')
allure.attach("w9", '大众', )
if __name__ == '__main__':
pytest.main(['--alluredir', 'report/result', 'huuu_test.py']) # 生成json类型的测试报告
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' # 将测试报告转为html格式
os.system(split) # system函数可以将字符串转化成命令在服务器上运行
读取文件(和unittest是一样的,拿到数据后就可以进行操作)
读取xml文件
class Readxml():
def read_xml(self, filename, onename, twoname):
root = minidom.parse(filename)
firstnode = root.getElementsByTagName(onename)[0]
secondnode = firstnode.getElementsByTagName(twoname)[0].firstChild.data
return secondnode
r = Readxml()
a = r.read_xml('../data/add.xml', 'k1', 'k2')
b = r.read_xml('../data/add.xml', 'k1', 'k3')
c = r.read_xml('../data/add.xml', 'k1', 'k4')
print(1,2,3)
读取csv文件
csv文件数据
1,3,4 3,4,7
import pytest
import csv # 导入csv模块
class ReadCsv():
def read_csv(self, path):
item = [] # 定义一个空列表
c = csv.reader(open(path, "r")) # 得到csv文件对象
for csv_i in c:
item.append(csv_i) # 将获取的数据添加到列表中
return item
path = '../data/add.csv'
r = ReadCsv()
lists = r.read_csv(path)
class TestClass():
def setUp(self):
print('setUp')
def test01(self):
su = 0
for i in lists:
su = int(i[0]) + int(i[1])
num = int(i[2])
assert num == su
# @pytest.mark.skip() # 跳过该用例(函数)
def test02(self):
num = int(lists[1][0]) + int(lists[1][1])
su = lists[1][2]
assert num == int(su)
def tearDown(self):
print('tearDown')
if __name__ == '__main__':
# 运行指定模块下,运行所有test开头的类和测试用例
pytest.main(['--html=./huuu_test/report.html', 'huuu_test.py', '-s'])
练习题
import pytest
import allure
import os
import csv # 导入csv模块
from demo.demo import CateClass
class ReadCsv():
def read_csv(self, path):
item = [] # 定义一个空列表
c = csv.reader(open(path, "r")) # 得到csv文件对象
for csv_i in c:
item.append(csv_i) # 将获取的数据添加到列表中
return item
path = '../data/add.csv'
rc = ReadCsv()
su = rc.read_csv(path)
list01 = su[0]
list02 = su[1]
c = CateClass()
class TestClass():
@allure.feature('加法')
@allure.story('实际结果')
def test_add(self):
with allure.step('加法'):
su = c.add(int(list01[0]), int(list01[1]))
assert int(list01[2]) == su
@allure.feature('减法')
@allure.story('实际结果')
def test_reduct(self):
with allure.step('减法'):
su = c.add(int(list02[0]), int(list02[1]))
assert su == int(list02[2])
if __name__ == '__main__':
pytest.main(['--alluredir', 'report/result', 'huuu_test.py']) # 生成json类型的测试报告
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' # 将测试报告转为html格式
os.system(split) # system函数可以将字符串转化成命令在服务器上运行



