针对真正的MongoDB编写针对Python Eve RESTful API的测试 [英] Writing tests for Python Eve RESTful APIs against a real MongoDB

查看:119
本文介绍了针对真正的MongoDB编写针对Python Eve RESTful API的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python-eve开发我的API服务器,并且想知道如何测试API端点.我要特别测试的几件事:

I am developing my API server with Python-eve, and would like to know how to test the API endpoints. A few things that I would like to test specifically:

  • 验证POST/PATCH请求
  • 不同端点的身份验证
  • Before_和after_挂钩工作属性
  • 返回正确的JSON响应

目前,我正在针对真实的MongoDB测试应用程序,我可以想象一旦我要运行数百或数千个测试,测试将需要很长时间才能运行.模拟东西是另一种方法,但是我找不到能使我做到的同时又使测试尽可能切合实际的工具.我想知道是否有推荐的方法来测试eve应用程序.谢谢!

Currently I am testing the app against a real MongoDB, and I can imagine the testing will take a long time to run once I have hundreds or thousands of tests to run. Mocking up stuff is another approach but I couldn't find tools that allow me to do that while keeping the tests as realistic as possible. I am wondering if there is a recommended way to test eve apps. Thanks!

这就是我现在所拥有的:

Here is what I am having now:

from pymongo import MongoClient
from myModule import create_app
import unittest, json

class ClientAppsTests(unittest.TestCase):
  def setUp(self):
    app = create_app()
    app.config['TESTING'] = True
    self.app = app.test_client()

    # Insert some fake data
    client = MongoClient(app.config['MONGO_HOST'], app.config['MONGO_PORT'])
    self.db = client[app.config['MONGO_DBNAME']]
    new_app = {
      'client_id'     : 'test',
      'client_secret' : 'secret',
      'token'         : 'token'
    }
    self.db.client_apps.insert(new_app)

  def tearDown(self):
    self.db.client_apps.remove()

  def test_access_public_token(self):
    res = self.app.get('/token')
    assert res.status_code == 200

  def test_get_token(self):
    query = { 'client_id': 'test', 'client_secret': 'secret' }
    res = self.app.get('/token', query_string=query)
    res_obj = json.loads(res.get_data())
    assert res_obj['token'] == 'token'

推荐答案

Eve测试套件本身是夏娃·莫克扩展名.

The Eve test suite itself is using a test db and not mocking anything. The test db gets created and dropped on every run to guarantee isolation (not super fast yes, but as close as possible to a production environment). While of course you should test your own code, you probably don't need to write tests like test_access_public_token above since, stuff like that is covered by the Eve suite already. You might want to check the Eve Mocker extension too.

还要使自己熟悉身份验证和授权教程.看起来您获得整个令牌的方式并不是很合适(您想对这类东西使用请求标头).

Also make yourself familiar with Authentication and Authorization tutorials. It looks like the way you're going get the whole token thing going is not really appropriate (you want to use request headers for that kind of stuff).

这篇关于针对真正的MongoDB编写针对Python Eve RESTful API的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