我正在使用 pytest + selenium 测试 Web 解决方案的用户消息功能。测试将向测试用户生成测试消息,然后登录该用户以验证该消息确实为该用户显示。
所以测试场景基本上是:
我的问题是我想避免每次运行我的测试类中的每个测试时都创建一个新的 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)