admin管理员组文章数量:1334887
I have the following python code:
main.py:
import pytest
import re
from playwright.sync_api import Page, expect
from time import sleep
TIMEOUT = 5000 # 5 seconds
# Helper function to send a message and assert response
def send_msg_and_assert(page: Page, message: str, regex: re.Pattern):
page.type('[placeholder="Message..."]', message)
page.keyboard.press("Enter")
sleep(DEFAULT_WAIT_TIME / 1000) # sleep time converted to seconds
assert page.get_by_text(regex)
# Go to the chat page
def go_to_chats(page: Page):
page.goto('/')
page.wait_for_timeout(TIMEOUT)
assert page.get_by_text("Chats").is_visible()
@pytest.mark.usefixtures("login_logout")
@pytest.mark.basic_interaction
@pytest.mark.parametrize("message, expected_regex", [
("How can you help me?", repile("/language|help/", re.IGNORECASE)),
("What kind of questions?", repile("/Language|Native|interview/", re.IGNORECASE)),
("What does my name mean?", repile("/sorry|determine|please tell/", re.IGNORECASE))
])
# This basic scenario is using playwright web application testing via the cohere web-page
def test_basic_interaction(go_to_chats, message: str, expected_regex: re.Pattern):
# Step 1: Login and go to chats page
page = go_to_chats
# Step 2: Send message and assert response
send_msg_and_assert(page, message, expected_regex)
# Step 3: Logout
Here you can find the conftest.py with the fixtures:
import pytest
import os
import re
from playwright.sync_api import sync_playwright, Page
TIMEOUT = 5000 # 5 seconds
DEFAULT_WAIT_TIME = 30000 # 30 seconds for responses
# Constants and credentials
COHERE_API_KEY = "your-api-key"
USER_CREDENTIALS = {
'email': '[email protected]',
'password': "blabla",
'user_name': 'goofy'
}
# Fixture for login, reusable across tests
@pytest.fixture(scope="session")
def login_logout():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False) # Set headless=True for no UI
page = browser.new_page()
# Performs login on the Cohere dashboard.
page.goto('')
page.locator('input[name="email"]').fill(USER_CREDENTIALS['email'])
page.locator('input[name="password"]').fill(USER_CREDENTIALS['password'])
page.get_by_role("button", name="Log in").click()
page.wait_for_timeout(TIMEOUT) # Wait for login to complete
assert page.get_by_text(f"Welcome, {USER_CREDENTIALS['user_name']}!").is_visible()
# Yield control to test
yield page
# Fixture for logout, reusable across tests
# Performs logout after all tests are completed.
page.goto('')
page.wait_for_timeout(TIMEOUT) # Wait for logout to complete
assert page.get_by_role("heading", name="Log in").is_visible()
browser.close()
The login_logout
function in the conftest.py
will be executed at the beginning of the session and at the end of the session to achieve the use of one session for test cases.
Now I was trying to move the go_to_chats()
(or any other function) also to the fixtures but not as part of the login_logout()
function.
How can I achieve that?
本文标签: How to move python function to the conftest as fixtureStack Overflow
版权声明:本文标题:How to move python function to the conftest as fixture? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325435a2453628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论