1

I can't find this anywhere, but is it possible to run a unittest from within a django view, to be output to a template? Currently if I run python manage.py test, I get a message like "Ran 10 tests in 0.401s OK". I would like to access this information from one of my views. Any suggestions? Thanks!

Edit: Tried, didnt work:

import StringIO
from django.core import manegement
output = StringIO.StringIO()
management.call_command('test', stdout=output)
print output.contents
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
  • 1
    This might be a crude way to do it, but try [`call_command`](https://docs.djangoproject.com/en/dev/ref/django-admin/#call-command) – karthikr Feb 11 '14 at 03:34
  • If you just need a web interface to view your test results, use a CI tool like buildbot, instead of creating your own. – Leonardo.Z Feb 11 '14 at 03:45

1 Answers1

1

Ended up getting what I wanted with:

import unittest
from testAdditional import UserTestCase #Where my unittest was defined
suite=unittest.TestLoader().loadTestsFromTestCase(UserTestCase)
testResult = unittest.TextTestRunner(verbosity=2).run(suite)
response_data['nrFailed'] = len(testResult.failures)
response_data['output'] = "{}{}".format('\n'.join([result[1] for result in testResult.errors]),'\n'.join([result[1] for result in testResult.failures]))
response_data['totalTests'] = testResult.testsRun
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65