Ejemplo n.º 1
0
def pytest_collection_modifyitems(session, config, items):
    final_items = []
    for item in items:
        assert isinstance(item, Function)
        marker = item.get_marker('timeit')
        if not marker:
            final_items.append(item)
            continue
        if marker.args:
            raise MarkerError(
                "'timeit' mark doesn't accept positional arguments (on '{}')".
                format(item.nodeid))
        number = marker.kwargs.get('number', marker.kwargs.get('n'))
        if number is None:
            raise MarkerError(
                "'timeit' mark on '{}' needs 'n'/'number' kwarg".format(
                    item.nodeid))
        reps = marker.kwargs.get('repetitions',
                                 marker.kwargs.get('r', DEFAULT_REPS))
        mode = marker.kwargs.get('mode', DEFAULT_MODE)

        original_name = item.name
        if reps > 1:
            item.name = "{}[n={}, r={}]".format(item.name, number, reps)
        else:
            item.name = "{}[n={}]".format(item.name, number)

        if mode == 'safe':
            if reps > 1:
                template = "{name}[n={n}/{number}, r={r}/{reps}]"
            else:
                template = "{name}[n={n}/{number}]"
        elif mode == 'fast':
            if reps > 1:
                template = "{name}[n={number}, r={r}/{reps}]"
            else:
                template = "{name}[n={number}]"
        else:
            raise MarkerError(
                "'timeit' mark 'mode' may only be one of 'safe' or 'fast' (on '{}')"
                .format(item.nodeid))

        for r in range(reps):
            if mode == 'safe':
                for n in range(number):
                    final_items.append(
                        _add_function(item, original_name, mode, n, number, r,
                                      reps, template))
            elif mode == 'fast':
                final_items.append(
                    _add_function(item, original_name, mode, 0, number, r,
                                  reps, template))

    items[:] = final_items
Ejemplo n.º 2
0
def pytest_generate_tests(metafunc):
    # those alternative spellings are common - raise a specific error to alert
    # the user
    alt_spellings = ["parameterize", "parametrise", "parameterise"]
    for attr in alt_spellings:
        if hasattr(metafunc.function, attr):
            msg = "{0} has '{1}', spelling should be 'parametrize'"
            raise MarkerError(msg.format(metafunc.function.__name__, attr))
    for marker in metafunc.definition.iter_markers(name="parametrize"):
        metafunc.parametrize(*marker.args, **marker.kwargs)
Ejemplo n.º 3
0
def pytest_generate_tests(metafunc):
    # those alternative spellings are common - raise a specific error to alert
    # the user
    alt_spellings = ['parameterize', 'parametrise', 'parameterise']
    for attr in alt_spellings:
        if hasattr(metafunc.function, attr):
            msg = "{0} has '{1}', spelling should be 'parametrize'"
            raise MarkerError(msg.format(metafunc.function.__name__, attr))
    try:
        markers = metafunc.function.parametrize
    except AttributeError:
        return
    for marker in markers:
        metafunc.parametrize(*marker.args, **marker.kwargs)