优化 Flask 客户端测试

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

在编写 Python 集成测试时,将诸如数据库访问之类的慢速代码放在 setUpClass 方法中很有用,这样它们只对整个 unittest.TestCase 执行一次。最近,在为 Flask API 编写集成测试时,我想为 TestCase 进行一次 API 调用,但有许多测试断言 JSON 响应的各个部分。这有点尴尬,因为 Flask 测试客户端 仅使用 TestCase 的实例进行实例化。

我最终在自定义基 TestCase 子类的全局变量中缓存了 API 响应。


 from functools import partial
from unittest import TestCase

_cached_api_responses = {}

class MyTestCase(TestCase):

def set_cached_json_data(self, cache_key, test_callable):
    """ We want to separate out tests for various keys in the json response 
        of an API call, but we only want to make an API once for performance
        reasons. Solution is to cache this between test calls, which is made
        more difficult due to test classes being re-instantiated between
        individual tests. Cache in a global. """
    global _cached_api_responses
    response_json = _cached_api_responses.get(cache_key)
    if not response_json:
        response = test_callable()
        response_json = response.json
        _cached_api_responses[cache_key] = response_json
    return response_json

class SpecificTestCase(MyTestCase):

@classmethod
def setUpClass(cls):
    # do a bunch of database record creation
    cls.db_object = ...

def setUp(self):
    # cache a flask API response 
    test_callable = partial(self.get, '/my-url')
    self.response_json = self.set_cached_json_data('my-url', test_callable)

def test_foo(self):
    foo = self.response_json['foo']
    self.assertEquals(len(foo), 1)

def test_bar(self):
    bar = self.response_json['foo']['bar']
    self.assertEquals(len(bar), 10)

使用 partial 只是为了更容易将任何测试客户端调用传递到缓存函数中。

它缓存 JSON 而不是 SQLAlchemy 数据库对象的集合这一事实很重要,如果您尝试这样做,您会发现 SQLAlchemy 会抛出有关对象不再与测试中的会话绑定的异常。

我目前在 NerdWallet 工作,这是一家位于旧金山的初创公司,致力于让生活中的所有财务决策变得清晰。我们正在 疯狂招聘 。在 Twitter 上联系我,我很想谈谈。