详解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传递参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

 

相关文章

  • Python实现自动登录百度空间的方法

    Python实现自动登录百度空间的方法

    这篇文章主要介绍了Python实现自动登录百度空间的方法,涉及Python的http请求发送、获取响应、cookie操作等相关技巧,需要的朋友可以参考下
    2017-06-06
  • python+POP3实现批量下载邮件附件

    python+POP3实现批量下载邮件附件

    这篇文章主要为大家详细介绍了python+POP3实现批量下载邮件附件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Python lambda 匿名函数优点和局限性深度总结

    Python lambda 匿名函数优点和局限性深度总结

    这篇文章主要为大家介绍了Python lambda 匿名函数的优点和局限性深度总结,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • python判断设备是否联网的方法

    python判断设备是否联网的方法

    这篇文章主要为大家详细介绍了python判断设备是否联网的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • python调用HEG工具批量处理MODIS数据的方法及注意事项

    python调用HEG工具批量处理MODIS数据的方法及注意事项

    这篇文章主要介绍了python调用HEG工具批量处理MODIS数据的方法,本文给大家提到了注意事项,通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • python3+telnetlib实现简单自动测试示例详解

    python3+telnetlib实现简单自动测试示例详解

    telnetlib 模块提供一个实现Telnet协议的类 Telnet,本文重点给大家介绍python3+telnetlib实现简单自动测试示例详解,需要的朋友可以参考下
    2021-08-08
  • Python生成器与迭代器详情

    Python生成器与迭代器详情

    这篇文章主要介绍了Python生成器与迭代器,现在可以通过生成器来直接创建一个列表,是由于内存的限制,表的容量肯定是有限的,果我们需要一个包含几百个元素的列表,是每次访问的时候只访问其中的几个,剩下的元素不使用就很浪费内存空间,下面来了解具体内容
    2021-11-11
  • Python实现统计mp4/avi视频的时长

    Python实现统计mp4/avi视频的时长

    moviepy是一个用于处理视频和音频的Python库,它提供了一组功能丰富的工具,所以本文将利用它实现统计mp4/avi视频的时长,希望对大家有所帮助
    2023-07-07
  • Python Mysql自动备份脚本

    Python Mysql自动备份脚本

    测试系统环境 Windows 2003 python 2.5.1 mysql 5.0.1 应该只适用于Win,因为调用了CMD。 增量备份,因为自用,数据库不大。
    2008-07-07
  • ITK 实现多张图像转成单个nii.gz或mha文件案例

    ITK 实现多张图像转成单个nii.gz或mha文件案例

    这篇文章主要介绍了ITK 实现多张图像转成单个nii.gz或mha文件案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07

最新评论