我如何告诉 django-nose 我的测试在哪里?
我在测试目录中对 Django 应用程序进行了测试:
my_project/apps/my_app/
├── __init__.py
├── tests
│ ├── __init__.py
│ ├── field_tests.py
│ └── storage_tests.py
├── urls.py
├── utils.py
└── views.py
Django 测试运行程序要求我将 suite() 函数放入应用程序测试目录的 __init__.py 文件中。该函数返回我执行时将运行的测试用例 $ python manage.py test
我安装了 django-nose。当我尝试使用 django-nose 运行测试时,会运行 0 个测试:
$ python manage.py test <app_name>
如果我直接指向测试模块,则会运行测试:
$ python manage.py test my_project.apps.my_app.tests.storage_tests
为什么 django-nose 的测试运行程序找不到我的测试?我必须做什么?
I have my tests for a Django application in a tests directory:
my_project/apps/my_app/
├── __init__.py
├── tests
│ ├── __init__.py
│ ├── field_tests.py
│ └── storage_tests.py
├── urls.py
├── utils.py
└── views.py
The Django test runner requires that I put a suite() function in the __init__.py
file of my application's tests directory. That function returns the test cases that will run when I do
$ python manage.py test
I installed django-nose. When I try to run the tests with django-nose, 0 tests are run:
$ python manage.py test <app_name>
If I point directly at the test module, the tests are run:
$ python manage.py test my_project.apps.my_app.tests.storage_tests
Why does django-nose's test runner not find my tests? What must I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用 django-nose,您可以使用一个简单的选择器:
这只是一个示例实现,您可以使其更具可配置性或根据您的需要进行调整。要在 Django 项目中使用它,只需在 settigs.py 中提供以下选项
If you use django-nose you can use a simple selector:
This is just an example implementation and you can make it more configurable or adjust it to your needs. To use it in your Django project just provide the following option in settigs.py
错误:
实际上是在类名之前加上斜杠并加上文件扩展名
正确:
Wrong:
is in fact with slashes until the class name and with the extension of the file
Right:
如果您在模块的开头添加 test = True 并且您的方法以 test_ 开头,nose 会自动选择,
请参阅nose 如何检测测试 http://nose.readthedocs.io/en/latest/finding_tests.html
nose will pick automatically, if you add test = True in the beginning of module and if your methods starts with test_
see how nose detects tests http://nose.readthedocs.io/en/latest/finding_tests.html
我也遇到过同样的情况。使用该套件,您可以运行所有测试或应用程序的测试,但不能运行特定的测试用例或特定的测试。
这是相当 hacky 的,但我为解决这个问题所做的不是在 __init__.py 中定义一个套件,而是从所有其他测试模块中导入 * ,这很糟糕,但它有效。
我做了一些事情来帮助确保我不会最终破坏其他模块中的测试套件...在每个测试模块中声明
__all__
,以便仅导入测试名称* 并保持 pylint 运行 我收到类名重新定义的通知。话虽如此,你应该能够让它工作,而不需要任何丑陋的
import *
废话...我不使用鼻子和 django-nose...(我打算很快纠正)。 ..因为这就是您正在做的事情,看起来您可以这样做来运行应用程序目录中的所有测试:或者运行单个测试模块的所有测试:
请注意,我没有将该项目包含在前面的例子......使用nosetests对我来说似乎工作得很好姜戈鼻子。
另外,要运行特定的测试套件,您可以执行以下操作:
或者运行一个特定的测试:
I have run into this same situation as well. Using the suite it will allow you to run all tests, or tests for an app, but not a specific test case or a specific test.
It's pretty hacky, but what I've done to get around this is instead of defining a suite in
__init__.py
is to justimport *
from all of the other test modules, it sucks but it works.There are a few things that I do to help make sure that I don't end up clobbering test suites in other modules... have
__all__
declared in each test modules so only the test names are imported with the * and keep pylint running I'm notified of class name redefinitions.Having said that you should be able to get this to work without any ugly
import *
crap... I do not use nose and django-nose...(which I intend to correct very soon)... since that is what you're doing it looks like you can do this to run all of the tests in your apps directory:or to run all of the tests for a single test module:
notice I did not include the project in the previous example... which seemed to work fine for me using nosetests and django-nose.
Also, to run a specific test suite you can do this:
Or to run one specific test:
为了让鼻子意识到你的应用程序的位置,将它们添加到manage.py的顶部,
从那时起你可以使用
python manage.py test my_app.tests.storage_tests:TestCase.test_name
To make nose realize where your apps are add these to the top of manage.py
Since then you can use
python manage.py test my_app.tests.storage_tests:TestCase.test_name