简体   繁体   English

如何对Django模型进行单元测试?

[英]How to unittest a Django modelform?

I want to include tests for my Django application. 我想包括针对我的Django应用程序的测试。 After reading several postings about unittesting vs. integration testing (especially this SO posting ), I'm unsure about the following situation: 在阅读了有关单元测试与集成测试的几篇文章之后(尤其是本SO文章 ),我不确定以下情况:

Unit tests are the sole tests that tell you where exactly the bug is. 单元测试是唯一可以告诉您错误在哪里的测试。 To draw this information, they must run the method in a mocked environment, where all other dependencies are supposed to correctly work. 要绘制此信息,他们必须在模拟的环境中运行该方法,在该环境中所有其他依赖项都应正常工作。

While testing my forms (in fact ModelForm s), I rely on Django's feature to provide test-data by a fixture . 在测试表单(实际上是ModelForm )时,我依靠Django的功能通过 ModelForm 提供测试数据 Therefore, my form tests make use of normal Django methods like Foo.objects.get() . 因此,我的表单测试使用了常规的 Django方法,例如Foo.objects.get()

However, the above linked SO posting suggests to perform unittests without relying on external components. 但是,以上链接的SO发布建议执行单元测试而不依赖外部组件。 So should I drop the fixtures for unittests and use them only in integration tests? 那么我应该丢掉单元测试的固定装置,只在集成测试中使用它们吗?

Here's an example of my current testing setup for forms: 这是我当前对表单进行测试的示例:

from django.test import TestCase

class FooTest(TestCase):
    """Base class for all app related tests"""
    fixtures = ['testdata.json']

class BarFormTest(FooTest):
    """Tests for the BarForm"""

    def test_constructor(self):
        """Tests if the form stores the supplied user"""

        # this is the line I'm unsure of:
        u = User.objects.get(username='TestUser01')
        # is this better?
        # u = User(username='TestUser01')

        form = BarForm(data=None, user=u)

        self.assertEqual(u, form.user)

Don't get me wrong, my tests are working as expected. 不要误会我的意思,我的测试正在按预期进行。 I'm just unsure if I can rely on external (Django built-in) functions. 我只是不确定我是否可以依赖外部(Django内置)函数。 They are tested aswell, so any error in my tests will most likely originate in my code. 它们也经过测试,因此测试中的任何错误很可能都源于我的代码。

Should I spare the fixtures for integration tests? 我应该保留夹具用于集成测试吗? I hope this question is not too broad , I'm asking about Django best-practices here. 我希望这个问题不太广泛 ,我在这里询问Django最佳做法。

This depends on how certain you want to be, that you can trust your unittests. 这取决于您要确定自己是否可以相信单元测试。

If you don't trust django's internal functions you shouldn't use django, but instead use pure python in your entire project. 如果您不信任django的内部功能,则不应使用django,而应在整个项目中使用纯python。 If you don't trust python's internal functions you shouldn't use python, but C. If you don't trust C you should use assembler. 如果您不信任python的内部函数,则不应使用python,而应使用C。如果您不信任C,则应使用汇编器。 ...

There are unittests included in django itself that make sure every release works as expected. django本身包含一些单元测试,以确保每个版本都能按预期工作。 So you can be safe that the ORM works as expected. 因此,您可以放心,ORM可以按预期工作。

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

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