python - Django `manage.py test` 忽略了一些测试

标签 python django unit-testing

我有一些基于 django 的应用程序。在这个应用程序中,我有 tests.py 文件。在这个文件中,我有两个测试类:

# -*- coding: utf-8 -*-

from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User
from translations import utils, views_ajax
from translations.models import Project
import json


def print_results(sentences):
    for sent in sentences:
        print "u\"" + sent + "\","


class TextSplitTest(TestCase):
    maxDiff = None

    def test_en_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'en')
        self.assertEqual(sentences, good_result)

    def test_fr_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'ru')
        self.assertEqual(sentences, good_result)

    def test_zh_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'zh')
        self.assertEqual(sentences, good_result)

    def test_es_split(self):
        # some actions
        sentences, marked_text, count_num = utils.split_text(text_to_split, 'es')
        # print_results(sentences)
        self.assertEqual(sentences, good_result)


class CreateProjectTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.user = User.objects.create_user(username='jacob', email='jacob@gmail.com', password='top_secret')

    def create_project(self):
        request = self.factory.post('/api/project-create/',
                                    data=json.dumps({'name': 4321, 'description': "ololo", 'type': "public"}),
                                    content_type='application/json')
        request.user = self.user
        response = views_ajax.create_project_ajax(request)

        all_user_projects = Project.objects.filter(manager=self.user)

        self.assertEqual(len(all_user_projects), 1)
        self.assertEqual(response.status_code, 200)

    def add_text_to_project(self):
        project = Project.objects.get(manager=self.user, name='4321')
        data = json.dumps({"project":project.id,
                           "title":"French test",
                           "subject":1,
                           "sourceLang":7,
                           "targetLang":2,
                           "textBody":"Some text"})

        request = self.factory.post('/api/text/', data=data, content_type='application/json')
        request.user = self.user

        response = views_ajax.text_ajax(request)

        self.assertEqual(response.status_code, 200)

但是当我尝试开始测试时,我得到:

# python manage.py test -v 2
...
test_basic_addition (entries.tests.SimpleTest) ... ok
test_en_split (translations.tests.TextSplitTest) ... ok
test_es_split (translations.tests.TextSplitTest) ... ok
test_fr_split (translations.tests.TextSplitTest) ... ok
test_ru_split (translations.tests.TextSplitTest) ... ok
test_zh_split (translations.tests.TextSplitTest) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.224s

OK

当我尝试开始准确的测试类(class)时:

# python manage.py test translations.tests.CreateProjectTest -v 2
...
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

为什么 django 会忽略一些测试?我试图用谷歌搜索它,但一无所获:(

最佳答案

您要运行的测试应以单词 test 开头:

def test_create_project(self)

等...

https://docs.python.org/2/library/unittest.html#basic-example

The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.

Django 文档: https://docs.djangoproject.com/en/1.9/topics/testing/overview/#writing-tests

When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite.

关于python - Django `manage.py test` 忽略了一些测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411331/

相关文章:

unit-testing - 使用 Mockito 模拟 Akka Actor 日志对象

python - 删除json中不必要的元素

python - 从 python 运行命令行并从内存中传递参数

Python 网页中的 Javascript 事件处理程序不起作用

python - 如何在 Flask 中提供静态文件

django - 有什么办法可以更改登录的Django-rest-auth的 View 吗?

python - 在 Django 中渲染模板变量时出现问题

python - 如何子类化 Django TextChoices 以添加其他属性?

python - 如何修补 Python 的 datashape 中装饰器注册的方法?

Python Unittest2 - 避免在 discover() 中包含 TestCase