pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能

conftest.py

import pytest

@pytest.fixture(scope="class")
def class_auto():
    print("")
    print("class-begin")
    yield
    print("class-end")

test_autouse.py

 1 import pytest
 2
 3
 4 @pytest.mark.usefixtures("class_auto")
 5 class TestClass(object):
 6
 7     @pytest.fixture(scope="function", autouse=True)
 8     def funcion_auto(self):
 9         print("begin")
10         yield
11         print("end")
12
13     def test_case1(self):
14         print("test_case1:")
15         assert 0 == 0
16
17     def test_case2(self):
18         print("test_case2:")
19         assert 0 == 0

执行命令

pytest -s test_autouse.py

执行结果:

注意:

1.fixture中的yield需要注意,不能缺

2.上面conftest.py中的fixture,也可以放在test_autouse.py中。效果是一样的

原文地址:https://www.cnblogs.com/moonpool/p/11393661.html

时间: 2024-07-30 19:14:58

pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能的相关文章

pytest文档17-fixture之autouse=True

前言 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦. fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了 调用fixture三种方法 1.函数或类里面方法直接传fixture的函数参数名称 2.使用装饰器@pytest.mark.usefixtures()修饰 3.autouse=True自动使用 用例传f

Pytest系列(3) - setup和teardown的详细使用

如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 用过unittest的童鞋都知道,有两个前置方法,两个后置方法:分别是 setup() setupClass() teardown() teardownClass() Pytest也贴心的提供了类似setup.teardown的方法,并且还超过四个,一共有十种 模块级别:setup_module.teardown_module

4.pytest中固定装置setup和teardown

pytest提供了不同级别的setup和teardown,来完成测试中前置和后置的操作. 1.函数级别:setup_function/teardown_function def test_source(): print('##### 打印功能 #####') def setup_function(): print('----- 开始打印 -----') def teardown_function(): print('----- 结束打印 -----') 2.类级别:setup_class/tea

pycharm运行Pytest警告:passing a string to pytest.main() is deprecated, pass a list of arguments instead.

初学pytest. 将pytest写进Python代码中 不同运行方式都可正常运行     =======================**********************======================================= ============================================================================ 若是没有将pytest写进Python代码,直接运行的话将不能执行pytest: 没

pytest框架之fixture详细使用

本人之前写了一套基于unnitest框架的UI自动化框架,但是发现了pytest框架之后觉得unnitest太low,现在重头开始学pytest框架,一边学习一边记录,和大家分享,话不多说,那就先从pytest框架的精髓fixture说起吧! 简介: fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进: 1.有独立的命名,并通过声明它们从测试函数.模块.类或整个项目中的使用来激活. 2.按模块化的方式实现,每个fixture都可以互相调用. 3.fixt

【Pytest03】全网最全最新的Pytest框架快速进阶篇(fixture应用)

fixtrue修饰器标记的方法通常用于在其他函数.模块.类或者整个工程调用时会优先执行,通常会被用于完成预置处理和重复操作.例如:登录,执行SQL等操作. 完整方法如下:fixture(scope='function',params=None,autouse=False,ids=None,name=None) 参数说明: 1.scope参数:标记方法的作用域.有4个可选值:function(默认,函数).class(类).module(模块).package/session(包) (1)作用域为

pytest的使用

一.python安装 1.windows(server): 双击python-3.6.7-amd64.exe执行安装流程,使用默认安装方式即可. 安装完成后查看是否安装成功: C:\Users\Administrator>python --version 'python' is not recognized as an internal or external command, operable program or batch file. 如果出现不识别python的情况,需要将python的

pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport)

前言 pytest提供的很多钩子(Hooks)方法方便我们对测试用例框架进行二次开发,可以根据自己的需求进行改造. 先学习下pytest_runtest_makereport这个钩子方法,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果. pytest_runtest_makereport 先看下相关的源码,在_pytest/runner.py下,可以导入之后,点进去查看 from _pytest import runner # 对应源码 def pytest_runtest_make

用 pytest 测试 python 代码

Pytest 是一个比较成熟且功能完备的 Python 测试框架.其提供完善的在线文档,并有着大量的第三方插件和内置帮助,适用于许多小型或大型项目.Pytest 灵活易学,打印调试和测试执行期间可以捕获标准输出,适合简单的单元测试到复杂的功能测试.还可以执行 nose, unittest 和 doctest 风格的测试用例,甚至 Django 和 trial.支持良好的集成实践, 支持扩展的 xUnit 风格 setup,支持非 python 测试.支持生成测试覆盖率报告,支持 PEP8 兼容的