为什么django没有看到我的测试?

use*_*495 8 python testing django client

我已经创建了test.py模块,填充了

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from forum.models import *

class SimpleTest(TestCase):


    def setUp(self):
        u = User.objects.create_user("ak", "ak@abc.org", "pwd")
        Forum.objects.create(title="forum")
        Site.objects.create(domain="test.org", name="test.org")

    def content_test(self, url, values):
        """Get content of url and test that each of items in `values` list is present."""
        r = self.c.get(url)
        self.assertEquals(r.status_code, 200)
        for v in values:
            self.assertTrue(v in r.content)

    def test(self):
        self.c = Client()
        self.c.login(username="ak", password="pwd")

        self.content_test("/forum/", ['<a href="/forum/forum/1/">forum</a>'])
        ....
Run Code Online (Sandbox Code Playgroud)

并将其放在我的应用程序的文件夹中.当我运行测试时

python manage.py test forum
Run Code Online (Sandbox Code Playgroud)

创建测试数据库后,我得到一个答案"在0.000s内进行0测试"

我究竟做错了什么 ?

PS这是我的项目层次结构:

MyProj:
    forum (it's my app):
        manage.py
        models.py
        views.py
        tests.py
        ...
Run Code Online (Sandbox Code Playgroud)

我将test.py重命名为tests.py.Eclipse得到了这个模块的测试,但答案仍然是"在0.000s内进行0测试"

小智 35

您需要test_为每个测试方法使用前缀.


Pau*_*que 11

摘要:

0)尝试仅针对您的应用运行:

python manage.py test YOUR_APP
Run Code Online (Sandbox Code Playgroud)

1)如果YOUR_APP在INSTALLED_APP配置中,请检入settings.py文件

2)测试方法应以"test"开头,例如:

def test_something(self):
    self.assertEquals(1, 2)
Run Code Online (Sandbox Code Playgroud)

3)如果您使用名为tests而不是tests.py文件的目录,请检查其中是否包含init .py文件.

4)如果您使用的是测试目录,请删除tests.pyctests.pyo文件.(Python3的pycache目录)

  • 除了4之外,别忘了删除`__pycache__`文件夹 (2认同)

Ala*_*air 6

尝试将您的方法重命名test为类似的方法test_content.

我相信测试运行器将运行所有命名的方法test_*(参见用于组织测试代码的python文档.Django TestCase是子类unittest.TestCase,因此应该应用相同的规则.


sim*_*rsh 5

你必须说出来tests.py.


Man*_*dan 3

如果将文件重命名为tests.py. 你如何进行测试?您是从命令行执行此操作还是使用 Eclipse 设置自定义运行目标?如果您还没有尝试过,请从命令行尝试一下。

同时启动 Django shell ( python manage.py shell) 并导入您的测试模块。

from MyProj.forum.tests import SimpleTest
Run Code Online (Sandbox Code Playgroud)

导入工作正常吗?