pytest for unit testing

Prerequisite: pytest and pytest-html need to be installed ( generate html test report)
pip install pytest and pip install pytest-html

1: Naming rules

The class name and method name in the Pytest unit test must start with test . Only classes and methods starting with test can be found during execution .
more rigorous
unittest : Setup >> setupclass , teardown >> teardownclass (classwork)
Pytest: setup, setup_class and teardown, teardown_class functions ( same as unittest execution )
Run at the beginning and end of the test method, that is : running a test function will run a setup and teardown
Runs at the beginning and end of the test method , but only executes setup_class and teardown_class once no matter how many test functions there are

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

Run the specified module specified class specified use case, colon split, and generate a test report

 Output report:

Case three:

Directly execute pytest.main() [Automatically find files starting with test or py files ending with test in the current directory ] (pytest_test )

Generate report:

 Pytest call statement

Output report: 

-x Exit the test when a test case fails

-s: show print content

Expansion: skip
Use @pytest.mark.skip() to skip that use case ( function )

 Generate report:

 3: How Pytest works

.dot , indicating that the use case passes
F means failure
E indicates that there is an exception Error in the use case

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

Allure is a lightweight and very flexible open source test reporting framework. It supports most testing frameworks such as TestNG ,
Pytest , JUint , etc. It's simple to use and easy to integrate.
First configure the environment variables of 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

@allure.feature # is used to describe the requirements of the product under test
@allure.story #Used to describe the user scenario of the feature , that is, the test requirement
with allure.step (): # used to describe the test steps, which will be output to the report
allure.attach #Used to enter some additional information into the test report, usually some test data, screenshots, etc.

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函数可以将字符串转化成命令在服务器上运行

Guess you like

Origin blog.csdn.net/shitoureng/article/details/124276292