为什么Django testrunner没有找到我创建的测试?

时间:2010-05-25 15:55:54

标签: python django

我一直在尝试为我正在开发的项目添加测试。

测试在forum / tests /

当我运行manage.py测试时,在Django 1.2的测试中找不到我创建的任何测试

我从他们自己的软件包开始我的所有测试,但已经简化为只在我的tests.py文件中。当前的tests.py看起来像:

from django.test.client import Client  
from django.test import TestCase  
from utils import *   
from forum.models import *  
from forum import auth  

class ForumTestCase(TestCase):  
    def test_root_page(self):  
        response = self.client.get('/')  
        self.assertEqual(response.status_code, 200)  

    def test_signin_page(self):  
        response = self.client.get("/account/signin/")  
        self.assertEqual(response.status_code, 200)  

我确信我遗漏了一些非常基本和明显的东西,但我无法解决问题。有任何想法吗?

INSTALLED_APPS = (  
    'django.contrib.auth',  
    'django.contrib.contenttypes',  
    'django.contrib.sessions',  
    'django.contrib.sites',  
    'django.contrib.admin',  
    'django.contrib.humanize',  
    'forum',  
    'django_authopenid',  
)  

为什么Django testrunner没有找到我创建的测试?

测试在forum / tests /:

__init__.py
forum/tests/test_views.py  
forum/tests/test_models.py

我目录中也有一个__init__.py文件。

1 个答案:

答案 0 :(得分:7)

正如评论中所述,Django 1.6引入了与 discovery of tests in any test module 的向后兼容性。

在Django 1.6之前,必须做以下事情:

中创建名为__init__.py的文件
forum/tests/__init__.py

并从其中的其他模块导入所有测试。

from test_views import SomeTestCase
from test_models import SomeOtherTestCase