How can I make Python's UnitTest Framework fail if an exception is raised by any thread?

0 votes

Hi. I've been using the unit test framework to automate integration tests of the following:-

  • multi-threaded python code
  • external hardware 
  • embedded C
I need the tests to fail if even one exception is raised by ay of the threads. Is this possible? 

A simple but non-workable solution would be to either 

  • refactor the code to avoid multi-threading 
  • test each thread separately

I can't perform either of these because the code interacts asynchronously with the hardware. I've also given a thought to passing messages to forward the exception the main unittest thread but that would require significant testing related changes to the code, and I want to avoid that at this moment.

For example, can I modify the test script below to fail on the exception raised in my_thread without modifying the x.ExceptionRaiser class?

import unittest
import x

class Test(unittest.TestCase):
    def test_x(self):
        my_thread = x.ExceptionRaiser()
        # Test case should fail when thread is started and raises
        # an exception.
        my_thread.start()
        my_thread.join()

if __name__ == '__main__':
    unittest.main()
Dec 5, 2018 in Python by charlie_brown
• 7,780 points
3,492 views

2 answers to this question.

0 votes

I've come across this problem myself, and the only solution I've been able to come up with is subclassing Thread to include an attribute for whether or not it terminates without an uncaught exception:

from threading import Thread

class ErrThread(Thread):
    """                                                                                                                                                                                               
    A subclass of Thread that will log store exceptions if the thread does                                                                                                                            
    not exit normally                                                                                                                                                                                 
    """
    def run(self):
        try:
            Thread.run(self)
        except Exception as self.err:
            pass
        else:
            self.err = None


class TaskQueue(object):
    """                                                                                                                                                                                               
    A utility class to run ErrThread objects in parallel and raises and exception                                                                                                                     
    in the event that *any* of them fail.                                                                                                                                                             
    """

    def __init__(self, *tasks):

        self.threads = []

        for t in tasks:
            try:
                self.threads.append(ErrThread(**t)) ## passing in a dict of target and args
            except TypeError:
                self.threads.append(ErrThread(target=t))

    def run(self):

        for t in self.threads:
            t.start()
for t in self.threads:
            t.join()
            if t.err:
                raise Exception('Thread %s failed with error: %s' % (t.name, t.err))
answered Dec 5, 2018 by aryya
• 7,380 points
0 votes
http://www.drdobbs.com/testing/testing-for-failures-with-python/240169387

Try reading the above document. I assume what you are looking for is post-failure testing?
answered Mar 9, 2019 by anonymous

Related Questions In Python

0 votes
1 answer

How can I raise an exception in Python so that it can later be caught via an except block?

It's pretty simple to raise a query  raise ...READ MORE

answered May 29, 2019 in Python by Umesh
151 views
0 votes
1 answer

How can I make an EXE file from a Python program?

Auto PY to EXE - A .py ...READ MORE

answered Aug 6, 2019 in Python by SDeb
• 13,250 points
339 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,475 views
0 votes
0 answers
0 votes
1 answer

How can I print an Error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Oct 12, 2018 in Python by aryya
• 7,380 points
298 views
0 votes
1 answer

How can I turn an input date into a readable string in Python?

The datetime class has a method strftime. strftime() ...READ MORE

answered Dec 11, 2018 in Python by aryya
• 7,380 points
79 views