我有一个长期测试,持续2天,我不想包括在通常的测试运行中.我也不想输入命令行参数,这会在每次通常的测试运行中取消选择它和其他测试.当我真正需要时,我宁愿选择默认取消选择的测试.我尝试将测试重命名test_longrun为longrun并使用该命令
py.test mytests.py::longrun
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
通常我会编写一个在每种方法中都使用pytest fixture的测试类.这是一个例子.我希望能够避免在每个方法的签名中编写夹具名称.这不是干的.如何才能做到这一点?
我希望能够通过将fixture作为测试类的属性来访问fixture.在这个例子中,我想将google fixture视为TestGoogle的一个属性.这可能吗?
from bs4 import BeautifulSoup
import pytest
import requests
@pytest.fixture()
def google():
return requests.get("https://www.google.com")
class TestGoogle:
def test_alive(self, google):
assert google.status_code == 200
def test_html_title(self, google):
soup = BeautifulSoup(google.content, "html.parser")
assert soup.title.text.upper() == "GOOGLE"
Run Code Online (Sandbox Code Playgroud) 在运行 python 脚本时,我们可以使用sys.argv获取命令行参数并在我们的 python 代码中的任何地方使用它。
在运行 pytest 时,我们可以使用pytest_addoption添加命令行参数,但在我看来,我们只能在测试或固定装置中使用它们,我们可以通过任何公开config对象的方式访问选项。
但是,就我而言,我希望能够从测试模块主体本身访问命令行选项。
是否可以在不需要任何夹具的情况下以某种方式访问 pytest 的配置?