我如何在Django中以比应用程序更高的粒度分组单元测试? [英] How do I group unit tests in Django at a higher granularity than the app?

查看:134
本文介绍了我如何在Django中以比应用程序更高的粒度分组单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django项目中,单元测试通常由应用程序组织,Django提供运行所有测试,单个应用程序测试,单个测试用例或单个测试方法的功能。



然而,我发现单个应用程序中的测试用例通常属于单独的逻辑组或集群,而且只能运行一组测试通常是有用的。想象一下,例如:




  • 我有几个测试用例涵盖了单个模型的不同配置,我想运行它们(只是他们),同时开发模型。 (这是特别麻烦的,因为这样做的便宜方法是将测试用例集中在一起,所以现在你有一个测试用例,您可以轻松地运行,但是是一个噩梦来阅读和维护)。

  • 我有一系列实用程序类,其测试非常快,因为它们没有打到数据库。我不需要运行模型测试并在重构这些测试时查看测试。



我搜索了。但是,在多个文件中分割测试相对简单(参见例如这些 两个问题),似乎没有分开运行测试组的简单方法。



那么我是否缺少一些东西?分组测试的秘诀是什么?

解决方案

值得一看的python鼻子测试框架,以解决这个问题



具体来说,您可以标记测试或设置属性。然后只运行一个测试集(通过应用程序或整个项目)标记给定的标签或标签。



https://nose.readthedocs.org/en/latest/plugins/attrib.html?highlight=tags



注意,鼻子扩展了单元测试,所以你现在的django unittest套件很可能已经与跑步者一起运行。


In a Django project, unit test are usually organized by app, with Django providing a facility to run all of the tests, the tests for a single app, a single test case, or a single test method.

Yet, I find that test cases in a single application usually belong to separate logical groups or clusters, and it would be often useful to be able to only run a single group of tests. Imagine, for example:

  • I have several test cases covering each a different configuration of a single model, and I want to run all of them (and just them) while developing the model. (This is especially troublesome, because the cheap way to do that is to lump the test cases together, so now you have a single test case which you can easily run, but is a nightmare to read and maintain).
  • I have a series of utility classes whose tests are extremely quick because they do not hit the database. I don't need to run model tests and view tests while refactoring these.

I searched around. But while it is relatively straightforward to split your tests in multiple files (see for example these two questions), there doesn't seem to be a straightforward way to run the test groups separately.

So, am I missing something? What is the secret sauce to group tests?

解决方案

It's worth looking at the nose test framework for python for one approach to this problem.

Specifically you can tag tests or set attributes on them. Then only run a subset of tests (across an app or your whole project) tagged with a given tag or tags.

https://nose.readthedocs.org/en/latest/plugins/attrib.html?highlight=tags

Note that nose extends unittest, so you're existing django unittest suite will likely already run with the nose runner.

这篇关于我如何在Django中以比应用程序更高的粒度分组单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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