小编Nor*_*din的帖子

Pytest“run-around-tests”装置在一个类中的所有测试之前只运行一次

我正在使用 pytest + selenium 测试 Web 解决方案的用户消息功能。测试将向测试用户生成测试消息,然后登录该用户以验证该消息确实为该用户显示。

  • 我需要通过内部 API 生成这些消息。
  • 为了能够访问这个 API,我首先必须通过不同的 API 生成一个 AUTH 令牌。

所以测试场景基本上是:

  1. 在测试启动时,通过 API 辅助函数生成新的 AUTH 令牌。
  2. 向另一个 API 发送请求以设置新消息(需要 AUTH 令牌)
  3. 向另一个 API 发送请求以将此消息“映射”到指定用户(需要 AUTH 令牌)
  4. 登录测试用户并验证新消息是否确实显示。

我的问题是我想避免每次运行我的测试类中的每个测试时都创建一个新的 AUTH 令牌 - 一旦所有测试在同一个测试运行中使用,我想创建一个新令牌。

在调用所有测试时生成一个新访问令牌的最聪明的解决方案是什么?

现在我想出了这样的东西,每次运行任何单独的测试时都会生成一个新的令牌:

import pytest
import helpers.api_access_token_helper as token_helper
import helpers.user_message_generator_api_helper as message_helper
import helpers.login_helper as login_helper
import helpers.popup_helper as popup_helper

class TestStuff(object):

    @pytest.yield_fixture(autouse=True)
    def run_around_tests(self):
        yield token_helper.get_api_access_token()

    def test_one(self, run_around_tests):
        auth_token = run_around_tests
        message_helper.create_new_message(auth_token, some_message_data)
        message_helper.map_message_to_user(auth_token, user_one["user_id"])
        login_helper.log_in_user(user_one["user_name"], user_one["user_password"])
        assert popup_helper.user_message_is_displaying(some_message_data["title"])

    def …
Run Code Online (Sandbox Code Playgroud)

python selenium automated-tests pytest

5
推荐指数
1
解决办法
4121
查看次数

标签 统计

automated-tests ×1

pytest ×1

python ×1

selenium ×1