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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.