Skip to main content
 首页 » 编程设计

python之构建 Django 测试和测试不会运行

2024年10月24日33unruledboy

我是 Django 测试的新手,我正在尝试为我的 Django 项目编写一些单元测试,但是当我将测试移动到一个新文件夹时。我收到以下消息。

Creating test database for alias 'default'... 
 
---------------------------------------------------------------------- 
Ran 0 tests in 0.000s # <======== ATTENTION 
 
OK 
Destroying test database for alias 'default'... 

测试成功通过。

python manage.py test public --traceback 

旧结构

project | 
  public | #app 
     tests.py| 
  private | #app 
     tests.py 

新结构

project | 
  public | #app 
     tests| 
        __init__.py 
        test_users.py 
        test_models.py 
        test_forms.py 
  private | #app 
     tests.py 

使用该结构我无法运行测试。

测试用户.py
class UserGroupCase(TestCase): 
    def setUp(self): 
        super(TestCase, self).setUp() 
        self.groups = ["Regular", "Photographer", "Blogist"] 
        for group in self.groups: 
            Group.objects.create(name=group) 
 
    def test_adding_user_in_group(self): 
        self.assertTrue(len(self.groups), 2) 
        regular = Group.objects.get(name=self.groups[0]) 
        photographer = Group.objects.get(name=self.groups[1]) 
        blogist = Group.objects.get(name=self.groups[2]) 
 
        robo1 = MyUser.objects.get(username="robot1") 
 
        regular.user_set.add(robo1) 

请帮助解决我在这里遗漏的问题。

Django1.5.1 

请您参考如下方法:

我找到了答案,将其张贴在这里,因为它可能对其他人有帮助。它与 DJANGO 1.5

有关

您可以将测试添加到 test/anydir/anydir1/test_user.py,但将每个测试导入到 tests/__init__.py 直到 django 1.5。

Also there is a test runner which will work the way unittest2 discovery work. This functionality has been integrated into django1.6.