python - 鼻子没有发现Django中的包级别测试 [英] python - Nose not discovering package level tests in Django

查看:138
本文介绍了python - 鼻子没有发现Django中的包级别测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Django应用程序设置一个目录结构,以分离功能和单元测试。我正在使用鼻子作为我的Django项目的测试运行器。

I'm setting up a directory structure for my Django app to separate functional and unit tests. I am using nose as the test runner for my Django project.

在Django项目的根目录下,我有一个名为tests的文件夹,具有以下结构: / p>

At the root of the Django project, I have a folder called "tests" that has this structure:

tests
├── __init__.py
├── functional
│   ├── __init__.py
└── unit
    ├── __init__.py
    ├── data.py
    ├── tests.py

如果我想运行单元测试,我应该不能使用项目根目录中的以下内容:

If I want to run just the unit tests, should I not be able to use the following from the project root:

$ nosetests tests.unit

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

OK

As你可以看到,这没有在tests.py文件中找到测试。

As you can see, this doesn't find the tests in the tests.py file.

但是,当我运行目录结构时,会发现这些测试应该是是:

However, when I run using the directory structure, the tests are found as they should be:

$ nosetests tests/unit/
E
# .. Some errors I expected because settings are not initialized when called this way
-----------------
Ran 1 test in 0.001s

FAILED (errors=1)

我缺少什么?我的主要问题是我在 tests.unit .__ init __。py 中有一个设置应该被调用来创建

What am I missing? My main issue is that I have a setup function in tests.unit.__init__.py that should be called for creating the data in the test DB for the upcoming tests.

谢谢

推荐答案

p>这一切都取决于什么样的代码在 tests / unit / __ init __。py

This all depends on what kind of code is in tests/unit/__init__.py

当你说

nosetests tests.unit

您指向 unit / __ init __。py 不是目录 unit / 因此,如果你在你的 __ init __。py 模块中没有测试,那么没有任何运行。所以当您使用目录路径,然后测试开始工作时,这是可以理解的。

You are pointing to unit/__init__.py not the directory unit/ thus if you had no tests in your __init__.py module then nothing would be run. So it is understandable when you say you used the directory path and then your tests started working.

您提到


我缺少什么?我的主要问题是我在
tests.unit中有一个设置函数。 init .py应该被调用以在
中为即将到来的测试创建测试数据库中的数据。

What am I missing? My main issue is that I have a setup function in tests.unit.init.py that should be called for creating the data in the test DB for the upcoming tests.

尽管您在 __ init __。py 中有一个安装程序,您可能还没有将测试函数导入到 __ init __。py

It is likely that although you have a setup function in __init__.py you may have not ever imported your test functions into __init__.py

一个快速的修复将是添加这行在 __ init __。py

One quick fix to this would be to add this line in __init__.py

from tests.unit.tests import *

表示将中的任何代码放在真的不是很明智__init __。py ,如果您有代码返回某种配置数据,我建议您创建一个具有将配置数据返回到测试的功能的新库模块。

That said it is really not very wise to be putting any code in __init__.py at all and if you have code that returns some kind of configuration data I would recommend creating a new library module with functions that will return configuration data to your tests

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

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