Skip to content

Commit

Permalink
ERF: use py.test 2 ini-file configuration.
Browse files Browse the repository at this point in the history
we can now check for py.test version, and get finally rid of the
redundant conftest.py files. we now have only one conftest.py file
for bimdp and mdp.
  • Loading branch information
otizonaizit committed Oct 11, 2011
1 parent fcb9147 commit 9ea64fb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 161 deletions.
76 changes: 0 additions & 76 deletions bimdp/test/conftest.py

This file was deleted.

69 changes: 60 additions & 9 deletions conftest.py
@@ -1,13 +1,64 @@
collect_ignore = ['build', 'cover', 'html',
]
# global hooks for py.test
import tempfile
import os
import shutil
import glob
import mdp
import py.test

def _have_option(parser, optionname):
return any(optionname == option.get_opt_string()
for option in parser._anonymous.options)
_err_str = """
IMPORTANT: some tests use random numbers. This could
occasionally lead to failures due to numerical degeneracies.
To rule this out, please run the tests more than once.
If you get reproducible failures please report a bug!
"""

def pytest_configure(config):
seed = config.getvalue("seed")
# if seed was not set by the user, we set one now
if seed is None or seed == ('NO', 'DEFAULT'):
config.option.seed = int(mdp.numx_rand.randint(2**31-1))

def pytest_unconfigure(config):
# remove garbage created during tests
# note that usage of TemporaryDirectory is not enough to assure
# that all garbage is removed, expacially because we use subprocesses
shutil.rmtree(py.test.mdp_tempdirname, ignore_errors=True)
# if pp was monkey-patched, remove any stale pp4mdp directories
if mdp.config.pp_monkeypatch_dirname:
monkey_dirs = os.path.join(mdp.config.pp_monkeypatch_dirname,
mdp.parallel.pp_support.TEMPDIR_PREFIX)
[shutil.rmtree(d, ignore_errors=True) for d in glob.glob(monkey_dirs+'*')]

def pytest_runtest_setup(item):
# set random seed before running each test
# so that a failure in a test can be reproduced just running
# that particular test. if this was not done, you would need
# to run the whole test suite again
mdp.numx_rand.seed(item.config.option.seed)

def pytest_addoption(parser):
"""Add random seed option to py.test
"""Add random seed option to py.test.
"""
if not _have_option(parser, '--seed'):
parser.addoption('--seed', dest='seed', type=int, action='store',
help='set random seed')
parser.addoption('--seed', dest='seed', type=int, action='store',
help='set random seed')

def pytest_report_header(config):
# report the random seed before and after running the tests
return '%s\nRandom Seed: %d\n' % (mdp.config.info(), config.option.seed)

def pytest_terminal_summary(terminalreporter):
# add a note about error due to randomness only if an error or a failure
# occured
t = terminalreporter
t.write_sep("=", "NOTE")
t.write_line("%s\nRandom Seed: %d" % (mdp.config.info(),
t.config.option.seed))
if 'failed' in t.stats or 'error' in t.stats:
t.write_line(_err_str)

def pytest_namespace():
# get temporary directory to put temporary files
# will be deleted at the end of the test run
dirname = tempfile.mkdtemp(suffix='.tmp', prefix='MDPtestdir_')
return dict(mdp_tempdirname=dirname)
76 changes: 0 additions & 76 deletions mdp/test/conftest.py

This file was deleted.

3 changes: 3 additions & 0 deletions pytest.ini
@@ -0,0 +1,3 @@
[pytest]
norecursedirs = build .git cover html dist
minversion = 2.1.2

0 comments on commit 9ea64fb

Please sign in to comment.