详解pytest传递参数的几种方式

 更新时间:2024年03月28日 09:31:36   作者:LetsStudy  
本文主要介绍了详解pytest传递参数的几种方式,详细的介绍了4种传参方式,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧

 

测试类内部,属性传递

import pytest

class Test_Case:
    t = 0

    def test_c(self):
        self.t = self.t + 1
        assert self.t == 1

    def test_d(self):
        self.t = self.t + 1
        assert self.t == 1

# t是测试类的属性,可以为所有测试方法共享该值,该值是固定不变的

global方式传递

import pytest

s = {}


class Test_Case:
    
    def test_b(self):
        global s
        s['name'] = 'hello'
        print(s['name'])
        assert s['name'] == 'hello'

    def test_c(self):
        global s
        s['age'] = 18
        print(s)
        assert s['age'] == 18


# global声明的变量可以在整个测试类中共享,值是可变的,global可以去掉,效果相同

@pytest.mark.parametrize()

import pytest

class Test_Case:
    @pytest.mark.parametrize("x", [1, 2, 3, 4])  # 传递单个值
    def test_b(self, x):
        assert x != 5

    @pytest.mark.parametrize("x,y", [(1, 2), (3, 4), (2, 3), (4, 6)])  # 多参数,传递元组
    def test_c(self, x, y):
        print(x + y)
        assert x + y != 5

    @pytest.mark.parametrize("x,y", [{1, 2}, {3, 4}, {2, 3}, {4, 6}])  # 多参数传递集合
    def test_d(self, x, y):
        print(x + y)
        assert x + y != 6

    @pytest.mark.parametrize("x", [{"a": 1, "b": 2}, {"a": 1, "c": 4}])  # 传递字典
    def test_e(self, x):
        print(x)
        assert x["a"] == 1

    @pytest.mark.parametrize("x,y", [({"a": 1, "b": 2}, {"a": 3, "c": 4})])  # 多参数传递字典
    def test_f(self, x, y):
        assert x["a"] == 1

    @pytest.mark.parametrize("x", [{"a": 1, "b": 2}])  # 装饰器叠加,传递多参数
    @pytest.mark.parametrize("y", [{"a": 1, "b": 2}])
    def test_g(self, x, y):
        assert y["a"] == 1

    @pytest.mark.parametrize(
        "test_input,expected",
        [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)],
    )  # xfail标记
    def test_h(self, test_input, expected):
        assert eval(test_input) == expected

    @pytest.mark.parametrize(
        "test_input,expected",
        [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.skip)],
    )  # skip标记
    def test_i(self, test_input, expected):
        assert eval(test_input) == expected

fixtrue传递

import pytest

class Test_Case:

    @pytest.fixture
    def get_d(self):  # 通过fixture值传递
        return [1, 2, 3]

    def test_a(self, get_d):
        x = get_d[0]
        assert x == 1
import pytest

class Test_Case:
# params = 'hello'等同于params = ['h','e','l','l','o']
    @pytest.fixture(params='hello') 
    def get_c(self, request):
        print(request.param)
        return request.param

    def test_c(self, get_c):
        name = get_c
        assert name == 'h'

    @pytest.fixture(params=[1, 2], ids=['hello', 'name'])  # 可以通过pytest -k <ids>执行指定的用例
    def get_d(self, request):
        return request.param

    def test_d(self, get_d):
        name = get_d
        assert name == 2

    @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
    def data_set(self, request):
        return request.param

    def test_f(self):
        pass
import pytest

#fixture嵌套传递
class Test_Case:

    @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
    def data_set(self, request):
        return request.param

    @pytest.fixture()
    def data_s(self, data_set):
        print(data_set)
        return data_set

    def test_g(self, data_s):
        assert data_s == 1
# yield传递
import pytest

class Test_Case:
    @pytest.fixture
    def s(self):
        c = 'test'
        yield c

    def test_name(self, s):
        assert s == "test"

配置文件传递

# test_case.py
import pytest
import _case.constant as d

class Test_Case:

    def test_g(self):
        d.data = 2
        assert d.data == 2

    def test_h(self):
        assert d.data == 2

# _case.constant.py
data = 1

# 和global使用类似,多个测试文件共享值,但是多个文件共享该值时,会收到测试文件的执行顺序影响
#  global只能在一个测试文件中共享值

conftest.py

# conftest.py最好是在项目根目录或者测试文件所在目录
import pytest

