在 pytest 中使用 record_testsuite_property fixture 时如何访问测试套件属性?
How to access test suite properties when using record_testsuite_property fixture in pytest?
根据 Pytest 文档,我们可以使用 record_testsuite_property
fixture 来记录特定于测试套件的属性。
所以我是这样使用那个固定装置的:
import pytest
class TestSuite:
@pytest.fixture(scope="class")
def init(self, record_testsuite_property):
record_testsuite_property("suite_name", "Test Suite #1")
def test_example(self, record_property):
record_property('test_id', 'ABC-123')
record_property('test_name', 'Example Test #1')
assert True
我想在生成报告时访问 suite_name
的值,如下所示:
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if item.user_properties:
test_properties = { prop[0]: prop[1] for prop in item.user_properties }
# These are added via `record_property` fixture and I am able to access them with no issue.
report.test_id = test_properties["test_id"]
report.test_name = test_properties["test_name"]
# Not able to get the suite_name from here.
# report.suite_name = test_properties["suite_name"]
setattr(report, "duration_formatter", "%M:%S")
我想通了。
整个想法是,我想让 suite_name
成为附加到每个项目的 属性,以便我可以将其包含在报告中。
所以我意识到我仍然会在这里使用 record_property
固定装置并在 function
范围内自动请求它(使用 autouse=True
)。
import pytest
class TestSuite:
@pytest.fixture(scope="function", autouse=True)
def init(self, record_property):
record_property("suite_name", "Test Suite #1")
def test_example(self, record_property):
record_property('test_id', 'ABC-123')
record_property('test_name', 'Example Test #1')
assert True
现在我可以在这里访问 suite_name
:
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if item.user_properties:
test_properties = { prop[0]: prop[1] for prop in item.user_properties }
report.test_id = test_properties["test_id"]
report.test_name = test_properties["test_name"]
report.suite_name = test_properties["suite_name"]
setattr(report, "duration_formatter", "%M:%S")
根据 Pytest 文档,我们可以使用 record_testsuite_property
fixture 来记录特定于测试套件的属性。
所以我是这样使用那个固定装置的:
import pytest
class TestSuite:
@pytest.fixture(scope="class")
def init(self, record_testsuite_property):
record_testsuite_property("suite_name", "Test Suite #1")
def test_example(self, record_property):
record_property('test_id', 'ABC-123')
record_property('test_name', 'Example Test #1')
assert True
我想在生成报告时访问 suite_name
的值,如下所示:
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if item.user_properties:
test_properties = { prop[0]: prop[1] for prop in item.user_properties }
# These are added via `record_property` fixture and I am able to access them with no issue.
report.test_id = test_properties["test_id"]
report.test_name = test_properties["test_name"]
# Not able to get the suite_name from here.
# report.suite_name = test_properties["suite_name"]
setattr(report, "duration_formatter", "%M:%S")
我想通了。
整个想法是,我想让 suite_name
成为附加到每个项目的 属性,以便我可以将其包含在报告中。
所以我意识到我仍然会在这里使用 record_property
固定装置并在 function
范围内自动请求它(使用 autouse=True
)。
import pytest
class TestSuite:
@pytest.fixture(scope="function", autouse=True)
def init(self, record_property):
record_property("suite_name", "Test Suite #1")
def test_example(self, record_property):
record_property('test_id', 'ABC-123')
record_property('test_name', 'Example Test #1')
assert True
现在我可以在这里访问 suite_name
:
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if item.user_properties:
test_properties = { prop[0]: prop[1] for prop in item.user_properties }
report.test_id = test_properties["test_id"]
report.test_name = test_properties["test_name"]
report.suite_name = test_properties["suite_name"]
setattr(report, "duration_formatter", "%M:%S")