一.pytest框架中使用setup、teardown、更灵活按照用例级别可以分为以下几类:

1.模块级:(setup_module、teardown_module)在模块始末调用

2.函数级:(setup_function、teardown_function)在函数始末调用 在类外部

3.类级:(setup_class、teardown_class)在类始末调用 在类中

4.方法级:(setup_method、teardown_method)在方法始末调用 在类中

5.方法级:(setup、teardown)在方法始末调用 在类中

二.调用顺序

setup_module>setup_class>setup_method>setup>teardown>teardown_method>teardown_class>teardown_module

三.实例

#!/usr/bin/env python
# _*_coding: utf-8 _*_ def setup_module():
print("\nsetup_module, 只执行一次,当有多个测试类的时候使用") def teardown_module():
print("\nteardown_module, 只执行一次,当有多个测试类的时候使用") class TestPytest1(object): @classmethod
def setup_class(cls):
print("\nsetup_class1, 只执行一次") @classmethod
def teardown_class(cls):
print("\nteardown_class1,只执行一次") def setup_method(self):
print("\nsetup_method, 每个测试方法执行一次") def teardown_method(self):
print("\nteardown_method, 每个测试方法执行一次") def test_three(self):
print("test_three, 测试用例") def test_four(self):
print("test_four, 测试用例") class TestPytest2(object): @classmethod
def setup_class(cls):
print("\nsetup_class2, 只执行一次") @classmethod
def teardown_class(cls):
print("\nteardown_class2,只执行一次") def setup_method(self):
print("\nsetup_method2, 每个测试方法执行一次") def teardown_method(self):
print("\nteardown_method2, 每个测试方法执行一次") def test_one(self):
print("test_one, 测试用例") def test_two(self):
print("test_two, 测试用例")

四.执行结果

Testing started at 15:06 ...
C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_run_setup.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_run_setup.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
plugins: html-2.1.1, metadata-1.11.0collected 4 items

test_run_setup.py
setup_module, 只执行一次,当有多个测试类的时候使用

setup_class1, 只执行一次

setup_method, 每个测试方法执行一次
.test_three, 测试用例

teardown_method, 每个测试方法执行一次

setup_method, 每个测试方法执行一次
.test_four, 测试用例

teardown_method, 每个测试方法执行一次

teardown_class1,只执行一次

setup_class2, 只执行一次

setup_method2, 每个测试方法执行一次
.test_one, 测试用例

teardown_method2, 每个测试方法执行一次

setup_method2, 每个测试方法执行一次
.test_two, 测试用例

teardown_method2, 每个测试方法执行一次

teardown_class2,只执行一次

teardown_module, 只执行一次,当有多个测试类的时候使用
[100%]

============================== 4 passed in 0.04s ==============================

Process finished with exit code 0

pytest学习笔记(pytest框架结构)的更多相关文章

  1. [转载]pytest学习笔记

    pytest学习笔记(三)   接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, ...

  2. pytest 学习笔记一 入门篇

    前言 之前做自动化测试的时候,用的测试框架为Python自带的unittest框架,随着工作的深入,发现了另外一个框架就是pytest (官方地址文档http://www.pytest.org/en/ ...

  3. Pytest学习笔记3-fixture

    前言 个人认为,fixture是pytest最精髓的地方,也是学习pytest必会的知识点. fixture用途 用于执行测试前后的初始化操作,比如打开浏览器.准备测试数据.清除之前的测试数据等等 用 ...

  4. Pytest学习笔记(一) 环境安装及入门

    简介 pytest是python的一个单元测试框架,类似于unittest,相对unittest来说,pytest使用更简单,功能更强大. 安装 pip3 install -U pytest 查看版本 ...

  5. Pytest学习笔记8-参数化

    前言 我们在实际自动化测试中,某些测试用例是无法通过一组测试数据来达到验证效果的,所以需要通过参数化来传递多组数据 在unittest中,我们可以使用第三方库parameterized来对数据进行参数 ...

  6. pytest学习笔记

    From: https://blog.csdn.net/gaowg11/article/details/54910974 由于对测试框架了解比较少,所以最近看了下pytest测试框架,对学习心得做个记 ...

  7. pytest学习笔记(一)

    这两天在学习pytest,之前有小用到pytest,觉得这个测试框架很灵巧,用在实现接口自动化(pytest+requests)非常的轻便,然后很有兴致的决定学习下,然后又发现了pytest-sele ...

  8. Pytest学习笔记5-conftest.py的用法

    前言 在之前介绍fixture的文章中,我们使用到了conftest.py文件,那么conftest.py文件到底该如何使用呢,下面我们就来详细了解一下conftest.py文件的特点和使用方法吧 什 ...

  9. Pytest学习笔记6-自定义标记mark

    前言 在pytest中,我们可以使用mark进行用例的自定义标记,通过不同的标记实现不同的运行策略 比如我们可以标记哪些用例是生产环境执行的,哪些用例是测试环境执行的,在运行代码的时候指定对应的mar ...

随机推荐

  1. C# 高并发、抢单解决思路

    高并发 高并发(High Concurrency)是互联网分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计保证系统能够同时并行处理很多请求.高并发相关常用的一些指标有响应时间(Respon ...

  2. ADO.NET 帮助类 参数传递 存储过程 分页

    SQLHelper public class SqlHelper { private readonly string _constr = ConfigurationManager.Connection ...

  3. [论文分享] DHP: Differentiable Meta Pruning via HyperNetworks

    [论文分享] DHP: Differentiable Meta Pruning via HyperNetworks authors: Yawei Li1, Shuhang Gu, etc. comme ...

  4. CSS系列 (04):盒模型详解

    盒模型 CSS盒模型分成W3C标准盒模型和IE模型 W3C标准盒模型(默认):box-sizing: content-box padding和border都会撑开盒子,改变盒子的宽度高度 总宽度:wi ...

  5. Redis 设计与实现 6:五大数据类型之字符串

    前文 Redis 设计与实现 2:Redis 对象 说到,五大数据类型都会封装成 RedisObject. typedef struct redisObject { unsigned type:4; ...

  6. Git 仓库拆分

    方案对比 subtree 使用命令 git subtree split -P dirPath -b branchName 将目标文件夹的代码都保存到指定分支.试了下,该方案虽然保留了 commit,但 ...

  7. 怎么判断map不为空

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  8. epoll原理与本质

    我们知道IO模型中有一个NIO模型,像我们平时接触到的dubbo类的RPC框架底层基于Netty作为通讯框架,而Netty实现的IO模型就是NIO模型.而NIO模型中 底层技术就用到了Linux的ep ...

  9. Beta冲刺——第五天

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  10. [论文阅读笔记] node2vec Scalable Feature Learning for Networks

    [论文阅读笔记] node2vec:Scalable Feature Learning for Networks 本文结构 解决问题 主要贡献 算法原理 参考文献 (1) 解决问题 由于DeepWal ...