一、用例运行级别

1、函数级别(setup、teardown 或 setup_function、teardown_function):

仅对处于同作用域的测试函数有效(该函数定义不在类中,则对非类中测试函数有效;若该函数定义在类中,则对类中测试函数有效)

def setup_function():        #  setup()也一样
print("setup_function") def teardown_function(): # teardown()也一样
print("teardown_function") def test_01():
print("---用例a执行---") class TestCase():
def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") def test_04():
print("---用例d执行---") 输出结果:
test_fixture2.py setup_function
---用例a执行---
.teardown_function
---用例b执行---
.---用例c执行---
.setup_function
---用例d执行---
.teardown_functio

2、方法级别(setup_method、teardown_method):

该函数只能定义在类中,且对类中的测试函数均有效

def test_01():
print("---用例a执行---") class TestCase():
def setup_method(self):
print("setup_method") def teardown_method(self):
print("teardown_method") def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") 输出结果:
test_fixture2.py ---用例a执行---
.setup_method
---用例b执行---
.teardown_method
setup_method
---用例c执行---
.teardown_method

3、类级别(setup_class、teardown_class):

该函数只能定义在类中,且分别在类开始和结束前各执行一次

def test_01():
print("---用例a执行---") class TestCase():
def setup_class(self):
print("setup_class") def teardown_class(self):
print("teardown_class") def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") 输出结果:
test_fixture2.py ---用例a执行---
.setup_class
---用例b执行---
.---用例c执行---
.teardown_class

4、模块级别(setup_module、teardown_module):

该函数只可定义在非类中,且分别在所有测试函数开始和结束前各执行一次

def setup_module():
print("setup_module") def teardown_module():
print("teardown_module") def test_01():
print("---用例a执行---") class TestCase(): def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") def test_04():
print("---用例d执行---") 输出结果:
setup_module
---用例a执行---
.---用例b执行---
.---用例c执行---
.---用例d执行---
.teardown_modul

5、不同级别函数混合:

运行的优先级:module > function > class > method > setup、teardown

def setup_module():
print("setup_module") def teardown_module():
print("teardown_module") def setup_function():
print("setup_function") def teardown_function():
print("teardown_function") def test_01():
print("---用例a执行---") class TestCase(): def setup(self):
print("setup") def teardown(self):
print("teardown") def setup_method(self):
print("setup_method") def teardown_method(self):
print("teardown_method") def setup_class(self):
print("setup_class") def teardown_class(self):
print("teardown_class") def test_02(self):
print("---用例b执行---") def test_03(self):
print("---用例c执行---") 输出结果:
test_fixture2.py setup_module
setup_function
---用例a执行---
.teardown_function
setup_class
setup_method
setup
---用例b执行---
.teardown
teardown_method
setup_method
setup
---用例c执行---
.teardown
teardown_method
teardown_class
teardown_module

参考:https://www.cnblogs.com/yoyoketang/p/9374957.html

pytest测试框架 -- setup和teardown等的更多相关文章

  1. Pytest测试框架(二):pytest 的setup/teardown方法

    PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...

  2. Pytest测试框架(三):pytest fixture 用法

    xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...

  3. 『德不孤』Pytest框架 — 1、Pytest测试框架介绍

    目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...

  4. pytest测试框架 -- 简介

    一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...

  5. Pytest测试框架(一):pytest安装及用例执行

    PyTest是基于Python的开源测试框架,语法简单易用,有大量的插件,功能非常多.自动检测测试用例,支持参数化,跳过特定用例,失败重试等功能. 安装 pip install -U pytest  ...

  6. Pytest测试框架(五):pytest + allure生成测试报告

    Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...

  7. python pytest测试框架介绍三

    之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...

  8. python pytest测试框架介绍二

    在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...

  9. Pytest 测试框架

    一 . Pytest 简介 Pytest是python的一种单元测试框架. 1. pytest 特点 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试 ...

随机推荐

  1. vue element Admin - 修改浏览器标签名 + 添加tagView标签 +固定导航头部 + 添加侧边栏Logo

    1 .修改浏览器标签名称: 修改浏览器标签名称在文件:\src\settings.js   image.png 2 .修改固定头部Header和侧边栏 Logo:   image.png 1)侧边栏文 ...

  2. QUIC协议详解之Initial包的处理

    从服务器发起请求开始追踪,细说数据包在 QUIC 协议中经历的每一步.大量实例代码展示,简明易懂了解 QUIC. 前言 本文介绍了在 QUIC 服务器在收到 QUIC 客户端发起的第一个 UDP 请求 ...

  3. 一、Spring的基本应用

    1.spring导包 导入maven包 <dependencies> <dependency> <groupId>org.springframework</g ...

  4. 对于python装饰器结合递归的进一步理解

    对于python装饰器结合递归的进一步理解 代码如下: import functools def memoize(fn): print('start memoize') known = dict() ...

  5. 用python进行实际地址经纬度提取

    实际地址经纬度提取 请求接口: https://apis.map.qq.com/ws/place/v1/suggestion/ 所需参数: 参数名称 是否必须 参数类型 说明 keyword 是 St ...

  6. SpringCloude简记_part3

    18. SpringCloud Alibaba Sentinel实现熔断与限流 18.1 Sentiel 官网 https://github.com/alibaba/Sentinel 中文 https ...

  7. java 工具类Integer

    Integer 是lang包下的工具类 为了更加熟悉Integer中的方法使用和理解 进行了一部分代码和原代码的总结 Intrger工具类方法: * * int parseInt(String s) ...

  8. linux云服务器搭建 express后台 nginx转发

    购买云服务器 或者自己在本地搭建一个虚拟机 (我用的是腾讯云云服务器(CVM),或者可以购买阿里云 ecs服务器) 购买完成后 配置安全组 允许http https ftp 端口 一般运营商会提供说明 ...

  9. goalng包和命令工具

    1. 包简介 任何包系统设计的目的都是为了简化大型程序的设计和维护工作,通过将一组相关的特性放进一个独立的单元以便于理解和更新,在每个单元更新的同时保持和程序中其它单元的相对独立性.这种模块化的特性允 ...

  10. CentOS7升级系统内核至4.4.xx版本

    CentOS7.x系统自带的3.10.x内核存在一些Bugs,导致运行的Docker.kubernetes不稳定,建议升级内核,容器使用的坑会少很多 下载内核源 rpm -Uvh http://www ...