Python pytest.mark() Examples

The following are 30 code examples of pytest.mark(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pytest , or try the search function .
Example #1
Source File: structures.py    From python-netsurv with MIT License 6 votes vote down vote up
def extract_from(cls, parameterset, force_tuple=False):
        """
        :param parameterset:
            a legacy style parameterset that may or may not be a tuple,
            and may or may not be wrapped into a mess of mark objects

        :param force_tuple:
            enforce tuple wrapping so single argument tuple values
            don't get decomposed and break tests
        """

        if isinstance(parameterset, cls):
            return parameterset
        if force_tuple:
            return cls.param(parameterset)
        else:
            return cls(parameterset, marks=[], id=None) 
Example #2
Source File: structures.py    From python-netsurv with MIT License 6 votes vote down vote up
def get_empty_parameterset_mark(config, argnames, func):
    from ..nodes import Collector

    requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
    if requested_mark in ("", None, "skip"):
        mark = MARK_GEN.skip
    elif requested_mark == "xfail":
        mark = MARK_GEN.xfail(run=False)
    elif requested_mark == "fail_at_collect":
        f_name = func.__name__
        _, lineno = getfslineno(func)
        raise Collector.CollectError(
            "Empty parameter set in '%s' at line %d" % (f_name, lineno + 1)
        )
    else:
        raise LookupError(requested_mark)
    fs, lineno = getfslineno(func)
    reason = "got empty parameter set %r, function %s at %s:%d" % (
        argnames,
        func.__name__,
        fs,
        lineno,
    )
    return mark(reason=reason) 
Example #3
Source File: common_pytest.py    From python-pytest-cases with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, mark):
        bound = get_parametrize_signature().bind(*mark.args, **mark.kwargs)
        try:
            remaining_kwargs = bound.arguments['kwargs']
        except KeyError:
            pass
        else:
            if len(remaining_kwargs) > 0:
                warn("parametrize kwargs not taken into account: %s. Please report it at"
                     " https://github.com/smarie/python-pytest-cases/issues" % remaining_kwargs)
        self.param_names = get_param_argnames_as_list(bound.arguments['argnames'])
        self.param_values = bound.arguments['argvalues']
        try:
            bound.apply_defaults()
            self.param_ids = bound.arguments['ids']
        except AttributeError:
            # can happen if signature is from funcsigs so we have to apply ourselves
            self.param_ids = bound.arguments.get('ids', None)


# -------- tools to get the parametrization mark whatever the pytest version 
Example #4
Source File: tree_construction.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def _getParserTests(self, treeName, treeAPIs):
        if treeAPIs is not None and "adapter" in treeAPIs:
            return
        for namespaceHTMLElements in (True, False):
            if namespaceHTMLElements:
                nodeid = "%s::parser::namespaced" % treeName
            else:
                nodeid = "%s::parser::void-namespace" % treeName
            item = ParserTest(nodeid,
                              self,
                              self.testdata,
                              treeAPIs["builder"] if treeAPIs is not None else None,
                              namespaceHTMLElements)
            item.add_marker(getattr(pytest.mark, treeName))
            item.add_marker(pytest.mark.parser)
            if namespaceHTMLElements:
                item.add_marker(pytest.mark.namespaced)
            if treeAPIs is None:
                item.add_marker(pytest.mark.skipif(True, reason="Treebuilder not loaded"))
            yield item 
Example #5
Source File: indirect_parametrize.py    From suds with GNU Lesser General Public License v3.0 6 votes vote down vote up
def indirect_parametrize(func, *args, **kwargs):
    """
    Decorator registering a custom parametrization function for a pytest test.

    This pytest.mark.indirect_parametrize() replacement allows the use of
    indirect parametrization functions taking no input parameters or, with
    pytest versions prior to 2.5.2, functions taking only keyword arguments.

    If a pytest.mark.indirect_parametrize() call is made with such an indirect
    parametrization function, it decorates the given function instead of
    storing and using it to decorate the intended function later on.

    """
    # In pytest versions prior to 2.5.2 pytest.mark.indirect_parametrize()
    # special handling occurs even when passing it additional keyword arguments
    # so we have to make sure we are passing it at least one additional
    # positional argument.
    def wrapper(func, *args, **kwargs):
        return func(*args, **kwargs)
    return pytest.mark.indirect_parametrize(wrapper, func, *args, **kwargs) 
