Pytest学习-如何在用例代码中调用fixtrue时传入参数

在这里插入图片描述

前言

在使用Pytest的时候,为了提高代码的复用性,我们一般会把一些常用操作,比如把登录方法写在 conftest.py 中,然后在不同测试用例中,调用这个登录方法。

但是在测试用例中,我们可能需要传入不同的账号密码·,然后通过调用fixtrue中的登录函数对该账户进行登录,这里就涉及到调用 fixture 时参数传递的问题了,今天我们就来学习下。

使用 pytest.mark.parametrize() 对函数传参

在Pytest中,我们一般使用其装饰器 @pytest.mark.parametrize() 来实现参数化。

下面是一个简单的示例:

import pytest


data = [
    ["user1", "password1"],
    ["wintest", "wintest"],
    ["user2", "password2"]
]


def login_user(username, password):
    print("登录用户:{}".format(username))
    print("登录密码:{}".format(password))
    return True if (username == "wintest" and password == "wintest") else False


@pytest.mark.parametrize("username, password", data)
def test_login_user(username, password):
    res = login_user(username, password)
    assert res

我们在 conftest.py 中把用例开始和结束给打印出来,以便看起来更直观。

@pytest.fixture(autouse=True)
def start_end():
    print("------------------------Start---------------------------")
    yield
    print("------------------------End---------------------------")

把以上用例运行后,得到结果如下:

------------------------Start---------------------------
登录用户:user1
登录密码:password1
F------------------------End---------------------------
------------------------Start---------------------------
登录用户:wintest
登录密码:wintest
.------------------------End---------------------------
------------------------Start---------------------------
登录用户:user2
登录密码:password2
F------------------------End---------------------------

============================ FAILURES =============================
________________ test_login_user[user1-password1] _________________

username = 'user1', password = 'password1'

    @pytest.mark.parametrize("username, password", data)
    def test_login_user(username, password):
        res = login_user(username, password)
>       assert res
E       assert False

test_04.py:20: AssertionError
________________ test_login_user[user2-password2] _________________

username = 'user2', password = 'password2'

    @pytest.mark.parametrize("username, password", data)
    def test_login_user(username, password):
        res = login_user(username, password)
>       assert res
E       assert False

test_04.py:20: AssertionError
2 failed, 1 passed in 0.13 seconds

request接收参数

我们通过查看fixture的源码,可以知道 fixture 下可以有以下5个参数,参数默认值如下:

def fixture(scope="function", params=None, autouse=False, ids=None, name=None):

在这些参数中,有一个参数 params ,它表示当前参数列表,而我们想要拿到当前的参数,可以通过 request.param 来获取。

    :arg params: an optional list of parameters which will cause multiple
                invocations of the fixture function and all of the tests
                using it.
                The current parameter is available in ``request.param``.

我们把上面函数传参的用例改一下,然后对 request.param 进行简单的示例:

import pytest


def test_login_user(login_user):
    res = login_user
    assert res

修改 conftest.py 如下:

import pytest


data = [
    ["user1", "password1"],
    ["wintest", "wintest"],
    ["user2", "password2"]
]


@pytest.fixture(scope="function", params=data, autouse=False)
def login_user(request):
    username = request.param[0]
    password = request.param[1]
    print("登录用户:{}".format(username))
    print("登录密码:{}".format(password))
    return True if (username == "wintest" and password == "wintest") else False


@pytest.fixture(scope="function", autouse=True)
def start_end():
    print("------------------------Start---------------------------")
    yield
    print("------------------------End---------------------------")

把以上用例运行后,得到结果如下:

------------------------Start---------------------------
登录用户:user1
登录密码:password1
F------------------------End---------------------------
------------------------Start---------------------------
登录用户:wintest
登录密码:wintest
.------------------------End---------------------------
------------------------Start---------------------------
登录用户:user2
登录密码:password2
F------------------------End---------------------------

============================ FAILURES =============================
__________________ test_login_user[login_user0] ___________________

login_user = False

    def test_login_user(login_user):
        res = login_user
>       assert res
E       assert False

test_04.py:6: AssertionError
__________________ test_login_user[login_user2] ___________________

login_user = False

    def test_login_user(login_user):
        res = login_user
>       assert res
E       assert False

test_04.py:6: AssertionError
2 failed, 1 passed in 0.17 seconds

调用fixtrue时结合request传参

