Skip to content

Instantly share code, notes, and snippets.

@atdt
Created March 22, 2012 23:22
Show Gist options
  • Select an option

  • Save atdt/2165432 to your computer and use it in GitHub Desktop.

Select an option

Save atdt/2165432 to your computer and use it in GitHub Desktop.
Randomized tests for Nose 2
# -*- coding: utf-8 -*-
"""
nose2-random
This plug-in facilitates capturing and setting the state of the internal
random number generator prior to test execution. Test output is annotated
with a seed value that you can re-use to reproduce randomly-generated test
data across runs.
.. note ::
Random values produced from external randomness sources (for example, via
``os.urandom``) will not be reproducible.
:copyright: (c) 2012 by Ori Livneh
:license: BSD
"""
import os
import random
import base64
from nose2 import session
from nose2.events import Plugin
from nose2.tests._common import FunctionalTestCase
DEFAULT_SEED_SIZE = 32 # Default size, in bytes, of seed value
def get_digest(size=DEFAULT_SEED_SIZE):
"""Encodes `size` random bytes into base64"""
seed = os.urandom(size)
return base64.b64encode(seed)
def set_digest(digest):
"""Decodes a base64-encoded array of bytes and uses its value to seed the
internal random number generator """
seed = base64.b64decode(digest)
random.seed(seed)
class Random(Plugin):
"""Capture / set random number generator state"""
configSection = 'random'
commandLineSwitch = ('R', 'random', 'Capture/set internal random number '
'generator state')
def __init__(self):
size = self.config.as_int('seed_size', default=DEFAULT_SEED_SIZE)
self.digest = get_digest(size)
self.addArgument(self.setSeedDigest, None, 'seed-digest',
'Seed random number generator with this digest')
def setSeedDigest(self, digest):
self.digest = digest
def startTest(self, event):
set_digest(self.digest)
def testOutcome(self, event):
event.metadata['digest'] = self.digest
def outcomeDetail(self, event):
event.extraDetail.append('Seed digest: {.digest}'.format(self))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment