相关疑难解决方法(0)

当您的应用程序具有测试目录时,在Django中运行特定的测试用例

Django文档(http://docs.djangoproject.com/en/1.3/topics/testing/#running-tests)表示您可以通过指定它们来运行单个测试用例:

$ ./manage.py test animals.AnimalTestCase
Run Code Online (Sandbox Code Playgroud)

这假设您在Django应用程序的tests.py文件中进行了测试.如果这是真的,那么这个命令就像预期的那样工作.

我在测试目录中测试了Django应用程序:

my_project/apps/my_app/
??? __init__.py
??? tests
?   ??? __init__.py
?   ??? field_tests.py
?   ??? storage_tests.py
??? urls.py
??? utils.py
??? views.py
Run Code Online (Sandbox Code Playgroud)

tests/__init__.py文件有一个suite()函数:

import unittest

from my_project.apps.my_app.tests import field_tests, storage_tests

def suite():
    tests_loader = unittest.TestLoader().loadTestsFromModule
    test_suites = []
    test_suites.append(tests_loader(field_tests))
    test_suites.append(tests_loader(storage_tests))
    return unittest.TestSuite(test_suites)
Run Code Online (Sandbox Code Playgroud)

要运行测试我做:

$ ./manage.py test my_app
Run Code Online (Sandbox Code Playgroud)

尝试指定单个测试用例会引发异常:

$ ./manage.py test my_app.tests.storage_tests.StorageTestCase
...
ValueError: Test label 'my_app.tests.storage_tests.StorageTestCase' should be of the form app.TestCase or app.TestCase.test_method
Run Code Online (Sandbox Code Playgroud)

我试着做异常消息说的话:

$ ./manage.py test my_app.StorageTestCase
... …
Run Code Online (Sandbox Code Playgroud)

python django unit-testing

148
推荐指数
5
解决办法
7万
查看次数

鼻子没有找到Django测试

我试图使用Django的鼻子在我目前的项目,但我无法弄清楚如何让鼻子跑我的测试.所以我开始了一个简单的Django 1.4.1项目来了解鼻子.但即使在这个简单的测试项目中,我也无法运行它.

在继续之前:我知道Stackoverflow上有很多类似的问题,比如这个问题:

我怎么告诉Django-nose我的测试在哪里?

但谷歌搜索后,阅读博客文章和StackOverflow答案我仍然无法运行.

我如何设置我的测试项目

  1. 创建虚拟环境.
  2. pip install django django-nose nose.
  3. django-admin.py startproject djangonosetest.创建项目.
  4. 创建一个应用程序 manage.py startapp testapp
  5. 编辑settings.py:

    • 设置ENGINEdjango.db.backends.sqlite3
    • 添加django_nose,testappINSTALLED_APPS
    • 补充说TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'.
  6. manage.py test

但我得到的只是这个输出:

nosetests --verbosity 1
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
Destroying test database for alias 'default'...
Run Code Online (Sandbox Code Playgroud)

但至少应该运行默认的测试用例.

当我运行python manage.py test djangonosetest.testapp.tests:SimpleTest它时,将运行测试.但是,如果我必须为每个测试文件执行此操作,那似乎有点矫枉过正.但它证明了测试可以运行.

当我跑manage.py test -v 3(高级别级别)时,这出现了:

nose.selector: INFO: /Users/Jens/Projects/Django/djangonosetest/djangonosetest/settings.py …
Run Code Online (Sandbox Code Playgroud)

django nose django-nose

13
推荐指数
1
解决办法
3321
查看次数

标签 统计

django ×2

django-nose ×1

nose ×1

python ×1

unit-testing ×1