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

标签 django django-testing django-unittest

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

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

如何在我的 test_func 中接收详细标志

最佳答案

您可以使用 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)

关于django - 如何在 Django 的测试中使用详细级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456881/

相关文章:

python - Django 模板未呈现

python - Django 单元测试 - 创建对象的 ID

在模板上呈现时,Django 子表单不显示标签

python - Django,自定义错误页面重定向到 500 而不是 404

Django 单元测试 on_delete CASCADE

python - Django 在 View 中测试 ajax 端点

python - Django 测试客户端提交带有 POST 请求的表单

django - HttpResponse 对象在传递给 assertContains 时变为字符串

django 测试 response.request.user.is_authenticated() 在注销后返回 True

django - 在 Django 中渲染 {{ var }} 和 {{ var|safe }} 的区别