简体   繁体   English

用pytest测试aiohttp和mongo

[英]Testing aiohttp & mongo with pytest

I have a simple coroutine register that accepts login and password as post arguments, then it goes into the database and so on. 我有一个简单的协程register ,它接受登录名和密码作为发布参数,然后进入数据库,依此类推。 The problem I have is that I do not know how to test the coroutine. 我的问题是我不知道如何测试协程。

I followed examples from https://aiohttp.readthedocs.io/en/latest/testing.html . 我遵循了https://aiohttp.readthedocs.io/en/latest/testing.html中的示例。

And everything seemed easy until I started writing tests myself. 在我开始自己编写测试之前,一切似乎都很容易。

Code for test_register.py test_register.py代码

from main import make_app
pytest_plugins = 'aiohttp.pytest_plugin'


@pytest.fixture
def cli(loop, test_client):
    return loop.run_until_complete(test_client(make_app))

async def test_register(cli):
    resp = await cli.post('/register', data={'login': 'emil', 'password': 'qwerty'})
    assert resp.status == 200
    text = await resp.text()    

And register.py register.py

from settings import db

async def register(request):
    post_data = await request.post()
    print('Gotta: ', post_data)
    login, password = post_data['login'], post_data['password']
    matches = await db.users.find({'login': login}).count()
    ...

main.py

from aiohttp import web
from routes import routes


def make_app(loop=None):
    app = web.Application(loop=loop)
    for route in routes:
        app.router.add_route(route.method, route.url, route.handler)
    return app


def main():
    web.run_app(make_app())


if __name__ == "__main__":
    main()

settings.py

from motor.motor_asyncio import AsyncIOMotorClient
DBNAME = 'testdb'
db = AsyncIOMotorClient()[DBNAME]

And then I ran py.test test_register.py and it got stuck on database operation matches = await db.users.find({'login': login}).count() 然后我运行py.test test_register.py ,它卡在数据库操作matches = await db.users.find({'login': login}).count()

The root of your problem is global variable usage . 问题的根源是全局变量用法

I suggest the following changes: 我建议进行以下更改:

from aiohttp import web
from motor.motor_asyncio import AsyncIOMotorClient
from routes import routes

def make_app(loop=None):
    app = web.Application(loop=loop)
    DBNAME = 'testdb'
    mongo = AsyncIOMotorClient(io_loop=loop)
    db = mongo[DBNAME]
    app['db'] = db

    async def cleanup(app):
        mongo.close()

    app.on_cleanup.append(cleanup)

    for route in routes:
        app.router.add_route(route.method, route.url, route.handler)
    return app

register.py register.py

async def register(request):
    post_data = await request.post()
    print('Gotta: ', post_data)
    login, password = post_data['login'], post_data['password']
    matches = await request.app['db'].users.find(
        {'login': login}).count()
    ...

Pushing common-used objects into application's storage is an appreciated way for handling database connections etc. 将常用对象推入应用程序的存储是处理数据库连接等的一种受赞赏的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM