添加@pytest.mark.skip跳过Playwright测试时提示'pytest'未定义的问题
添加@pytest.mark.skip跳过Playwright测试时提示'pytest'未定义的问题
嗨,我来帮你搞定这个小问题~
你之所以会碰到'pytest' is not defined这个错误,核心原因很简单:你在代码里用了@pytest.mark.skip这个装饰器,但压根没导入pytest模块,Python找不到pytest这个名称,可不就报错了嘛!
解决方法超直接,只需要在你的导入代码块里加上一行导入语句就行:
import pytest
修改后的完整代码参考如下:
import re import pytest # 新增这行pytest导入 from playwright.sync_api import Page, expect def test_has_title(page: Page): page.goto("https://bbc.com") # Expect a title "to contain" a substring. expect(page).to_have_title(re.compile("BBC Home")) @pytest.mark.skip(reason="暂时跳过该测试") # 用reason参数标注跳过原因更规范 def test_get_started_link(page: Page): page.goto("https://bbc.com") # Click the get started link. page.get_by_role("link", name="Home").click() # Expects page to have a heading with the name of Installation. expect(page.get_by_role("heading", name="BBC")).to_be_visible()
顺便给你个小建议:@pytest.mark.skip最好带上reason参数,这样在生成测试报告的时候,能清楚看到这个测试被跳过的具体原因,后续维护起来也更方便~
备注:内容来源于stack exchange,提问作者Michael Durrant
相关产品推荐
相关产品推荐

