def with_seed(seed=None):
def test_helper(orig_test):<EOL><INDENT>@make_decorator(orig_test)<EOL>def test_new(*args, **kwargs):<EOL><INDENT>test_count = int(os.getenv('<STR_LIT>', '<STR_LIT:1>'))<EOL>env_seed_str = os.getenv('<STR_LIT>')<EOL>for i in range(test_count):<EOL><INDENT>if seed is not None:<EOL><INDENT>this_test_seed = seed<EOL>log_level = logging.INFO<EOL><DEDENT>elif env_seed_str is not None:<EOL><INDENT>this_test_seed = int(env_seed_str)<EOL>log_level = logging.INFO<EOL><DEDENT>else:<EOL><INDENT>this_test_seed = np.random.randint(<NUM_LIT:0>, np.iinfo(np.int32).max)<EOL>log_level = logging.DEBUG<EOL><DEDENT>post_test_state = np.random.get_state()<EOL>np.random.seed(this_test_seed)<EOL>mx.random.seed(this_test_seed)<EOL>random.seed(this_test_seed)<EOL>logger = default_logger()<EOL>test_count_msg = '<STR_LIT>'.format(i+<NUM_LIT:1>,test_count) if test_count > <NUM_LIT:1> else '<STR_LIT>'<EOL>test_msg = ('<STR_LIT>'<EOL>'<STR_LIT>').format(test_count_msg, this_test_seed)<EOL>logger.log(log_level, test_msg)<EOL>try:<EOL><INDENT>orig_test(*args, **kwargs)<EOL><DEDENT>except:<EOL><INDENT>if log_level < logging.INFO:<EOL><INDENT>logger.info(test_msg)<EOL><DEDENT>raise<EOL><DEDENT>finally:<EOL><INDENT>np.random.set_state(post_test_state)<EOL><DEDENT><DEDENT><DEDENT>return test_new<EOL><DEDENT>return test_helper<EOL>
A decorator for nosetests test functions that manages rng seeds.

Parameters
----------

seed : the seed to pass to np.random and mx.random


This tests decorator sets the np, mx and python random seeds identically
prior to each test, then outputs those seeds if the test fails or
if the test requires a fixed seed (as a reminder to make the test
more robust against random data).

@with_seed()
def test_ok_with_random_data():
    ...

@with_seed(1234)
def test_not_ok_with_random_data():
    ...

Use of the @with_seed() decorator for all tests creates
tests isolation and reproducability of failures.  When a
test fails, the decorator outputs the seed used.  The user
can then set the environment variable MXNET_TEST_SEED to
the value reported, then rerun the test with:

    nosetests --verbose -s <test_module_name.py>:<failing_test>

To run a test repeatedly, set MXNET_TEST_COUNT=<NNN> in the environment.
To see the seeds of even the passing tests, add '--logging-level=DEBUG' to nosetests.