Beispiel #1
0
def pytest_cmdline_main(config):
    import _pytest.config
    genscript = config.getvalue("genscript")
    if genscript:
        tw = _pytest.config.create_terminal_writer(config)
        tw.line("WARNING: usage of genscript is deprecated.", red=True)
        deps = ['py', '_pytest', 'pytest']  # pluggy is vendored
        if sys.version_info < (2, 7):
            deps.append("argparse")
            tw.line("generated script will run on python2.6-python3.3++")
        else:
            tw.line(
                "WARNING: generated script will not run on python2.6 "
                "due to 'argparse' dependency. Use python2.6 "
                "to generate a python2.6 compatible script",
                red=True)
        script = generate_script(
            'import pytest; raise SystemExit(pytest.cmdline.main())',
            deps,
        )
        genscript = py.path.local(genscript)
        genscript.write(script)
        tw.line("generated pytest standalone script: %s" % genscript,
                bold=True)
        return 0
Beispiel #2
0
def _showfixtures_main(config, session):
    import _pytest.config

    session.perform_collect()
    curdir = py.path.local()
    tw = _pytest.config.create_terminal_writer(config)
    verbose = config.getvalue("verbose")

    fm = session._fixturemanager

    available = []
    seen = set()

    for argname, fixturedefs in fm._arg2fixturedefs.items():
        assert fixturedefs is not None
        if not fixturedefs:
            continue
        for fixturedef in fixturedefs:
            loc = getlocation(fixturedef.func, curdir)
            if (fixturedef.argname, loc) in seen:
                continue
            seen.add((fixturedef.argname, loc))
            available.append(
                (
                    len(fixturedef.baseid),
                    fixturedef.func.__module__,
                    curdir.bestrelpath(loc),
                    fixturedef.argname,
                    fixturedef,
                )
            )

    available.sort()
    currentmodule = None
    for baseid, module, bestrel, argname, fixturedef in available:
        if currentmodule != module:
            if not module.startswith("_pytest."):
                tw.line()
                tw.sep("-", "fixtures defined from %s" % (module,))
                currentmodule = module
        if verbose <= 0 and argname[0] == "_":
            continue
        if verbose > 0:
            funcargspec = "%s -- %s" % (argname, bestrel)
        else:
            funcargspec = argname
        tw.line(funcargspec, green=True)
        loc = getlocation(fixturedef.func, curdir)
        doc = fixturedef.func.__doc__ or ""
        if doc:
            write_docstring(tw, doc)
        else:
            tw.line("    %s: no docstring available" % (loc,), red=True)
Beispiel #3
0
def _showfixtures_main(config, session):
    import _pytest.config

    session.perform_collect()
    curdir = py.path.local()
    tw = _pytest.config.create_terminal_writer(config)
    verbose = config.getvalue("verbose")

    fm = session._fixturemanager

    available = []
    seen = set()

    for argname, fixturedefs in fm._arg2fixturedefs.items():
        assert fixturedefs is not None
        if not fixturedefs:
            continue
        for fixturedef in fixturedefs:
            loc = getlocation(fixturedef.func, curdir)
            if (fixturedef.argname, loc) in seen:
                continue
            seen.add((fixturedef.argname, loc))
            available.append(
                (
                    len(fixturedef.baseid),
                    fixturedef.func.__module__,
                    curdir.bestrelpath(loc),
                    fixturedef.argname,
                    fixturedef,
                )
            )

    available.sort()
    currentmodule = None
    for baseid, module, bestrel, argname, fixturedef in available:
        if currentmodule != module:
            if not module.startswith("_pytest."):
                tw.line()
                tw.sep("-", "fixtures defined from %s" % (module,))
                currentmodule = module
        if verbose <= 0 and argname[0] == "_":
            continue
        if verbose > 0:
            funcargspec = "%s -- %s" % (argname, bestrel)
        else:
            funcargspec = argname
        tw.line(funcargspec, green=True)
        loc = getlocation(fixturedef.func, curdir)
        doc = fixturedef.func.__doc__ or ""
        if doc:
            write_docstring(tw, doc)
        else:
            tw.line("    %s: no docstring available" % (loc,), red=True)
