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中,它在通過測試之前對協程進行了評估。因此,等待不再需要併產生錯誤。
嗯......但是,這仍然意味着我需要在我的測試中「等待協程」,這不是我想要的。 – matthewatabet