#firtures通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境
# setup_module/teardown_module:在当前文件中,在所有测试用例执行之前与之后执行,只执行一次;
# setup_function/teardown_function:在每个测试函数之前与之后执行;
# setup/teardown:在每个测试函数之前与之后执行;

# 在当前文件下打开cmd窗口执行:pytest -s test_fixtures_01.py

#功能函数
def multiply(a,b):
return a * b

#===============Firtures=============
def setup_module(module):
print('setup_module------------>')

def teardown_module(module):
print('teardown_module--------->')

def setup_function(function):
print('setup_function---------->')

def teardown_function(function):
print('teardown_function-------->')

def setup():
print('setup---------->')

def teardown():
print('teardown------->')

#==========测试用例==========
def test_multiply_3_4():
print('test_numbers_3_4')
assert multiply(3,4) == 12

def test_multiply_a_3():
print('test_numbers_a_3')
assert multiply('a',3) == 'aaa'

#pytest 是支持使用测试类的,同样必须以Test开头,注意首字母大写。
# setup_class/teardown_class:在当前测试类的开始与结束时执行,只执行一次;
# setup_module/teardown_module:在每个测试方法开始与结束时执行;
# setup/teardown:在每个测试方法开始与结束时执行;

# 在当前文件下打开cmd窗口执行:pytest -s test_fixtures_02.py

#功能函数
def multiply(a,b):
return a * b

class TestMultiply:
#===============Firtures=============
@classmethod
def setup_class(cls):
print('setup_class------------>')

@classmethod
def teardown_class(cls):
print('teardown_class--------->')

def setup_module(self,module):
print('setup_module------------>')

def teardown_module(self,module):
print('teardown_module--------->')

def setup(self):
print('setup---------->')

def teardown(self):
print('teardown------->')

#==========测试用例==========
def test_multiply_5_6(self):
print('test_numbers_5_6')
assert multiply(5,6) == 30

def test_multiply_b_3(self):
print('test_numbers_b_3')
assert multiply('b',3) == 'bbb'

python + pytest 基本使用方法(Fixture)的更多相关文章

  1. python + pytest基本使用方法(拓展库)

    一.测试钩子配置文件 import pytest# conftest.py 是pytest特有的本地测试配置文件;# 既可以用来设置项目级别的Fixture,也可用来导入外部插件,还可以指定钩子函数# ...

  2. python + pytest基本使用方法(运行测试&测试报告)

    import pytest# 1.运行名称中包含某字符串的测试用例#名称中含add 的测试用例# 执行: pytest -k add test_assert.py# 2.减少测试的运行冗长# 执行: ...

  3. python + pytest基本使用方法(参数化)

    import pytestimport math#pytest 参数化#'base,exponent,expected'用来定义参数的名称.# 通过数组定义参数时,每一个元组都是一条测试用例使用的测试 ...

  4. python + pytest基本使用方法(断言)

    #pytest 的基本用法# 安装: pip install pytest#在当前目录下运行 : 输入 pytest# 1.断言#功能:用于计算a与b相加的和def add(a,b): return ...

  5. python pytest测试框架介绍二

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

  6. python:pytest中的setup和teardown

    原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...

  7. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...

  8. Pytest(3)fixture的使用

    fixture的优势 Pytest的fixture相对于传统的xUnit的setup/teardown函数做了显著的改进: 命名方式灵活,不局限于 setup 和teardown 这几个命名 conf ...

  9. python+pytest接口自动化(11)-测试函数、测试类/测试方法的封装

    前言 在python+pytest 接口自动化系列中,我们之前的文章基本都没有将代码进行封装,但实际编写自动化测试脚本中,我们都需要将测试代码进行封装,才能被测试框架识别执行. 例如单个接口的请求代码 ...

随机推荐

  1. 如何为应用选择最佳的FPGA(上)

    如何为应用选择最佳的FPGA(上) How To Select The Best FPGA For Your Application 在项目规划阶段,为任何一个项目选择一个FPGA部件是最关键的决策之 ...

  2. 与现代传感器的接口:轮询ADC驱动程序

    与现代传感器的接口:轮询ADC驱动程序 Interfacing with modern sensors: Polled ADC drivers 我们研究了在现代嵌入式应用程序中,开发人员应该如何创建一 ...

  3. java.lang.ClassNotFoundException: org.apache.curator.RetryPolicy

    dubbo项目启动过程中遇到这个异常,很明显是找不到curator的包,所以需要引入curator的相关包才可以, curator是zookeeper的客户端框架,且要引入完整,才不会报错 比如: & ...

  4. 「题解」NWRRC2017 Joker

    本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题目链接:洛谷 P7028.gym101612J. 题意概述 有一个长度为 \(n\) 的数列,第 \(i\) 个元素的值为 \(a ...

  5. 面试官:为什么Mysql中Innodb的索引结构采取B+树?

    前言 如果面试官问的是,为什么Mysql中Innodb的索引结构采取B+树?这个问题时,给自己留一条后路,不要把B树喷的一文不值.因为网上有些答案是说,B树不适合做文件存储系统的索引结构.如果按照那种 ...

  6. 【模拟7.19】那一天我们许下约定(组合数学,DP)

    看了题目名字深切怀疑出题人是不是失恋了,然后出题折磨我们.然后这题就愉快的打了个暴力,最后莫名其妙wa20,伤心..... 其实这题正解不是很难想,如果说把暴力的DP搞出来,正解也差不到哪去了, 我们 ...

  7. Pytest学习笔记4-assert断言

    前言 pytest作为单元测试框架,自然少不了断言功能,用过unittest的人都知道,在unittest中有丰富的断言方法,比如assertEqual().assertIn().assertTrue ...

  8. 为什么 Python 没有函数重载?如何用装饰器实现函数重载?

    英文:https://arpitbhayani.me/blogs/function-overloading 作者:arprit 译者:豌豆花下猫("Python猫"公众号作者) 声 ...

  9. Luat Demo | 一文读懂,如何使用Cat.1开发板实现Camera功能

    让万物互联更简单,合宙通信高效便捷的二次开发方式Luat,为广大客户提供了丰富实用的Luat Demo示例,便于项目开发灵活应用. 本期采用合宙全新推出的VSCode插件LuatIDE,为大家演示如何 ...

  10. 整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Stream专门用于事件驱动的微服务系统,使用消息中间件来收发信息.使用Spring ...