Beispiel #4
0
def _show_fixtures_per_test(config, session):
    import _pytest.config
    session.perform_collect()
    curdir = py.path.local()
    tw = _pytest.config.create_terminal_writer(config)
    verbose = config.getvalue("verbose")

    def get_best_rel(func):
        loc = getlocation(func, curdir)
        return curdir.bestrelpath(loc)

    def write_fixture(fixture_def):
        argname = fixture_def.argname

        if verbose <= 0 and argname.startswith("_"):
            return
        if verbose > 0:
            bestrel = get_best_rel(fixture_def.func)
            funcargspec = "{0} -- {1}".format(argname, bestrel)
        else:
            funcargspec = argname
        tw.line(funcargspec, green=True)

        fixture_doc = fixture_def.func.__doc__

        if fixture_doc:
            write_docstring(tw, fixture_doc)
        else:
            tw.line('    no docstring available', red=True)

    def write_item(item):
        name2fixturedefs = item._fixtureinfo.name2fixturedefs

        if not name2fixturedefs:
            # The given test item does not use any fixtures
            return
        bestrel = get_best_rel(item.function)

        tw.line()
        tw.sep('-', 'fixtures used by {0}'.format(item.name))
        tw.sep('-', '({0})'.format(bestrel))
        for argname, fixture_defs in sorted(name2fixturedefs.items()):
            assert fixture_defs is not None
            if not fixture_defs:
                continue
            # The last fixture def item in the list is expected
            # to be the one used by the test item
            write_fixture(fixture_defs[-1])

    for item in session.items:
        write_item(item)
Beispiel #5
0
def _show_fixtures_per_test(config, session):
    import _pytest.config
    session.perform_collect()
    curdir = py.path.local()
    tw = _pytest.config.create_terminal_writer(config)
    verbose = config.getvalue("verbose")

    def get_best_rel(func):
        loc = getlocation(func, curdir)
        return curdir.bestrelpath(loc)

    def write_fixture(fixture_def):
        argname = fixture_def.argname

        if verbose <= 0 and argname.startswith("_"):
            return
        if verbose > 0:
            bestrel = get_best_rel(fixture_def.func)
            funcargspec = "{0} -- {1}".format(argname, bestrel)
        else:
            funcargspec = argname
        tw.line(funcargspec, green=True)

        fixture_doc = fixture_def.func.__doc__

        if fixture_doc:
            write_docstring(tw, fixture_doc)
        else:
            tw.line('    no docstring available', red=True)

    def write_item(item):
        name2fixturedefs = item._fixtureinfo.name2fixturedefs

        if not name2fixturedefs:
            # The given test item does not use any fixtures
            return
        bestrel = get_best_rel(item.function)

        tw.line()
        tw.sep('-', 'fixtures used by {0}'.format(item.name))
        tw.sep('-', '({0})'.format(bestrel))
        for argname, fixture_defs in sorted(name2fixturedefs.items()):
            assert fixture_defs is not None
            if not fixture_defs:
                continue
            # The last fixture def item in the list is expected
            # to be the one used by the test item
            write_fixture(fixture_defs[-1])

    for item in session.items:
        write_item(item)
Beispiel #6
0
def _show_fixtures_per_test(config, session):
    import _pytest.config

    session.perform_collect()
    curdir = py.path.local()
    tw = _pytest.config.create_terminal_writer(config)
    verbose = config.getvalue("verbose")

    def get_best_relpath(func):
        loc = getlocation(func, curdir)
        return curdir.bestrelpath(loc)

    def write_fixture(fixture_def):
        argname = fixture_def.argname
        if verbose <= 0 and argname.startswith("_"):
            return
        if verbose > 0:
            bestrel = get_best_relpath(fixture_def.func)
            funcargspec = "{} -- {}".format(argname, bestrel)
        else:
            funcargspec = argname
        tw.line(funcargspec, green=True)
        fixture_doc = fixture_def.func.__doc__
        if fixture_doc:
            write_docstring(tw, fixture_doc)
        else:
            tw.line("    no docstring available", red=True)

    def write_item(item):
        try:
            info = item._fixtureinfo
        except AttributeError:
            # doctests items have no _fixtureinfo attribute
            return
        if not info.name2fixturedefs:
            # this test item does not use any fixtures
            return
        tw.line()
        tw.sep("-", "fixtures used by {}".format(item.name))
        tw.sep("-", "({})".format(get_best_relpath(item.function)))
        # dict key not used in loop but needed for sorting
        for _, fixturedefs in sorted(info.name2fixturedefs.items()):
            assert fixturedefs is not None
            if not fixturedefs:
                continue
            # last item is expected to be the one used by the test item
            write_fixture(fixturedefs[-1])

    for session_item in session.items:
        write_item(session_item)
