[英]Run Django tests with nosetests
My python apps testing is performed on the remote server with command nosetests
. 我的python应用程序测试是在远程服务器上使用命令
nosetests
。 I cannot modify the way tests are started nor can I add options to it. 我无法修改测试的启动方式,也无法为其添加选项。 I have Django app with tests, but tests are not working properly.
我有测试的Django应用程序,但测试不正常。
My project structure: 我的项目结构:
project
├── README.md
├── setup.py
├── mysite
│ ├── blog
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── tests.py
| | ├── ...
│ ├── db.sqlite3
│ ├── manage.py
│ ├── mysite
│ │ ├── __init__.py
│ │ ├── settings.py
| | ├── ...
Command nosetests
is executed in project
directory. 命令
nosetests
在project
目录中执行。 I want it to properly run tests.py
which has 2 Django testcases. 我希望它能正确运行带有2个Django测试用例的
tests.py
。 I tried creating tests
directory in project root and invoke tests with DiscoverRunner
): 我尝试在项目根目录中创建
tests
目录并使用DiscoverRunner
调用测试:
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
test_dir = os.path.dirname(os.path.dirname(__file__)) # one level up
sys.path.insert(0, os.path.join(test_dir, 'mysite'))
class ServerTest(unittest.TestCase):
def test_runtests(self):
django.setup()
self.test_runner = DiscoverRunner(verbosity=1, interactive=True, failfast=False)
failures = self.test_runner.run_tests(['mysite'])
self.assertEqual(failures, 0)
It works but the problem is all the tests are considered as a single test and wrong reports are produced by the server. 它的工作原理但问题是所有测试都被视为单个测试,并且服务器会生成错误的报告。
Another solution: if I add empty __init__.py
to project/mysite
nose discovers tests.py
but the tests fail because 'Apps are not loaded yet'
which probably means I have to invoke django.setup()
earlier but I don't know how to do it. 另一个解决方案:如果我添加空
__init__.py
到project/mysite
鼻子发现tests.py
但测试失败,因为'Apps are not loaded yet'
这可能意味着我必须提前调用django.setup()
但我不知道怎么做。 I found a plugin for the nose which does it but I cannot install plugins or alter options on the remote machine. 我找到了一个鼻子插件,但是我不能在远程机器上安装插件或更改选项。
Any ideas how to make any of my approaches solve the problem? 任何想法如何使我的任何方法解决问题?
Firsts things first, you should install and configure django-nose
if you haven't done so already: 首先,如果你还没有这样做,你应该安装和配置
django-nose
:
pip install django-nose
Then add it on your INSTALLED_APPS
: 然后将其添加到您的
INSTALLED_APPS
:
INSTALLED_APPS = (
...
'django_nose'
)
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
Now for the nosetests
command to run correctly, you can create a folder named tests
with the following structure: 现在,
nosetests
命令正确运行,您可以使用以下结构创建名为tests
的文件夹:
project
├── mysite
│ ├── blog
│ │ ├── __init__.py
│ │ ├── models.py
| | ├── ...
│ ├── mysite
│ │ ├── __init__.py
│ │ ├── settings.py
| | ├── ...
│ ├── tests
│ │ ├── __init__.py
│ │ ├── test_a_whole_class_of_methods.py
│ │ ├── test_a_whole_view.py
│ │ ├── test_one_of_my_methods.py
│ │ ├── test_another_one_of_my_methods.py
| | ├── ...
│ ├── db.sqlite3
│ ├── manage.py
├── README.md
├── setup.py
DON'T FORGET THE __init__.py
FILE INSIDE THE tests
FOLDER!!! 不要忘记
tests
__init__.py
文件FOLDER !!!
Now when you run nosetests
from the root of your project, it will run every test in the tests
folder: 现在,当您从项目的根目录运行
nosetests
时,它将运行tests
文件夹中的每个测试:
~ $ cd path/to/project
path/to/project $ nosetests
Since you can add files to the root of your project, you could try adding a setup.cfg
file there that nose will look for when executing, and in it specify where to look for tests: 由于您可以将文件添加到项目的根目录,因此您可以尝试在执行时添加一个鼻子将查找的
setup.cfg
文件,并在其中指定查找测试的位置:
# setup.cfg
[nosetests]
where=mysite/blog/
(See the documentation for what parameters you can put here). (请参阅文档 ,了解您可以在此处输入的参数)。
I'm not sure that this will work (it is possible that the command that starts nose has already specified a different configuration file to use), but it seems worth a shot. 我不确定这是否会起作用(启动nose的命令可能已经指定了一个不同的配置文件),但它似乎值得一试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.