Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e4a98b

Browse files
committed
#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
1 parent aaa5d1c commit 3e4a98b

2 files changed

Lines changed: 15 additions & 27 deletions

File tree

Lib/test/test_random.py

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from math import log, exp, pi, fsum, sin
99
from test import support
1010

11-
class TestBasicOps(unittest.TestCase):
11+
class TestBasicOps:
1212
# Superclass with tests common to all generators.
1313
# Subclasses must arrange for self.gen to retrieve the Random instance
1414
# to be tested.
@@ -142,7 +142,15 @@ def test_bug_9025(self):
142142
k = sum(randrange(6755399441055744) % 3 == 2 for i in range(n))
143143
self.assertTrue(0.30 < k/n < .37, (k/n))
144144

145-
class SystemRandom_TestBasicOps(TestBasicOps):
145+
try:
146+
random.SystemRandom().random()
147+
except NotImplementedError:
148+
SystemRandom_available = False
149+
else:
150+
SystemRandom_available = True
151+
152+
@unittest.skipUnless(SystemRandom_available, "random.SystemRandom not available")
153+
class SystemRandom_TestBasicOps(TestBasicOps, unittest.TestCase):
146154
gen = random.SystemRandom()
147155

148156
def test_autoseed(self):
@@ -239,7 +247,7 @@ def test_randbelow_logic(self, _log=log, int=int):
239247
self.assertTrue(2**k > n > 2**(k-1)) # note the stronger assertion
240248

241249

242-
class MersenneTwister_TestBasicOps(TestBasicOps):
250+
class MersenneTwister_TestBasicOps(TestBasicOps, unittest.TestCase):
243251
gen = random.Random()
244252

245253
def test_guaranteed_stable(self):
@@ -542,28 +550,5 @@ def __init__(self, newarg=None):
542550
Subclass(newarg=1)
543551

544552

545-
def test_main(verbose=None):
546-
testclasses = [MersenneTwister_TestBasicOps,
547-
TestDistributions,
548-
TestModule]
549-
550-
try:
551-
random.SystemRandom().random()
552-
except NotImplementedError:
553-
pass
554-
else:
555-
testclasses.append(SystemRandom_TestBasicOps)
556-
557-
support.run_unittest(*testclasses)
558-
559-
# verify reference counting
560-
import sys
561-
if verbose and hasattr(sys, "gettotalrefcount"):
562-
counts = [None] * 5
563-
for i in range(len(counts)):
564-
support.run_unittest(*testclasses)
565-
counts[i] = sys.gettotalrefcount()
566-
print(counts)
567-
568553
if __name__ == "__main__":
569-
test_main(verbose=True)
554+
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ Tests
120120
- Issue #12820: add tests for the xml.dom.minicompat module.
121121
Patch by John Chandler and Phil Connell.
122122

123+
- Issue #17789: test_random now works with unittest test discovery.
124+
Patch by Zachary Ware.
125+
123126
- Issue #17779: test_osx_env now works with unittest test discovery.
124127
Patch by Zachary Ware.
125128

0 commit comments

Comments
 (0)