【发布时间】:2018-10-13 12:57:52
【问题描述】:
我正在使用 pytest + selenium 测试 Web 解决方案的用户消息功能。测试将为测试用户生成一条测试消息,然后登录该用户以验证该消息确实为该用户显示。
- 我需要通过内部 API 生成这些消息。
- 为了能够访问此 API,我首先必须通过不同的 API 生成一个 AUTH 令牌。
所以测试场景基本是:
- 在测试启动时,通过 API 帮助函数生成新的 AUTH 令牌。
- 向另一个 API 发送请求以设置新消息(需要 AUTH 令牌)
- 向另一个 API 发送请求以将此消息“映射”到指定用户(需要 AUTH 令牌)
- 登录测试用户并验证新消息确实正在显示。
我的问题是,我想避免每次运行我的测试类中的每个测试时都创建一个新的 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 test_two(self, run_around_tests):
auth_token = run_around_tests
message_helper.create_new_message(auth_token, some_other_message_data)
message_helper.map_message_to_user(auth_token, user_two["user_id"])
login_helper.log_in_user(user_two["user_name"], user_two["user_password"])
assert popup_helper.user_message_is_displaying(some_other_message_data["title"])
我已经用“run-around-tests”夹具来回进行了一些实验,但还没有找到解决方案。
【问题讨论】:
标签: python selenium automated-tests pytest