在上面的代码中,我们是把含有用户账号密码的data数据直接放在 conftest.py 中,如果我需要在测试用例中调用 fixtrue 时才传入账号密码,那应该要怎么做呢?

可以在测试用例中通过 @pytest.mark.parametrize() ,把调用的fixtrue当作一个函数来进行参数传递,最后再使用request接收参数。

我们继续对上面的代码进行修改,先修改测试用例部分代码:

import pytest


data = [
    ["user1", "password1"],
    ["wintest", "wintest"],
    ["user2", "password2"]
]


# indirect=True的作用是为了把 login_user 当作一个fixture函数进行调用和传递
@pytest.mark.parametrize("login_user", data, indirect=True)
def test_login_user(login_user):
    res = login_user
    assert res

修改 conftest.py 如下:

import pytest


@pytest.fixture(scope="function", autouse=False)
def login_user(request):
    username = request.param[0]
    password = request.param[1]
    print("登录用户:{}".format(username))
    print("登录密码:{}".format(password))
    return True if (username == "wintest" and password == "wintest") else False


@pytest.fixture(scope="function", autouse=True)
def start_end():
    print("------------------------Start---------------------------")
    yield
    print("------------------------End---------------------------")

把以上用例运行后,得到结果如下:

------------------------Start---------------------------
登录用户:user1
登录密码:password1
F------------------------End---------------------------
------------------------Start---------------------------
登录用户:wintest
登录密码:wintest
.------------------------End---------------------------
------------------------Start---------------------------
登录用户:user2
登录密码:password2
F------------------------End---------------------------

============================ FAILURES =============================
__________________ test_login_user[login_user0] ___________________

login_user = False

    @pytest.mark.parametrize("login_user", data, indirect=True)
    def test_login_user(login_user):
        res = login_user
>       assert res
E       assert False

test_04.py:15: AssertionError
__________________ test_login_user[login_user2] ___________________

login_user = False

    @pytest.mark.parametrize("login_user", data, indirect=True)
    def test_login_user(login_user):
        res = login_user
>       assert res
E       assert False

test_04.py:15: AssertionError
2 failed, 1 passed in 0.10 seconds

最后: 可以关注公众号:伤心的辣条 ! 进去有许多资料共享!资料都是面试时面试官必问的知识点,也包括了很多测试行业常见知识,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。

如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!


好文推荐

转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!

面试经:一线城市搬砖!又面软件测试岗,5000就知足了…

面试官:工作三年,还来面初级测试?恐怕你的软件测试工程师的头衔要加双引号…

什么样的人适合从事软件测试工作?

那个准点下班的人,比我先升职了…

测试岗反复跳槽,跳着跳着就跳没了…

### 在 pytest-html 报告中增加测试的作者字段 为了在 `pytest-html` 生成的 HTML 测试报告中添加自定义信息,比如测试的作者字段,可以利用 `pytest_html_results_table_header` 和 `pytest_html_results_table_row` 这两个钩子函数。通过这两个钩子函数可以在表格头部以及每一行的结果记录里加额外列。 #### 定义自定义属性并修改报告头 当编写测试,在每个测试函数上附加元数据作为参数传递给装饰器 `@pytest.mark.parametrize()` 或者直接使用 `request.node.add_marker()` 方法动态添加标记。对于静态声明的情况如下所示: ```python import pytest @pytest.mark.author("张三") def test_sample(): assert True ``` 接着需要配置环境以便这些信息能够显示在最终生成的HTML文档内。这通常是在项目的根目录下的 `conftest.py` 文件完成操作。以下是具体实现方式: ```python # conftest.py def pytest_html_results_table_header(cells): cells.insert(2, "<th>Author</th>") # 向表头插新列 'Author' def pytest_html_results_table_row(report, cells): try: author = report.user_properties.get('author', '') cells.insert(2, f"<td>{author}</td>") except AttributeError: pass @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() marker = item.get_closest_marker(name="author") if marker is not None: report.user_properties['author'] = marker.args[0] ``` 上述代码片段实现了向默认结果表单中新增一列表示负责人的名字,并且从测试项获取到对应的标注信息填充进去[^1]。 这样做的好处在于不仅限于展示开发者姓名,还可以扩展至其他任何有意义的数据点,只要遵循相同模式即可轻松集成更多维度的信息进可视化报表之中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值