Is there a way to introduce a small delay between each unittest case?

Viewed 53

i'm testing an application inside a docker container, and it seems that the application crashes for some virtualization reason (i'm forced to use an qemu emulated amd64 container on an M1 Mac).

I've read that the qemu layer stability is I/O sensible. Is there a way to slow dow the unittest runner, possibly with a delay/sleep time between each test ?

Thx in advance

1 Answers

If you don't want to insert some sleep time in the setUp or tearDown methods, you can subclass and override the default TestResult by doing something like this:

import time
import unittest

class SlowTestResult(unittest.TestResult):
    def startTest(self, test):
        time.sleep(1)
        super().startTest(test)

slowTestRunner = unittest.TextTestRunner(resultclass=SlowTestResult)

if __name__ == '__main__':
    unittest.main(testRunner=slowTestRunner)
Related