如何在Django的测试中使用详细级别

时间:2014-12-13 08:15:39

标签: django django-testing django-unittest

运行Django测试用例允许您像这样指定详细程度(0,1,2,3)

manage.py test -v 2 myapp.tests.test_mycode.TestMyCode.test_func 

如何在test_func

中收到详细标记

1 个答案:

答案 0 :(得分:2)

您可以使用inspect模块:

from django.test import TestCase
import inspect

class MyTest(TestCase):

    def get_verbosity(self):
        for s in reversed(inspect.stack()):
            options = s[0].f_locals.get('options')
            if isinstance(options, dict):
                return int(options['verbosity'])
        return 1

    def test_func(self):
        verbosity = self.get_verbosity()
        self.assertEqual(verbosity, 2)