Pytest 使用夹具到 return 值而不是函数位置
Pytest using a fixture to return value instead of function location
我想为 HighSchool
中的 tenth_standard
方法执行 pytest
class:
Class HighSchool():
...
def tenth_standard(self):
return f"I-am-studying-in-{self.school}-at-{self.country}"
我想使用@pytest.fixture
和@pytest.mark.parametrize
来执行pytest,我的代码如下:
@pytest.fixture(scope="function")
def expected(request):
return f"I-am-studying-in-{school}-at-{country}"
@pytest.fixture(scope="function")
def school(request):
return request.param
@pytest.fixture(scope="function")
def country(request):
return request.param
@pytest.mark.parametrize("school", ["abcd", "efgh"], indirect=True)
@pytest.mark.parametrize("country", ["India", "Japan"], indirect=True)
def test_tenthstandard(school, country, expected):
b = HighSchool(school=school, country=country)
assert expected == b.tenth_standard()
当我 运行 这个时,我得到一个 AssertionError
如下:
AssertionError: assert ('I-am-studying-in-<function school at 0x7f6b858a63a0>-at-<function country at '0x7f6b858a6280>) == 'I-am-studying-in-abcd-at-India'
我想将 expected fixture
的值固定为 return 而不是 function at XXX location
。有人可以帮我解决这个问题吗?
您的 expected
fixture 没有从其他 fixture 获取参数,而只是 fixture 函数,这当然不是您想要的。您可以从其他灯具“导出”expected
灯具,因此它将使用相同的参数自动参数化:
@pytest.fixture
def school(request):
return request.param
@pytest.fixture
def country(request):
return request.param
@pytest.fixture
def expected(school, country):
return f"I-am-studying-in-{school}-at-{country}"
@pytest.mark.parametrize("school", ["abcd", "efgh"])
@pytest.mark.parametrize("country", ["India", "Japan"])
def test_tenthstandard(school, country, expected):
b = HighSchool(school=school, country=country)
assert expected == b.tenth_standard()
请注意,在这种情况下,您甚至可以跳过 indirect=True
部分,因为 expected
夹具已经获得了正确的值。
附带说明:在测试中复制应用程序逻辑通常不是一个好主意,就像这里所做的那样。这样 bug 就可以很容易地传播到测试中而不会被发现。
(虽然在这种情况下可能只是由于一个愚蠢的例子)
我想为 HighSchool
中的 tenth_standard
方法执行 pytest
class:
Class HighSchool():
...
def tenth_standard(self):
return f"I-am-studying-in-{self.school}-at-{self.country}"
我想使用@pytest.fixture
和@pytest.mark.parametrize
来执行pytest,我的代码如下:
@pytest.fixture(scope="function")
def expected(request):
return f"I-am-studying-in-{school}-at-{country}"
@pytest.fixture(scope="function")
def school(request):
return request.param
@pytest.fixture(scope="function")
def country(request):
return request.param
@pytest.mark.parametrize("school", ["abcd", "efgh"], indirect=True)
@pytest.mark.parametrize("country", ["India", "Japan"], indirect=True)
def test_tenthstandard(school, country, expected):
b = HighSchool(school=school, country=country)
assert expected == b.tenth_standard()
当我 运行 这个时,我得到一个 AssertionError
如下:
AssertionError: assert ('I-am-studying-in-<function school at 0x7f6b858a63a0>-at-<function country at '0x7f6b858a6280>) == 'I-am-studying-in-abcd-at-India'
我想将 expected fixture
的值固定为 return 而不是 function at XXX location
。有人可以帮我解决这个问题吗?
您的 expected
fixture 没有从其他 fixture 获取参数,而只是 fixture 函数,这当然不是您想要的。您可以从其他灯具“导出”expected
灯具,因此它将使用相同的参数自动参数化:
@pytest.fixture
def school(request):
return request.param
@pytest.fixture
def country(request):
return request.param
@pytest.fixture
def expected(school, country):
return f"I-am-studying-in-{school}-at-{country}"
@pytest.mark.parametrize("school", ["abcd", "efgh"])
@pytest.mark.parametrize("country", ["India", "Japan"])
def test_tenthstandard(school, country, expected):
b = HighSchool(school=school, country=country)
assert expected == b.tenth_standard()
请注意,在这种情况下,您甚至可以跳过 indirect=True
部分,因为 expected
夹具已经获得了正确的值。
附带说明:在测试中复制应用程序逻辑通常不是一个好主意,就像这里所做的那样。这样 bug 就可以很容易地传播到测试中而不会被发现。
(虽然在这种情况下可能只是由于一个愚蠢的例子)