@pytest.fixture(scope='session')
def say():
    return 'hello'

# test_case.py
import pytest


class Test_Case:

    def test_g(self, say):
        assert say == 'hello'

命令行参数传参

# conftest.py   全局变量使用
import pytest

def pytest_addoption(parser):
    parser.addoption("--file", default="test")

@pytest.fixture
def file_name(request):
    return request.config.getoption("--file")

# test_case.py
import pytest


class Test_Case:

    def test_name(self, file_name):
        assert file_name == "test"

# test_case.py或者直接在测试文件中通过pytestconfig获取,示例如下
    def test_name(self, pytestconfig):
        print(pytestconfig.getoption('file'))
        assert pytestconfig.getoption("file") == "test"

钩子函数传参

# conftest.py
import pytest

def pytest_addoption(parser):
    parser.addoption("--file", default="test")

def pytest_generate_tests(metafunc):
    file = metafunc.config.getoption('--file')
    metafunc.parametrize("case_data", [file])

# test_case.py
import pytest


class Test_Case:

    def test_g(self, case_data):
        assert case_data == 'test'

到此这篇关于详解pytest传递参数的几种方式的文章就介绍到这了,更多相关pytest传递参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

 

相关文章

  • Win下PyInstaller 安装和使用教程

    Win下PyInstaller 安装和使用教程

    pyinstaller是一个非常简单的打包python的py文件的库,这篇文章主要介绍了PyInstaller-Win安装和使用教程,本文通过流程实例相结合给大家介绍的非常详细,需要的朋友可以参考下
    2019-12-12
  • 聊聊python中的load、loads实现反序列化的问题

    聊聊python中的load、loads实现反序列化的问题

    在python自动化中,我们传递一些参数是需要从文件中读取过来的,读取过来的字典并非python对象数据类型而是string类型。本文给大家分享python中的load、loads实现反序列化的问题,感兴趣的朋友一起看看吧
    2021-10-10
  • Matlab读取excel并利用拉依达准则筛选数据的全过程

    Matlab读取excel并利用拉依达准则筛选数据的全过程

    在Excel中录入好数据以后经常需要被matlab读取,具体该如何读取并进行筛选呢?下面这篇文章就来给大家介绍了关于Matlab读取excel并利用拉依达准则筛选数据的相关资料,需要的朋友可以参考下
    2021-08-08
  • python中return不返回值的问题解析

    python中return不返回值的问题解析

    在本篇文章里小编给各位分享的是一篇关于python中return不返回值的问题解析,需要的朋友们可以学习下。
    2020-07-07
  • Python cookbook(数据结构与算法)实现查找两个字典相同点的方法

    Python cookbook(数据结构与算法)实现查找两个字典相同点的方法

    这篇文章主要介绍了Python实现查找两个字典相同点的方法,涉及Python常见集合运算操作技巧,需要的朋友可以参考下
    2018-02-02
  • 浅析Python 中整型对象存储的位置

    浅析Python 中整型对象存储的位置

    下面小编就为大家带来一篇浅析Python 中整型对象存储的位置。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧
    2016-05-05
  • Python多线程同步---文件读写控制方法

    Python多线程同步---文件读写控制方法

    今天小编就为大家分享一篇Python多线程同步---文件读写控制方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • Python使用turtle和matplotlib绘制圆和爱心的示例代码

    Python使用turtle和matplotlib绘制圆和爱心的示例代码

    这篇文章主要是带大家用Python的turtle和matplotlib画出圆满和爱心,文中的示例代码讲解的非常详细,对我们学习Python有一定帮助,感兴趣的可以了解一下
    2023-06-06
  • python函数enumerate,operator和Counter使用技巧实例小结

    python函数enumerate,operator和Counter使用技巧实例小结

    这篇文章主要介绍了python函数enumerate,operator和Counter使用技巧,结合实例形式总结分析了python内置函数enumerate,operator和Counter基本功能、原理、用法及操作注意事项,需要的朋友可以参考下
    2020-02-02
  • 5款实用的python 工具推荐

    5款实用的python 工具推荐

    工欲善其事必先利其器,一个好的工具能让起到事半功倍的效果,Python 社区提供了足够多的优秀工具来帮助开发者更方便的实现某些想法,下面这几个工具给我的工作也带来了很多便利,推荐给追求美好事物的你。
    2020-10-10

最新评论