Django测试外部脚本 [英] Django testing external script

查看:32
本文介绍了Django测试外部脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对与我的Django应用程序(即数据库)交互的脚本执行测试.通常,当我们想在Django中测试某些东西时,我们只需启动内置的测试套件即可.通过此测试套件,我们甚至可以获得不错的命令行开关,例如使用不同的设置文件覆盖内置的 settings.py :

I want to perform testing on a script that interacts with my Django application, namely the database. Normally, when we want to test something in Django we simply fire up the built in test suite. With this test suite we even get nice command line switches such as overriding the built in settings.py with a different settings file:

python manage.py test myApp --settings='settings_test'

问题出在这里:

1)我想测试不是应用程序一部分的脚本,所以我不知道使用 manage.py 调用测试套件的方法.这可能吗?即:

1) I want to test said script which is not part of an app, so I know of no way to invoke the test suite using manage.py. Is this possible? I.e.:

python manage.py test /path/myScript.py --settings='settings_test'

推荐答案

我建议使用其他测试运行程序.

I would suggest to use a different test runner.

您可以执行 pip install django-nose ,然后在 test_settings.py

TEST_RUNNER = `django_nose.NoseTestSuiteRunner`

现在您可以使用

./manage.py test --settings=yourproject.test_settings.py 

,Nose testrunner将在所有子文件夹中搜索名为 tests 的文件夹,并在这些文件夹中搜索以 _tests.py 结尾的文件(在这些文件中将像往常一样搜索从 TestCase 派生的类.

and the Nose testrunner will search all subfolders for folders called tests and in those folders it will search for files that end with _tests.py (and in those files it will search for classes that derive from TestCase, as usual).

因此您的项目结构应如下所示:

So your project structure should look something like this:

- Project-Root/
  - Your-Non-App-Code/
    - __init__.py
    - non_app_code.py
    - tests/
      - __init__.py
      - non_app_code_tests.py

有关如何安装django-nose的更多信息,请查看其Github存储库: https://github.com/django-nose/django-nose

For more info on how to install django-nose, check their Github repo: https://github.com/django-nose/django-nose

这篇关于Django测试外部脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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