4

Running a Django test case allows you to specify verbosity (0,1,2,3) like this

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

How can I receive the verbosity flag inside my test_func

user
  • 17,781
  • 20
  • 98
  • 124

1 Answers1

6

You can use inspect module:

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)
catavaran
  • 44,703
  • 8
  • 98
  • 85
  • 1
    Well done. I'm surprised there's no straight forward option available when you inherit from `TestCase` – user Dec 13 '14 at 09:43
  • 1
    I suppose that `TestCase` doesn't intended to output anything so there is no reason to have `verbosity` attribute in it. – catavaran Dec 13 '14 at 09:55