跳转到主要内容

捕获并断言transaction.on_commit()回调。

项目描述

https://img.shields.io/github/workflow/status/adamchainz/django-capture-on-commit-callbacks/CI/main?style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/django-capture-on-commit-callbacks.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

捕获并断言transaction.on_commit() 回调。这允许您使用TestCase编写测试,而无需使用较慢的TransactionTestCase实际提交事务。

此包是为Django PR #12944制作的初步版本,该版本是针对问题#30457 “on_commit应在TestCase中触发”的解决方案。该PR已合并到Django中,并已在3.2版本中发布,因此此包现在可以被认为是回滚包。

更多内容请参考我的博客文章快速测试Django transaction.on_commit()回调的方法

安装

使用 pip

python -m pip install django-capture-on-commit-callbacks

要求

支持Python 3.7到3.10。

支持Django 3.2到4.0。

注意:在Django 4.0+上不需要此包,使用此类版本将导致错误。


您的测试速度慢吗?请查看我的书籍加快Django测试速度,其中涵盖了大量编写更快、更准确测试的方法。


API

capture_on_commit_callbacks(*, using="default", execute=False)

作为上下文管理器,捕获给定数据库连接的on_commit回调函数。在退出上下文时,它返回一个包含捕获的回调函数的列表。您可以从该列表中对回调进行断言或调用它们以触发其副作用,模拟提交。

所有参数必须以关键字参数的形式传递。

using 是要捕获回调的数据库连接的别名。

execute 指定在退出上下文管理器且未抛出异常时,是否自动调用所有回调。

例如,您可以这样测试发送电子邮件的提交钩子

from django.core import mail
from django.test import TestCase
from django_capture_on_commit_callbacks import capture_on_commit_callbacks


class ContactTests(TestCase):
    def test_post(self):
        with capture_on_commit_callbacks() as callbacks:
            response = self.client.post(
                "/contact/",
                {"message": "I like your site"},
            )

        self.assertEqual(response.status_code, 200)

        self.assertEqual(len(callbacks), 1)
        # Execute the callback
        callbacks[0]()

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, "Contact Form")
        self.assertEqual(mail.outbox[0].body, "I like your site")

使用 execute=True 可以更简洁地编写相同的测试

from django.core import mail
from django.test import TestCase
from django_capture_on_commit_callbacks import capture_on_commit_callbacks


class ContactTests(TestCase):
    def test_post(self):
        with capture_on_commit_callbacks(execute=True) as callbacks:
            response = self.client.post(
                "/contact/",
                {"message": "I like your site"},
            )

        self.assertEqual(response.status_code, 200)

        self.assertEqual(len(callbacks), 1)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, "Contact Form")
        self.assertEqual(mail.outbox[0].body, "I like your site")

TestCaseMixin

一个混合类,可添加到您的自定义 TestCase 子类。它添加一个方法,captureOnCommitCallbacks(),该方法是 capture_on_commit_callbacks() 的别名,以匹配 unittest 断言的 camelCase 风格。

您可以这样向您的自定义 TestCase 类添加内容

from django import test
from django_capture_on_commit_callbacks import TestCaseMixin


class TestCase(TestCaseMixin, test.TestCase):
    pass

然后,您可以像这样使用您的自定义 TestCase 类重写上述测试

from django.core import mail
from example.test import TestCase


class ContactTests(TestCase):
    def test_post(self):
        with self.captureOnCommitCallbacks(execute=True) as callbacks:
            response = self.client.post(
                "/contact/",
                {"message": "I like your site"},
            )

        self.assertEqual(response.status_code, 200)

        self.assertEqual(len(callbacks), 1)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, "Contact Form")
        self.assertEqual(mail.outbox[0].body, "I like your site")

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

django-capture-on-commit-callbacks-1.11.0.tar.gz (6.7 kB 查看哈希值)

上传时间 源代码

构建分发

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面