在pytest中,我试图找出是否有可能将对象或变量从测试功能传递到“功能范围”灯具(传递至灯具设置或拆卸)
答案 0 :(得分:0)
您可以按以下方式与灯具进行交互,但不在设置或拆卸中:
代码
import pytest
class FixtureStack():
def __init__(self):
self.messages = []
def __str__(self):
return f'messages={self.messages}'
def push(self, msg):
self.messages.append(msg)
@pytest.fixture(scope='function')
def stack():
yield FixtureStack()
def test_1(stack):
print(stack)
stack.push('msg_1')
print(stack)
assert stack.messages == ['msg_1']
def test_2(stack):
print(stack)
stack.push('msg_2')
print(stack)
assert stack.messages == ['msg_2']
Pytest执行:
$ pytest -v driver.py -s
=============================================== test session starts ================================================
platform linux -- Python 3.7.1, pytest-5.0.1, py-1.7.0, pluggy-0.12.0 -- /home/backend/venvs/py3.7.1/bin/python3.7
cachedir: .pytest_cache
rootdir: /home/backend/backend, inifile: pytest.ini
plugins: mock-1.10.4
collected 2 items
driver.py::test_1 messages=[]
messages=['msg_1']
PASSED
driver.py::test_2 messages=[]
messages=['msg_2']
PASSED
============================================= 2 passed in 0.01 seconds =============================================