Python 通过self而不是作为方法参数的类的Pytest fixture
我通常会编写一个测试类,在每个方法中使用pytest夹具。这里有一个例子。我希望能够避免在每个方法的签名中写入fixture名称。它不干。如何做到这一点 我希望能够通过将fixture作为测试类的属性来访问fixture。在本例中,我希望将GoogleFixture视为TestGoogle的一个属性。这可能吗Python 通过self而不是作为方法参数的类的Pytest fixture,python,unit-testing,pytest,Python,Unit Testing,Pytest,我通常会编写一个测试类,在每个方法中使用pytest夹具。这里有一个例子。我希望能够避免在每个方法的签名中写入fixture名称。它不干。如何做到这一点 我希望能够通过将fixture作为测试类的属性来访问fixture。在本例中,我希望将GoogleFixture视为TestGoogle的一个属性。这可能吗 from bs4 import BeautifulSoup import pytest import requests @pytest.fixture() def google():
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"
当然,只要使用自动使用夹具即可。在您的示例中,更改将引入一个额外的装置(我将其命名为
\u request\u google\u page
):
您甚至可以完全放下google
夹具,将代码移动到\u request\u google\u页面
:
from bs4 import BeautifulSoup
import pytest
import requests
@pytest.fixture()
def google():
return requests.get("https://www.google.com")
class TestGoogle:
@pytest.fixture(autouse=True)
def _request_google_page(self, google):
self._response = google
def test_alive(self):
assert self._response.status_code == 200
def test_html_title(self):
soup = BeautifulSoup(self._response.content, "html.parser")
assert soup.title.text.upper() == "GOOGLE"
@pytest.fixture(autouse=True)
def _request_google_page(self):
self._response = requests.get("https://www.google.com")
请注意,\u request\u google\u page
默认情况下每个测试将调用一次,因此每个测试都将得到一个新的响应。如果您希望在TestGoogle
类中初始化一次响应并在所有测试中重复使用,请调整夹具范围(scope='class'
用于\u请求\u google\u页面
和scope='module'
或scope='session'
用于google
)。例如:
from bs4 import BeautifulSoup
import pytest
import requests
@pytest.fixture(scope='module')
def google():
return requests.get("https://www.google.com")
@pytest.fixture(autouse=True, scope='class')
def _request_google_page(request, google):
request.cls._response = google
class TestGoogle:
def test_alive(self):
assert self._response.status_code == 200
def test_html_title(self):
soup = BeautifulSoup(self._response.content, "html.parser")
assert soup.title.text.upper() == "GOOGLE"
我必须解决一个类似的问题,而接受的解决方案在类范围的fixture中并不适用 我想为每个测试类调用一次fixture,并使用
self
重新使用测试方法中的值。这实际上也是OP计划要做的
您可以使用request
fixture来访问正在使用它的类(request.cls
),并在类属性中指定fixture值。然后可以从self
访问此属性。以下是完整的片段:
from bs4 import BeautifulSoup
import pytest
import requests
@pytest.fixture(scope="class")
def google(request):
request.cls.google = requests.get("https://www.google.com")
@pytest.mark.usefixtures("google")
class TestGoogle:
def test_alive(self):
assert self.google.status_code == 200
def test_html_title(self):
soup = BeautifulSoup(self.google.content, "html.parser")
assert soup.title.text.upper() == "GOOGLE"
希望这能帮助其他人回答这个问题。谢谢@hoefling这正是我想要的。我从未理解自动使用夹具的功能。我现在明白了。干杯。很高兴我能帮助你@hoefling出于某种原因这对我不起作用。测试方法似乎正在获得一个新实例,
self
在设置方法中没有属性。仅出于上下文考虑,我在类中使用了2pytext.mark…
。@Ken4scholars最好是问一个单独的问题,包括一个失败的测试设置。你能做到吗?@PrashantPathak这是测试收集和准备的一部分<当在测试集合上评估\u request\u google\u页面
夹具时,code>request.cls将是TestGoogle
,因此在收集TestGoogle
后,TestGoogle.\u response
将被初始化;然后,执行TestGoogle().test\u alive()
,并提供self.\u响应。实际上,这是一个类变量。但是,没有办法通过它设置self.variable
的值?@MohitC我认为没有,我想不出什么情况下需要它,而且cls
不起作用。我实际上想要的正是这种行为。在测试中设置self.variable的值,并使用该结果进行以下操作tests@JorritSmit在一个测试中设置一个值以用于其他测试听起来很奇怪。测试不应为其他测试生成值。