系列文章目录

第一章 Pytest单元测试框架基础

第二章 Pytest单元测试框架之fixture装饰器实现前后置


文章目录


系列文章目录

前言

一、pytest安装

二、pytest运行方式

三、Pytest多线程

四、Pytest 用例失败重跑

五、Pytest 的setup和teardown函数



前言

pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点:

  • 简单灵活,容易上手
  • 支持参数化
  • 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests)
  • pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等
  • 测试用例的skip和xfail处理
  • 可以很好的和jenkins集成
  • report框架----allure 也支持了pytest

一、pytest安装

1. 安装,下面两个命令都可以安装

pip install pytest

pip3 install pytest

2. 验证安装 

pytest --version

3. pytest文档

     官方文档:https://docs.pytest.org/en/latest/contents.htmlhttps://links.jianshu.com/go?to=https://docs.pytest.org/en/latest/contents.html

二、pytest运行方式

# file_name: test_abc.py
 import pytest # 引入pytest包

 def test_a(): # test开头的测试函数
     print("------->test_a")
     
 def test_b():
     print("------->test_b")
     
 if __name__ == '__main__':
        pytest.main(['-vs']) # 调用pytest的main函数执行测试

-vs : 控制台显示用例执行的详细信息

1. 测试类主函数模式

pytest.main(['-vs'])

2.命令行模式

pytest 文件路径/测试文件名
例如:pytest ./test_abc.py

三、Pytest多线程

当cases量很多时,运行时间也会变的很长,如果想缩短脚本运行的时长, 就可以用多进程来运行。

1. 安装pytest-xdist

# 下面两个命令都可以安装:

pip install pytest-xdist

pip3 install pytest-xdist

2. 运行模式

pytest test_se.py -n NUM
-n : 表示使用多线程运行
Num :填写线程数
例如10线程:pytest test_se.py -n 10

四、Pytest 用例失败重跑

在做自动化测试时,有时会遇到页面无法加载或元素未加载完成,导致case运行失败,而 这并非是 我们期望的结果,此时可以就可以通过重试运行cases的方式来解决。

1. 安装pytest-rerunfailures

# 下面两个命令都可以安装:
pip install pytest-rerunfailures
pip3 install pytest-rerunfailures

2. 运行模式

pytest test_se.py --reruns NUM
--reruns : 使用重试运行cases
Num :填写失败重试次数
例如当用例失败时重试2次:pytest test_se.py --reruns 2

五、Pytest 的setup和teardown函数

1. 什么是setup、teardown

setup: 用例执行前的操作

teardown:用例执行后的操作

2. setup和teardown主要分为:

模块级:setup_module(),teardown_module()

类级:setup_class(),teardown_class()

函数级: setup(),teardown()

代码示例如下:

函数级:

import pytest

# 测试类01
class Test_login:

  # 函数级开始
  def setup(self):
      print("------->函数级前置")

   # 模块级结束
  def teardown(self):
      print("------->函数级后置")


  def test_a(self):
      print("------->用例1")

  def test_b(self):
      print("------->用例2")

if __name__ == '__main__':
    pytest.main(['-vs'])

运行结果:

Python 搭建自动化测试框架 python自动化框架pytest_selenium

 此时可以看到两条用例都调用了前后置。

类级:

import pytest

# 测试类01
class Test_login:
    # 类级开始
    def setup_class(self):
        print("------->类级前置")

    # 类级结束
    def teardown_class(self):
        print("------->类级后置")

    def test_a(self):
        print("------->用例1")

    def test_b(self):
        print("------->用例2")

if __name__ == '__main__':
    pytest.main(['-vs'])

运行结果:

Python 搭建自动化测试框架 python自动化框架pytest_selenium_02

 使用类级前后之后此时Test_login这个类里面的两条用例只调用了一次前后置函数。

模块级:

import pytest

# 模块级开始
def setup_module():
    print("------->模块级前置")

# 测试类01
class Test_login:

  def test_a(self):
      print("------->用例1")

  def test_b(self):
      print("------->用例2")

# 测试类02
class Test_login01:

    def test_a(self):
        print("------->用例3")

    def test_b(self):
        print("------->用例4")

# 模块级结束
def teardown_module():
    print("------->模块级后置")

if __name__ == '__main__':
    pytest.main(['-vs'])

运行结果:

Python 搭建自动化测试框架 python自动化框架pytest_selenium_03

此时Test_login与Test_login01两个测试类只调用了一次前后置。

 这就是pytest自动化测试框架的基本的一些使用。后面会更新进阶用法。