pytest学习笔记(pytest框架结构)
一.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框架结构)的更多相关文章
- [转载]pytest学习笔记
pytest学习笔记(三) 接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, ...
- pytest 学习笔记一 入门篇
前言 之前做自动化测试的时候,用的测试框架为Python自带的unittest框架,随着工作的深入,发现了另外一个框架就是pytest (官方地址文档http://www.pytest.org/en/ ...
- Pytest学习笔记3-fixture
前言 个人认为,fixture是pytest最精髓的地方,也是学习pytest必会的知识点. fixture用途 用于执行测试前后的初始化操作,比如打开浏览器.准备测试数据.清除之前的测试数据等等 用 ...
- Pytest学习笔记(一) 环境安装及入门
简介 pytest是python的一个单元测试框架,类似于unittest,相对unittest来说,pytest使用更简单,功能更强大. 安装 pip3 install -U pytest 查看版本 ...
- Pytest学习笔记8-参数化
前言 我们在实际自动化测试中,某些测试用例是无法通过一组测试数据来达到验证效果的,所以需要通过参数化来传递多组数据 在unittest中,我们可以使用第三方库parameterized来对数据进行参数 ...
- pytest学习笔记
From: https://blog.csdn.net/gaowg11/article/details/54910974 由于对测试框架了解比较少,所以最近看了下pytest测试框架,对学习心得做个记 ...
- pytest学习笔记(一)
这两天在学习pytest,之前有小用到pytest,觉得这个测试框架很灵巧,用在实现接口自动化(pytest+requests)非常的轻便,然后很有兴致的决定学习下,然后又发现了pytest-sele ...
- Pytest学习笔记5-conftest.py的用法
前言 在之前介绍fixture的文章中,我们使用到了conftest.py文件,那么conftest.py文件到底该如何使用呢,下面我们就来详细了解一下conftest.py文件的特点和使用方法吧 什 ...
- Pytest学习笔记6-自定义标记mark
前言 在pytest中,我们可以使用mark进行用例的自定义标记,通过不同的标记实现不同的运行策略 比如我们可以标记哪些用例是生产环境执行的,哪些用例是测试环境执行的,在运行代码的时候指定对应的mar ...
随机推荐
- 面试 15-虚拟DOM
15-虚拟DOM #前言 vdom 是 vue 和 React 的核心,先讲哪个都绕不开它. vdom 比较独立,使用也比较简单. 如果面试问到 vue 和 React 和实现,免不了问 vdom: ...
- Attention & Transformer
Attention & Transformer seq2seq; attention; self-attention; transformer; 1 注意力机制在NLP上的发展 Seq2Seq ...
- Docker Container 就是一个进程,多新鲜啊?
大家对 Docker 都应该有了或多或少的认识了,相信大家都是从这两张图来粗旷的理解 Docker 及容器概念的 那我们如何更轻松的理解容器 Container 呢?说白了 Container 就是一 ...
- 如何做好Code Review
Code Review(代码审查)很多团队都会做,效果如何不好说.如果你能轻易地从一堆出自正经团队之手的代码里找出几个低级错误,往往意味着团队管理者长期忽视了Code Review的重要性. 根据经验 ...
- java连接mysql数据查询数据
package com.cn.peitest.connectDatabase; import java.sql.Connection; import java.sql.DriverManager; i ...
- [leetcode]48RotateImage二维数组翻转
import java.util.Arrays; /** * You are given an n x n 2D matrix representing an image. Rotate the im ...
- Javaweb前台界面代码复用总结
servlet声明定义message信息传给前天界面判断输出message: if(booknamelist.size()==0) { message="根据书名查询没有结果!"; ...
- http协议中的缓存机制
强缓存 - expires,服务器给客户端一个过期日期,如(2020-12-12),过了该时间,客户端请求服务器重新获取.存在问题:客户端与服务端存在时间差,会导致过期时间不准确 - Cache-co ...
- 在onelogin中使用OpenId Connect Implicit Flow
目录 简介 OpenId Implicit Flow 创建onelogin的配置 页面的运行和请求流程 关键代码 总结 简介 onelogin支持多种OpenId Connect的连接模式,上一篇文章 ...
- Linux服务器初始化调优及安全加固
一,开启iptables 仅开放必要的SSH端口和监控端口 示例:SSH tcp 22snmpd udp 161nrpe tcp 5666本人公网IP全端口开放 二,除非特别熟悉selinux配置,否 ...