Skip to content

Instantly share code, notes, and snippets.

@aychedee
Last active September 8, 2021 10:47
Show Gist options
  • Save aychedee/5244147 to your computer and use it in GitHub Desktop.
Save aychedee/5244147 to your computer and use it in GitHub Desktop.
Demonstration running a test multiple times using unittest
import unittest
import random
class NullWriter(object):
def write(*_, **__):
pass
def flush(*_, **__):
pass
SETUP_COUNTER = 0
class MyTestCase(unittest.TestCase):
def setUp(self):
global SETUP_COUNTER
SETUP_COUNTER += 1
def test_one(self):
self.assertTrue(random.random() > 0.3)
def test_two(self):
# We just want to make sure this isn't run
self.assertTrue(False, "This should not have been run")
def suite():
tests = []
for _ in range(100):
tests.append('test_one')
return unittest.TestSuite(map(MyTestCase, tests))
results = unittest.TextTestRunner(stream=NullWriter()).run(suite())
print dir(results)
print 'setUp was run', SETUP_COUNTER, 'times'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment