Python单元测试之Pytest

前提:需要安装pytest和pytest-html(生成html测试报告)

pip install pytest 和 pip install pytest-html

一、命名规则

Pytest单元测试中的类名和方法名必须是以test开头,执行中只能找到test开头的类和方法,比unittest更加严谨

案例

import pytest from xml.dom

import minidom

class TestPy01():

        def testPy001(self):

                print("第一个pytest")

                assert 1==1

        def testPy002(self):

                print("第二个pytest")

                assert 1==2

        def testPy003(self):

                print("第三个pytest")

                assert 1 == 1

if __name__ == '__main__':

        pytest.main()

unittest:Setup>>  setupclass , teardown >> teardownclass

Pytest:  setup, setup_class和teardown, teardown_class函数(和unittest执行效果一样)

运行于测试方法的始末,即:运行一次测试函数会运行一次setup和teardown

运行于测试方法的始末,但是不管有多少测试函数都只执行一次setup_class和 teardown_class

二、Pytest生成自带的html测试报告

前提条件:需要下载pytest-html模块(python自带的生成测试报告模块)

pip install pytest-html

1.方式一

1.1  格式

pytest.main("模块.py")【运行指定模块下,运行所有test开头的类和测试用例】

pytest.main(["--html=./report.html","模块.py"])

1.2  代码:

pytest.main(["--html=../report1.html", "test_01.py"])

2.方式二

2.1  格式

运行指定模块指定类指定用例,冒号分割,并生成测试报告

pytest.main([‘--html=./report.html’,‘模块.py::类::方法'])

运行指定模块指定类指定用例,冒号分割,并生成测试报告

2.2  代码

pytest.main(["--html=../report1.html", "test_01.py::TestPy01::testPy001"])

3.方式三(无效)

3.1  格式

直接执行pytest.main() 【自动查找当前目录下,以test_开头的文件或者以_test结尾的py文件】

pytest.main([‘--html=./report.html’])

3.2  代码:

pytest.main(["--html=../report1.html"])

4.方式四

Pytest调用语句

pytest.main(['-x','--html=./report.html','test001.py'])

-x:出现一条测试用例失败就退出测试

-v:丰富信息模式, 输出更详细的用例执行信息

-s:显示print内容

-q:简化结果信息,不会显示每个用例的文件名

扩充:跳过

使用@pytest.mark.skip()跳过该用例(函数)

@pytest.mark.skip()

deftest001(self):

        assert 2 == 2

三、Pytest的运行方式

.   点号,表示用例通过

F  表示失败 Failure

E  表示用例中存在异常 Error

思考:实际开发中是直接assert 1==2吗?

四、文件读取

1.读取csv文件

先创建文件,然后读取

import csv #导入csv模块

class ReadCsv():

        def read_csv(self):

                item =[]       #定义一个空列表

                c = csv.reader(open("../commonDemo/test1.csv","r"))       #得到csv文件对象

                for csv_i in c: item.append(csv_i)       #将获取的数据添加到列表中

                return item

r = ReadCsv()

print(r.read_csv())

2.读取xml文件

from xml.dom import minidom

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

五、Allure

Allure是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成。

1.安装allure

1.1  首先配置allure的环境变量

Allure下载

1.2  验证allure是否配置成功

1.3  其次要安装allure

pip install allure-pytest

allure-pytest是Pytest的一个插件,通过它我们可以生成Allure所需要的用于生成测试报告的数据

2.Allure常用的几个特性

@allure.feature       # 用于描述被测试产品需求

@allure.story           # 用于描述feature的用户场景,即测试需求

with allure.step()     # 用于描述测试步骤,将会输出到报告中

allure.attach      # 用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图等

2.1  allure.feature

@allure.feature     # 用于描述被测试产品需求

2.2  allure.story

@allure.story        # 用于描述feature的用户场景,即测试需求

案例

实现用户登录功能,场景为登录成功和登录失败

import pytest,allure,os

class TestClass005():

        @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_06.py'])        #生成json类型的测试报告

        split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'    #将测试报告转为html格式  (注意空格)

        os.system(split)     # system函数可以将字符串转化成命令在服务器上运行

Pytest和allure效果展示

2.3  with allure.step()

用于描述测试步骤,将会输出到报告中

2.4  allure.attach

用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图等

案例

实现产品信息展示,车展中的各种车的品牌

import pytest,os,allure

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', 'test_07.py'])

        split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'

        os.system(split)

Pytest和allure效果展示

你可能感兴趣的:(Python单元测试之Pytest)