Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ERF: use py.test 2 ini-file configuration.
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
1 parent
fcb9147
commit 9ea64fb
Showing
4 changed files
with
63 additions
and
161 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[pytest] | ||
norecursedirs = build .git cover html dist | ||
minversion = 2.1.2 |