Exemplo n.º 1
0
def test(show=False, coverage=False):
    """Test all code examples in docstrings using pytest."""
    try:
        import pytest
    except ImportError:
        raise ImportError("pytest is required to run test suite. " + \
                          "Try 'sudo pip install pytest'.")

    from matplotlib import pyplot as plt
    from pygimli.utils import opt_import
    pc = opt_import("pytest_cov", "create a coverage report")

    old_backend = plt.get_backend()
    if not show:
        plt.switch_backend("Agg")
    cwd = __path__[0]
    cfg = os.path.join(cwd, '../tests/setup.cfg')
    cmd = ""
    if os.path.exists(cfg):
        cmd += "-c %s " % cfg
    if pc and coverage:
        cmd += "--cov pygimli --cov-report coveralls --cov-report html " + \
               "--cov-config %s " % cfg.replace("setup.cfg", ".coveragerc")
    cmd += "%s" % cwd
    try:
        pytest.main(cmd)
    finally:
        plt.switch_backend(old_backend)
Exemplo n.º 2
0
def test(show=False, onlydoctests=False, coverage=False, htmlreport=False):
    """Run docstring examples and additional tests.

    Parameters
    ----------
    show : boolean, optional
        Show matplotlib windows during test run. They will be closed
        automatically.
    onlydoctests : boolean, optional
        Run test files in ../tests as well.
    coverage : boolean, optional
        Create a coverage report. Requires the pytest-cov plugin.
    htmlreport : str, optional
        Filename for HTML report such as www.pygimli.org/build_tests.html.
        Requires pytest-html plugin.
    """
    try:
        import pytest
    except ImportError:
        raise ImportError("pytest is required to run test suite. " + \
                          "Try 'sudo pip install pytest'.")

    from matplotlib import pyplot as plt
    from pygimli.utils import opt_import
    pc = opt_import("pytest_cov", "create a code coverage report")
    ph = opt_import("pytest_html", "create a html report")

    old_backend = plt.get_backend()
    if not show:
        plt.switch_backend("Agg")
    cwd = os.path.realpath(__path__[0])
    cfg = os.path.join(cwd, "../tests/setup.cfg")
    cmd = ""
    if os.path.exists(cfg):
        cmd += "-c %s " % cfg
    if pc and coverage:
        cmd += "--cov pygimli --cov-report term " + \
               "--cov-config %s " % cfg.replace("setup.cfg", ".coveragerc")
    if ph and htmlreport:
        cmd += "--html %s " % htmlreport
    cmd += "%s " % cwd
    if not onlydoctests and os.path.exists(cfg):
        cmd += os.path.join(cwd, "../tests")

    exitcode = pytest.main(cmd)
    plt.switch_backend(old_backend)
    plt.close('all')
    sys.exit(exitcode)