File: conftest.py

package info (click to toggle)
mdp 3.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,500 kB
  • sloc: python: 25,503; makefile: 9; sh: 8
file content (69 lines) | stat: -rw-r--r-- 2,406 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# global hooks for pytest
import tempfile
import os
import shutil
import glob
import mdp
import pytest

_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))

    # get temp dir
    pytest.mdp_tempdirname = tempfile.mkdtemp(
        suffix='.tmp', prefix='MDPtestdir_')


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(pytest.mdp_tempdirname, ignore_errors=True)
    # if pp was monkey-patched, remove any stale pp4mdp directories
    if hasattr(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 pytest.
    """
    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)