Last active
September 8, 2021 10:47
-
-
Save aychedee/5244147 to your computer and use it in GitHub Desktop.
Demonstration running a test multiple times using unittest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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