1: Naming rules
2: Pytest generates its own html test report
Prerequisites: You need to download the pytest-html module (python 's built-in test report module )
pip install pytest-html
Case number one
pytest.main("module.py ") [run the specified module , run all classes and test cases starting with test]
Generate report
Case 2
Output report:
Case three:
Generate report:
Pytest call statement
Output report:
-x Exit the test when a test case fails
-s: show print content
Generate report:
3: How Pytest works
4: Read the xml file
xml file
Development code module:
Read data module:
Test code module:
Generate report:
Implementation code:
#xml文件
<ento>
<add>
<add1>1</add1>
<add2>2</add2>
<denyu>3</denyu>
</add>
</ento>
#开发代码
class Clac():
def add(self,a,b):
return a + b
def jian(self,a,b):
return a - b
#读取数据
from xml.dom import minidom
class ReadXml():
def readxml(self,path,firsthod,secondnode):
root = minidom.parse(path)
firsthod = root.getElementsByTagName(firsthod)[0]
secondnode = firsthod.getElementsByTagName(secondnode)[0].firstChild.data
return secondnode
#测试代码
from readdata.readxml import ReadXml #获取数据
from assets.clac import Clac #引入开发代码
import pytest,allure,os
readxmlclass = ReadXml()
c = Clac()
aa = int(readxmlclass.readxml("../data/test.xml","add","add1"))
bb = int(readxmlclass.readxml("../data/test.xml","add","add2"))
cc = int(readxmlclass.readxml("../data/test.xml","add","denyu"))
class TestClass():
def test001(self):
result = c.add(int(aa),int(bb))
assert result == int(cc)
4.1 Read csv file
Development code module:
Read data module:
Test code module:
Generate a test report:
Implementation code:
#csv文件
10,20,30
#开发代码
class Clac():
def add(self,a,b):
return a + b
def jian(self,a,b):
return a - b
#读取数据
import csv
class CsvClass():
def csvclass(self):
item = []
c = csv.reader(open("../data/test.csv"))
for csv_i in c:
item.append(csv_i)
return item
r = CsvClass()
print(r.csvclass())
#测试代码
from readdata.redatacsv import CsvClass
from assets.clac import Clac
import pytest
r = CsvClass()
data = r.csvclass()
c = Clac()
class TestCsv():
def test001(self):
for item_i in data:
result = c.add(int(item_i[0]),int(item_i[1]))
assert result == int(item_i[2])
if __name__ == '__main__':
pytest.main(["-s","test_02.py","--html=./report123.html"])
5:allure
Verify that allure is configured successfully
Next, install allure
pip install allure-pytest
allure-pytest is a plugin for Pytest , through which we can generate the data needed by Allure to generate test reports
5.1 : Several features commonly used by Allure
5.1.1:allure.feature
@allure.feature # is used to describe the requirements of the product under test
5.1.2:allure.story
@allure.story #Used to describe the user scenario of the feature , that is, the test requirement
case
Implement user login function, the scenarios are login success and login failure
Pytest and allure effect display
Read xml to generate allure report
Development code:
Read data:
Generate report:
#xml文件
<ento>
<add>
<add1>1</add1>
<add2>2</add2>
<denyu>3</denyu>
</add>
</ento>
#开发代码
class Clac():
def add(self,a,b):
return a + b
def jian(self,a,b):
return a - b
#读取数据
from xml.dom import minidom
class ReadXml():
def readxml(self,path,firsthod,secondnode):
root = minidom.parse(path)
firsthod = root.getElementsByTagName(firsthod)[0]
secondnode = firsthod.getElementsByTagName(secondnode)[0].firstChild.data
return secondnode
#测试代码
from readdata.readxml import ReadXml #获取数据
from assets.clac import Clac #引入开发代码
import pytest,allure,os
readxmlclass = ReadXml()
c = Clac()
aa = int(readxmlclass.readxml("../data/test.xml","add","add1"))
bb = int(readxmlclass.readxml("../data/test.xml","add","add2"))
cc = int(readxmlclass.readxml("../data/test.xml","add","denyu"))
class TestClass():
@allure.feature("加法判断功能") #用于定义被测试的功能,被测产品的需求点
@allure.story("断言成功") #用于定义被测功能的用户场景,即子功能点
def test001(self):
with allure.step("查看相加结果"): #用于描述测试步骤,将会输出到报告中
allure.attach("等于3","1加2")
result = c.add(int(aa),int(bb))
assert result == int(cc)
@allure.feature("等于判断功能")
@allure.story("断言失败")
def test002(self):
assert 1==2
if __name__ == '__main__':
pytest.main(['--alluredir', 'report/result', 'test_01.py']) # 生成json类型的测试报告
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' # 将测试报告转为html格式
os.system(split) # system函数可以将字符串转化成命令在服务器上运行