Beispiel #7
0
def _show_fixtures_per_test(config, session):
    import _pytest.config

    session.perform_collect()
    curdir = py.path.local()
    tw = _pytest.config.create_terminal_writer(config)
    verbose = config.getvalue("verbose")

    def get_best_relpath(func):
        loc = getlocation(func, curdir)
        return curdir.bestrelpath(loc)

    def write_fixture(fixture_def):
        argname = fixture_def.argname
        if verbose <= 0 and argname.startswith("_"):
            return
        if verbose > 0:
            bestrel = get_best_relpath(fixture_def.func)
            funcargspec = "{} -- {}".format(argname, bestrel)
        else:
            funcargspec = argname
        tw.line(funcargspec, green=True)
        fixture_doc = fixture_def.func.__doc__
        if fixture_doc:
            write_docstring(tw, fixture_doc)
        else:
            tw.line("    no docstring available", red=True)

    def write_item(item):
        try:
            info = item._fixtureinfo
        except AttributeError:
            # doctests items have no _fixtureinfo attribute
            return
        if not info.name2fixturedefs:
            # this test item does not use any fixtures
            return
        tw.line()
        tw.sep("-", "fixtures used by {}".format(item.name))
        tw.sep("-", "({})".format(get_best_relpath(item.function)))
        # dict key not used in loop but needed for sorting
        for _, fixturedefs in sorted(info.name2fixturedefs.items()):
            assert fixturedefs is not None
            if not fixturedefs:
                continue
            # last item is expected to be the one used by the test item
            write_fixture(fixturedefs[-1])

    for session_item in session.items:
        write_item(session_item)
Beispiel #8
0
def show_dead_fixtures(config, session):
    session.perform_collect()
    tw = _pytest.config.create_terminal_writer(config)
    show_fixture_doc = config.getvalue('show_fixture_doc')

    used_fixtures = get_used_fixturesdefs(session)
    available_fixtures = get_fixtures(session)

    unused_fixtures = [fixture for fixture in available_fixtures
                       if fixture.fixturedef not in used_fixtures]

    tw.line()
    if unused_fixtures:
        tw.line(UNUSED_FIXTURES_FOUND_HEADLINE, red=True)
        write_fixtures(tw, unused_fixtures, show_fixture_doc)
    else:
        tw.line(UNUSED_FIXTURES_NOT_FOUND_HEADLINE, green=True)
    return unused_fixtures
Beispiel #9
0
def show_dead_fixtures(config, session):
    session.perform_collect()
    tw = _pytest.config.create_terminal_writer(config)
    verbose = config.getvalue('verbose')

    used_fixtures = get_used_fixturesdefs(session)
    available_fixtures = get_fixtures(session)

    unused_fixtures = [fixture for fixture in available_fixtures
                       if fixture.fixturedef not in used_fixtures]

    tw.line()
    if unused_fixtures:
        tw.line(
            'Hey there, I believe the following fixture(s) are not being used:',
            red=True
        )
        write_fixtures(tw, unused_fixtures, verbose)
    else:
        tw.line('Cool, every declared fixture are being used.', green=True)
Beispiel #10
0
def pytest_cmdline_main(config):
    import _pytest.config
    genscript = config.getvalue("genscript")
    if genscript:
        tw = _pytest.config.create_terminal_writer(config)
        deps =  ['py', 'pluggy', '_pytest', 'pytest']
        if sys.version_info < (2,7):
            deps.append("argparse")
            tw.line("generated script will run on python2.6-python3.3++")
        else:
            tw.line("WARNING: generated script will not run on python2.6 "
                    "due to 'argparse' dependency. Use python2.6 "
                    "to generate a python2.6 compatible script", red=True)
        script = generate_script(
            'import pytest; raise SystemExit(pytest.cmdline.main())',
            deps,
        )
        genscript = py.path.local(genscript)
        genscript.write(script)
        tw.line("generated pytest standalone script: %s" % genscript,
                bold=True)
        return 0