2017-08-29 13 views
6

次の非同期メソッドのためにpytestを書き込もうとしていますが、私はどこにもいません。非同期と待機メソッドのPython pytestケース

class UserDb(object): 
    async def add_user_info(self,userInfo): 
     return await self.post_route(route='users',json=userInfo) 

    async def post_route(self,route=None,json=None,params=None): 
     uri = self.uri + route if route else self.uri  
     async with self.client.post(uri,json=json,params=params) as resp:    
     assert resp.status == 200 
     return await resp.json() 

誰かがこれを手伝ってくれますか? TIA

+0

あなたはaiohttpを使用していますか? – Juggernaut

+0

@Juggernaut:はい、私はaiohttpを使用しています。 –

答えて

6

pip install pytest-aiohttpは、この

from pytest import fixture 

def make_app(): 
    app = Application() 
    # Config your app here 
    return app 

@fixture 
def test_fixture(loop, test_client): 
    """Test fixture to be used in test cases""" 
    app = make_app() 
    return loop.run_until_complete(test_client(app)) 

のようなフィクスチャを作成今

f = test_fixture 

async def test_add_user_info(f): 
    resp = await f.get('/') 
    assert resp.status == 200 
    assert await resp.json() == {'some_key': 'some_value'} 

はまた、私はあなたのadd_user_infoコルーチンは何も返していない気づいたあなたのテストを書きます。 詳細はhere

+0

私はpytestの初心者です。アプリの設定とフィクスチャのテストに関する詳細を教えてください。 –

+1

の設定はオプションです。セッション管理やdb構成管理などの中間製品の追加で構成されています。単にテストケースを作成し、テストケースの引数として 'test_fixture'を使用してください。私が答えに入れたもののように。あなたの端末で 'py.test test_module.py'を実行してください。 – Juggernaut

+1

はそれを得ました。どうもありがとうございます :) –

関連する問題