为django模型编写测试用例 [英] Writing test cases for django models

查看:176
本文介绍了为django模型编写测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过我目前的项目,在经历了无数分钟的调试苦难之后,我决定采用TDD。要开始,我计划为每个现有的模型编写一套单元测试。但是对于只有属性定义的模型(即没有其他方法/属性),我不知道我需要测试什么,也不知道如何。

  class Product(models.Model):
name = models.CharField(max_length = 50)
description = models.TextField(default ='',blank = True)
retails = models.ManyToManyField(零售,verbose_name ='携带产品的零售店)
制造商= models.ForeignKey(制造商, related_name ='products')
date_created = models.DateTimeField(auto_now_add = True)
date_modified = models.DateTimeField(auto_now = True)

使用 Product 作为示例,单元测试应包括哪些内容?





一篇我发现有用的文章: http://toastdriven.com/blog/2011 / APR / 10 /导向到测试功能于django的/ 。以下是要测试的一个很好的总结:


开发人员/设计师新进行测试的另一个常见的挫折是
的问题我应该(或不应该)测试?虽然没有
hard&这里的快速规则整齐地适用于所有地方,有一些
一般准则,我可以提供决定:




  • 如果相关代码是内置的Python函数/库,请不要测试。例如datetime库。


  • 如果有问题的代码内置到Django中,请不要测试。例如模型上的字段或测试内置
    template.Node如何包含的标签。


  • 如果您的模型具有自定义方法,您应该通过单元测试进行测试。


  • 同样适用于自定义视图,表单,模板标签,上下文处理器,中间件,管理命令等。您实现了
    业务逻辑,您应该测试代码的方面。



所以,对于你的例子,在写一些自定义函数之前,真的不会有任何东西要测试。

在我看来,测试 ForeignKey ManyToManyField 链接将属于第二类(Django内置的代码),所以我不会测试这些,因为你真的测试Django是否正在运行正常。如果您有创建产品实例(包括外部关系和M2M)的方法,则可以验证已创建的数据,这将测试您的自定义方法,而不是Django功能。



使用TDD范例,测试用于验证业务逻辑和设计要求。


Half way through my current project, after suffering the pain of spending uncountable minutes on debugging, I have decided to adopt TDD. To start, I am planning to write a set of unit tests for each existing models. But for models that only have attributes defined (ie. no additional methods/properties) I am not sure what I need to test nor how.

class Product(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField(default='', blank=True)
    retails = models.ManyToManyField(Retail, verbose_name='Retail stores that carry the product')
    manufacturer = models.ForeignKey(Manufacturer, related_name='products')
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

Using Product as an example, what are the things about it that unit tests should cover? And how should ForeignKey and ManyToManyField be covered?

解决方案

This was an article I found helpful: http://toastdriven.com/blog/2011/apr/10/guide-to-testing-in-django/. Here is a good summary of what to test:

Another common setback for developers/designers new to testing is the question of 'what should (or shouldn't) I test?' While there are no hard & fast rules here that neatly apply everywhere, there are some general guidelines I can offer on making the decision:

  • If the code in question is a built-in Python function/library, don't test it. Examples like the datetime library.

  • If the code in question is built into Django, don't test it. Examples like the fields on a Model or testing how the built-in template.Node renders included tags.

  • If your model has custom methods, you should test that, usually with unit tests.

  • Same goes for custom views, forms, template tags, context processors, middleware, management commands, etc. If you implemented the business logic, you should test your aspects of the code.

So, for your example, there wouldn't really be anything to test until you write some custom functions.
In my opinion, testing ForeignKey and ManyToManyField links would fall under the second category (code built into Django), so I wouldn't test these, as you are really testing whether or not Django is functioning properly. If you have a method which creates an instance of your product, including foreign relationships and M2Ms, you could verify the data has been created, that would be testing your custom method, not Django functionality.

Using the TDD paradigm, the tests are built to verify business logic, and design requirements.

这篇关于为django模型编写测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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