Example #6
Source File: structures.py    From python-netsurv with MIT License 6 votes vote down vote up
def get_empty_parameterset_mark(config, argnames, func):
    from ..nodes import Collector

    requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
    if requested_mark in ("", None, "skip"):
        mark = MARK_GEN.skip
    elif requested_mark == "xfail":
        mark = MARK_GEN.xfail(run=False)
    elif requested_mark == "fail_at_collect":
        f_name = func.__name__
        _, lineno = getfslineno(func)
        raise Collector.CollectError(
            "Empty parameter set in '%s' at line %d" % (f_name, lineno + 1)
        )
    else:
        raise LookupError(requested_mark)
    fs, lineno = getfslineno(func)
    reason = "got empty parameter set %r, function %s at %s:%d" % (
        argnames,
        func.__name__,
        fs,
        lineno,
    )
    return mark(reason=reason) 
Example #7
Source File: structures.py    From python-netsurv with MIT License 6 votes vote down vote up
def extract_from(cls, parameterset, force_tuple=False):
        """
        :param parameterset:
            a legacy style parameterset that may or may not be a tuple,
            and may or may not be wrapped into a mess of mark objects

        :param force_tuple:
            enforce tuple wrapping so single argument tuple values
            don't get decomposed and break tests
        """

        if isinstance(parameterset, cls):
            return parameterset
        if force_tuple:
            return cls.param(parameterset)
        else:
            return cls(parameterset, marks=[], id=None) 
Example #8
Source File: helpers.py    From pycallnumber with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def generate_params(data, param_type):
    flattened = []
    for kind, param_sets in data.items():
        markers = [getattr(pytest.mark, m)
                   for m in param_sets.get('markers', [])]
        markers.append(getattr(pytest.mark, param_type))
        param_set = param_sets.get(param_type, [])
        for values in param_set:
            if not isinstance(values, tuple):
                values = (values,)
            inner_params_list = (kind,) + values
            flattened.append(pytest.param(*inner_params_list, marks=markers))
    return flattened 
Example #9
Source File: structures.py    From pytest with MIT License 5 votes vote down vote up
def kwargs(self) -> Mapping[str, Any]:
        """Alias for mark.kwargs."""
        return self.mark.kwargs 
Example #10
Source File: conftest.py    From telepresence with Apache License 2.0 5 votes vote down vote up
def _probe_parametrize(fixture_name):
    """
    Create a "parametrized" pytest fixture which will supply Probes (one for
    each coordinate in the cartesion space defined by METHODS and OPERATIONS)
    to test functions which use it.
    """
    param_values = [
        (method, op_getter())
        for method, op_getter in product(METHODS, OPERATION_GETTERS)
    ]
    return pytest.mark.parametrize(
        # Parameterize the probe parameter to decorated methods
        fixture_name,

        # The parameters are the elements of the cartesian product of methods,
        # operations.
        [
            pytest.param(value, marks=_get_marks(value))
            for value in param_values
        ],

        # Use the `name` of methods and operations to generate readable
        # parameterized test names.
        ids=lambda param: "{},{}".format(param[0].name, param[1].name),

        # Pass the parameters through the probe fixture to get the object
        # that's really passed to the decorated function.
        indirect=True,
    )


# Create a fixture supplying a Probe. 
Example #11
Source File: conftest.py    From telepresence with Apache License 2.0 5 votes vote down vote up
def _make_mark(name):
    """
    Turn a string into a pytest mark.
    """
    return getattr(pytest.mark, name.replace("-", "_")) 
Example #12
Source File: conftest.py    From quay with Apache License 2.0 5 votes vote down vote up
def pytest_collection_modifyitems(config, items):
    """
    This adds a pytest marker that consistently shards all collected tests.

    Use it like the following:
    $ py.test -m shard_1_of_3
    $ py.test -m shard_2_of_3
    $ py.test -m shard_3_of_3

    This code was originally adopted from the MIT-licensed ansible/molecule@9e7b79b:
    Copyright (c) 2015-2018 Cisco Systems, Inc.
    Copyright (c) 2018 Red Hat, Inc.
    """
    mark_opt = config.getoption("-m")
    if not mark_opt.startswith("shard_"):
        return

    desired_shard, _, total_shards = mark_opt[len("shard_") :].partition("_of_")
    if not total_shards or not desired_shard:
        return

    desired_shard = int(desired_shard)
    total_shards = int(total_shards)

    if not 0 < desired_shard <= total_shards:
        raise ValueError("desired_shard must be greater than 0 and not bigger than total_shards")

    for test_counter, item in enumerate(items):
        shard = test_counter % total_shards + 1
        marker = getattr(pytest.mark, "shard_{}_of_{}".format(shard, total_shards))
        item.add_marker(marker)

    print("Running sharded test group #{} out of {}".format(desired_shard, total_shards)) 
Example #13
Source File: conftest.py    From nbodykit with GNU General Public License v3.0 5 votes vote down vote up
def pytest_collection_modifyitems(items):
    """
    Automatically apply a ``pytest.mark`` decorator for each sample
    in the benchmarking samples.
    """
    for item in items:
        for name in BenchmarkingSample.samples:
            postfix = item.name.split(item.originalname)[-1] # this looks like [boss_like-10000]
            if name in postfix:
                item.add_marker(getattr(pytest.mark, name)) 
Example #14
Source File: structures.py    From pytest with MIT License 5 votes vote down vote up
def __getattr__(self, name: str) -> MarkDecorator:
        if name[0] == "_":
            raise AttributeError("Marker name must NOT start with underscore")

        if self._config is not None:
            # We store a set of markers as a performance optimisation - if a mark
            # name is in the set we definitely know it, but a mark may be known and
            # not in the set.  We therefore start by updating the set!
            if name not in self._markers:
                for line in self._config.getini("markers"):
                    # example lines: "skipif(condition): skip the given test if..."
                    # or "hypothesis: tests which use Hypothesis", so to get the
                    # marker name we split on both `:` and `(`.
                    marker = line.split(":")[0].split("(")[0].strip()
                    self._markers.add(marker)

            # If the name is not in the set of known marks after updating,
            # then it really is time to issue a warning or an error.
            if name not in self._markers:
                if self._config.option.strict_markers:
                    fail(
                        "{!r} not found in `markers` configuration option".format(name),
                        pytrace=False,
                    )

                # Raise a specific error for common misspellings of "parametrize".
                if name in ["parameterize", "parametrise", "parameterise"]:
                    __tracebackhide__ = True
                    fail("Unknown '{}' mark, did you mean 'parametrize'?".format(name))

                warnings.warn(
                    "Unknown pytest.mark.%s - is this a typo?  You can register "
                    "custom marks to avoid this warning - for details, see "
                    "https://docs.pytest.org/en/latest/mark.html" % name,
                    PytestUnknownMarkWarning,
                    2,
                )

        return MarkDecorator(Mark(name, (), {})) 
Example #15
Source File: structures.py    From pytest with MIT License 5 votes vote down vote up
def normalize_mark_list(mark_list: Iterable[Union[Mark, MarkDecorator]]) -> List[Mark]:
    """
    normalizes marker decorating helpers to mark objects

    :type mark_list: List[Union[Mark, Markdecorator]]
    :rtype: List[Mark]
    """
    extracted = [
        getattr(mark, "mark", mark) for mark in mark_list
    ]  # unpack MarkDecorator
    for mark in extracted:
        if not isinstance(mark, Mark):
            raise TypeError("got {!r} instead of Mark".format(mark))
    return [x for x in extracted if isinstance(x, Mark)] 
Example #16
Source File: structures.py    From pytest with MIT License 5 votes vote down vote up
def __call__(self, *args: object, **kwargs: object):  # noqa: F811
        """Call the MarkDecorator."""
        if args and not kwargs:
            func = args[0]
            is_class = inspect.isclass(func)
            if len(args) == 1 and (istestfunc(func) or is_class):
                store_mark(func, self.mark)
                return func
        return self.with_args(*args, **kwargs) 
Example #17
Source File: structures.py    From pytest with MIT License 5 votes vote down vote up
def with_args(self, *args: object, **kwargs: object) -> "MarkDecorator":
        """Return a MarkDecorator with extra arguments added.

        Unlike calling the MarkDecorator, with_args() can be used even
        if the sole argument is a callable/class.

        :return: MarkDecorator
        """
        mark = Mark(self.name, args, kwargs)
        return self.__class__(self.mark.combined_with(mark))

    # Type ignored because the overloads overlap with an incompatible
    # return type. Not much we can do about that. Thankfully mypy picks
    # the first match so it works out even if we break the rules. 
Example #18
Source File: structures.py    From pytest with MIT License 5 votes vote down vote up
def __repr__(self) -> str:
        return "<MarkDecorator {!r}>".format(self.mark) 
Example #19
Source File: conftest.py    From molecule with MIT License 5 votes vote down vote up
def pytest_collection_modifyitems(items, config):

    marker = config.getoption("-m")
    is_sharded = False
    shard_id = 0
    shards_num = 0
    if not marker.startswith("shard_"):
        return
    shard_id, _, shards_num = marker[6:].partition("_of_")
    if shards_num:
        shard_id = int(shard_id)
        shards_num = int(shards_num)
        is_sharded = True
    else:
        raise ValueError("shard_{}_of_{} marker is invalid")
    if not is_sharded:
        return
    if not (0 < shard_id <= shards_num):
        raise ValueError(
            "shard_id must be greater than 0 and not bigger than shards_num"
        )
    for test_counter, item in enumerate(items):
        cur_shard_id = test_counter % shards_num + 1
        marker = getattr(pytest.mark, "shard_{}_of_{}".format(cur_shard_id, shards_num))
        item.add_marker(marker)
    del marker
    print("Running sharded test group #{} out of {}".format(shard_id, shards_num)) 
Example #20
Source File: plugin.py    From pytest-testrail with MIT License 5 votes vote down vote up
def case(*ids):
        """
        Decorator to mark tests with testcase ids.

        ie. @pytestrail.case('C123', 'C12345')

        :return pytest.mark:
        """
        return pytest.mark.testrail(ids=ids) 
Example #21
Source File: plugin.py    From pytest-testrail with MIT License 5 votes vote down vote up
def defect(*defect_ids):
        """
                Decorator to mark defects with defect ids.

                ie. @pytestrail.defect('PF-513', 'BR-3255')

                :return pytest.mark:
                """
        return pytest.mark.testrail_defects(defect_ids=defect_ids) 
Example #22
Source File: plugin.py    From pytest-testrail with MIT License 5 votes vote down vote up
def testrail(*ids):
    """
    Decorator to mark tests with testcase ids.

    ie. @testrail('C123', 'C12345')

    :return pytest.mark:
    """
    deprecation_msg = ('pytest_testrail: the @testrail decorator is deprecated and will be removed. Please use the '
            '@pytestrail.case decorator instead.')
    warnings.warn(deprecation_msg, DeprecatedTestDecorator)
    return pytestrail.case(*ids) 
