2017-07-14 24 views
1

我有一个库,它支持3.5和3.6并广泛使用asyncio。我想要有可以在3.5和3.6下工作的异步夹具,但这非常困难。到目前为止我发现的最好的方法是编写我自己的fixture装饰器来解决3.5和3.6中的差异。该库基本上从外部源获取数据驱动的协同程序链。我想测试一下生成的协程链。如何编写一个在Python 3.5和3.6中工作的异步pytest fixture?

我的夹具和测试这个样子的(和工作在3.5):

@pytest.mark.asyncio 
test_my_coroutine(coroutine): 
    coroutine = await coroutine 
    assert await coroutine() == 'expected result' 

@pytest.fixture 
async def coroutine(): 
    return await load_dynamic_coroutine() 

注意,我必须使用3.5在测试中await协程。但在python 3.6中,它在通过测试之前对协程进行了评估。因此,等待不再需要并产生错误。

回答

0

您可以从灯具中返回协程。

@pytest.fixture 
def coroutine(): 
    async def _inner(): 
     return await load_dynamic_coroutine() 
    return _inner 
+0

嗯......但是,这仍然意味着我需要在我的测试中“等待协程”,这不是我想要的。 – matthewatabet

相关问题