没有找到Django包测试 [英] Django Package tests not found

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

问题描述

我有一个奇怪的问题。

我正在创建一个django包,并希望为其提供一些测试用例。

I'm creating a django package and would like to provide some test cases for it.

包名为ajax_forms,完整路径是我的PYTHONPATH的一部分。
此目录还包含一个测试目录(使用 __ init __ .py文件)一个tests.py文件(包含测试)和一个urls.py文件。

The package is named ajax_forms and the full path is part of my PYTHONPATH. This directory also contains a tests directory (with the __init__.py file) a tests.py file (containing the tests) and a urls.py file.

现在我想运行测试。

ajax_forms父目录有一个示例项目设置, ajax_forms在INSTALLED_APPS。

The ajax_forms parent directory has an example project setup with ajax_forms in INSTALLED_APPS.

但我似乎不能让测试运行:

But I can't seem to let the tests run:

 >> ./manage.py test ajax_forms
Creating test database for alias 'default'...

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

OK
Destroying test database for alias 'default'...

没有发现任何测试,所以我尝试更具体:

No tests are found so I try to be more specific:

 >> ./manage.py test ajax_forms.tests
....
raise ValueError("Test label '%s' does not refer to a test" % label)
ValueError: Test label 'ajax_forms.tests' does not refer to a test
(ajax-validation)

甚至。 /manage.py ajax_forms.tests.tests.AjaxFormViewTest将找不到测试并给出与上述相同的错误。

Even ./manage.py ajax_forms.tests.tests.AjaxFormViewTest will not find the tests and gives the same error as above.

但尝试从./中导入测试用例manage.py shell工作正常

But trying to import the test case from the ./manage.py shell works fine

In [2]: from ajax_forms.tests.tests import AjaxFormViewTest

我在这里很无聊,发生了什么。

I'm kinda clueless here, on what's going on.

推荐答案

您需要将 __ init __。py 文件中的

You need to put from tests import * in your __init__.py file

Django将在您的应用程序中查找一个名为tests的包/模块,然后询问其命名空间用于测试用例。在这种情况下,它将查看测试包的命名空间(从 init .py文件),并且不发现任何内容。通过在 __ init __。py 文件中导入,您将从测试模块导入测试库到测试包的命名空间,并且django将能够找到它们运行它们。

Django will look in your app for a package/module called tests, and then interrogate its namespace for test cases. In this case it will look at the namespace of your tests package (from the init.py file), and discover nothing there. By doing the import in the __init__.py file then you will import the testcases from your tests module into the namespace of the tests package, and django will be able to locate them and run them.

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

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