pytest 夹具产量返回生成器而不是对象

Pro*_*sor 5 pytest python-3.x falconframework

我正在运行 pytest-3。我正在定义一个应该返回 falcon TestClient 对象的装置。我还需要拆解,所以我正在努力放弃它。

def client():
    api=create_app()
    c = testing.TestClient(api)
    yield c
    remove_db()
Run Code Online (Sandbox Code Playgroud)

如果我“返回”而不是“yield”,则测试用例运行得很好。但是通过收益,我的测试用例得到一个生成器对象而不是 TestClient 对象

Ami*_*mit 4

可能是因为该功能未标记为固定装置。用 装饰该函数后尝试@pytest.fixture。例如,

@pytest.fixture(scope="session")
def client():
    api=create_app()
    c = testing.TestClient(api)
    yield c
    remove_db()
Run Code Online (Sandbox Code Playgroud)