Login

Forcing unit test runner to abort after failed test

Author:
simonbun
Posted:
May 22, 2007
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

Sometimes running a test suite can take a long time. This can get especially annoying when you see a failure or error, but have to wait for all the remaining tests to run before you can see the relevant stack traces.

Pressing ctrl+c to interrupt the tests does not work; the KeyboardInterrupt exception does not get caught and only a stack trace irrelevant to your tests is produced.

This behavior can be altered through overriding the testcase's run method and catching the exception yourself. Now you can stop the tests whenever you want to and still see what is wrong with your tests.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.test import TestCase

class SimpleTest(TestCase):
    ''' Your unit tests go here '''  

    def test_a(self):
        pass
  
  
    def run(self, result=None):
        if result is None: result = self.defaultTestResult()
        try:
            super(SimpleTest, self).run(result)
        except KeyboardInterrupt:
            result.stop()
      

More like this

  1. Image compression before saving the new model / work with JPG, PNG by Schleidens 2 weeks, 2 days ago
  2. Help text hyperlinks by sa2812 1 month, 1 week ago
  3. Stuff by NixonDash 3 months, 2 weeks ago
  4. Add custom fields to the built-in Group model by jmoppel 5 months, 3 weeks ago
  5. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 9 months ago

Comments

Please login first before commenting.