且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何传播django单元测试多个文件?

更新时间:2023-01-18 14:43:42

Django 1.6中的行为发生了变化,所以不再需要创建一个包。只需命名您的文件 test * .py

The behavior has changed in Django 1.6, so there is no longer a need to create a package. Just name your files test*.py.

从Django 1.7文档


当您运行测试时,测试实用程序的默认行为是
,以查找所有测试用例(即unittest.TestCase的子类)
在任何名称以test开头的文件中,自动构建一个测试
suite在这些测试用例中,并运行该套件。

When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite.

Django 1.6文档,


测试发现是基于unittest模块内置测试
发现。默认情况下,这将在当前工作目录下的任何名为
test * .py的文件中发现测试。

Test discovery is based on the unittest module’s built-in test discovery. By default, this will discover tests in any file named "test*.py" under the current working directory.

以前的行为,从Django 1.5文档


当您运行测试时,测试实用程序的默认行为为
,以查找所有测试用例(即unittest.TestCase的子类)
models.py和tests.py自动从
中的这些测试用例中建立一个测试套件,并运行该套件。

When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in models.py and tests.py, automatically build a test suite out of those test cases, and run that suite.

有另外一种方法来定义一个模块的测试套件:如果
在models.py或tests.py中定义一个名为suite()的函数,
Django测试运行器将使用该函数构建测试套件
为该模块。这符合单位
测试的建议组织。有关如何
构建复杂测试套件的更多详细信息,请参阅Python文档。

There is a second way to define the test suite for a module: if you define a function called suite() in either models.py or tests.py, the Django test runner will use that function to construct the test suite for that module. This follows the suggested organization for unit tests. See the Python documentation for more details on how to construct a complex test suite.