Example #23
Source File: plugin.py    From pytest-testrail with MIT License 5 votes vote down vote up
def pytest_collection_modifyitems(self, session, config, items):
        items_with_tr_keys = get_testrail_keys(items)
        tr_keys = [case_id for item in items_with_tr_keys for case_id in item[1]]

        if self.testplan_id and self.is_testplan_available():
            self.testrun_id = 0
        elif self.testrun_id and self.is_testrun_available():
            self.testplan_id = 0
            if self.skip_missing:
                tests_list = [
                    test.get('case_id') for test in self.get_tests(self.testrun_id)
                ]
                for item, case_id in items_with_tr_keys:
                    if not set(case_id).intersection(set(tests_list)):
                        mark = pytest.mark.skip('Test is not present in testrun.')
                        item.add_marker(mark)
        else:
            if self.testrun_name is None:
                self.testrun_name = testrun_name()

            self.create_test_run(
                self.assign_user_id,
                self.project_id,
                self.suite_id,
                self.include_all,
                self.testrun_name,
                tr_keys,
                self.milestone_id,
                self.testrun_description
            ) 
Example #24
Source File: tree_construction.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def _getTreeWalkerTests(self, treeName, treeAPIs):
        nodeid = "%s::treewalker" % treeName
        item = TreeWalkerTest(nodeid,
                              self,
                              self.testdata,
                              treeAPIs)
        item.add_marker(getattr(pytest.mark, treeName))
        item.add_marker(pytest.mark.treewalker)
        if treeAPIs is None:
            item.add_marker(pytest.mark.skipif(True, reason="Treebuilder not loaded"))
        yield item 
Example #25
Source File: indirect_parametrize.py    From suds with GNU Lesser General Public License v3.0 5 votes vote down vote up
def pytest_configure(config):
    """Describe the new pytest marker in the --markers output."""
    config.addinivalue_line("markers",
        "indirect_parametrize(function, argnames, argvalues): similar to the "
        "builtin pytest.mark.parametrize implementation, except that it takes "
        "an indirect parametrization function as an additional initial "
        "positional argument. All the other parameters are forwarded to the "
        "indirect parametrization function which then returns the actual "
        "metafunc.parametrize() parameters (standard Python positional "
        "argument list and keyword argument dictionary) based on the received "
        "input data. For more detailed information see the "
        "indirect_parametrize pytest plugin implementation module.")
    """pytest hook publishing references in the toplevel pytest namespace."""
    pytest.indirect_parametrize = indirect_parametrize 
Example #26
Source File: indirect_parametrize.py    From suds with GNU Lesser General Public License v3.0 5 votes vote down vote up
def pytest_generate_tests(metafunc):
    """pytest hook called for all detected test functions."""
    mark = metafunc.definition.get_closest_marker('indirect_parametrize')
    if not mark:
        return
    args, kwargs = mark.args[0](*mark.args[1:], **mark.kwargs)
    metafunc.parametrize(*args, **kwargs) 
Example #27
Source File: helper.py    From allure-python with Apache License 2.0 5 votes vote down vote up
def decorate_as_title(self, test_title):
        allure_title = getattr(pytest.mark, ALLURE_DISPLAY_NAME_MARK)
        return allure_title(test_title) 
Example #28
Source File: helper.py    From allure-python with Apache License 2.0 5 votes vote down vote up
def decorate_as_description(self, test_description):
        allure_description = getattr(pytest.mark, ALLURE_DESCRIPTION_MARK)
        return allure_description(test_description) 
Example #29
Source File: helper.py    From allure-python with Apache License 2.0 5 votes vote down vote up
def decorate_as_description_html(self, test_description_html):
        allure_description_html = getattr(pytest.mark, ALLURE_DESCRIPTION_HTML_MARK)
        return allure_description_html(test_description_html) 
Example #30
Source File: helper.py    From allure-python with Apache License 2.0 5 votes vote down vote up
def decorate_as_label(self, label_type, labels):
        allure_label = getattr(pytest.mark, ALLURE_LABEL_MARK)
        return allure_label(*labels, label_type=label_type)