Exemple #1
0
def pytest_pycollect_makeitem(collector, name, obj):
    """A pytest hook to collect asyncio coroutines."""
    if collector.funcnamefilter(name) and _is_coroutine(obj):
        item = pytest.Function(name, parent=collector)

        # Due to how pytest test collection works, module-level pytestmarks
        # are applied after the collection step. Since this is the collection
        # step, we look ourselves.
        transfer_markers(obj, item.cls, item.module)
        item = pytest.Function(name, parent=collector)  # To reload keywords.

        if 'asyncio' in item.keywords:
            return list(collector._genfunctions(name, obj))
Exemple #2
0
 def test_function_equality(self, testdir, tmpdir):
     from _pytest.python import FixtureManager
     config = testdir.parseconfigure()
     session = testdir.Session(config)
     session._fixturemanager = FixtureManager(session)
     def func1():
         pass
     def func2():
         pass
     f1 = pytest.Function(name="name", parent=session, config=config,
             args=(1,), callobj=func1)
     assert f1 == f1
     f2 = pytest.Function(name="name",config=config,
             callobj=func2, parent=session)
     assert f1 != f2
Exemple #3
0
def pytest_pycollect_makeitem(collector, name, obj):
    """
    Convert test generator into list of function tests so that pytest doesn't
    complain about deprecated yield tests.

    Note that unlike nose, the tests are generated and saved instead of run
    immediately.  This means that any dynamic context, such as a for-loop
    variable, must be captured by wrapping the yield result in a function call.

    For example::

        for value in 1, 2, 3:
            for test in test_cases:
                yield test, value

    will need to be changed to::

        def build_test(test, value):
            return test, value
        for value in 1, 2, 3:
            for test in test_cases:
                yield build_test(test, value)

    This allows the context (test and value) to be captured by lexical closure
    in build_test. See https://stackoverflow.com/a/233835/6195051.
    """
    if collector.istestfunction(obj, name) and is_generator(obj):
        tests = []
        for number, yielded in enumerate(obj()):
            index, call, args = split_yielded_test(yielded, number)
            test = pytest.Function(name+index, collector, args=args, callobj=call)
            tests.append(test)
        return tests
Exemple #4
0
def pytest_pycollect_makeitem(collector, name, obj):
    if collector.funcnamefilter(name):
        if not callable(obj):
            return
        item = pytest.Function(name, parent=collector)
        if 'run_loop' in item.keywords:
            return list(collector._genfunctions(name, obj))
Exemple #5
0
def pytest_pycollect_makeitem(collector, name, obj):
    if collector.funcnamefilter(name):
        if not callable(obj):
            return
        item = pytest.Function(name, parent=collector)
        if 'run_loop' in item.keywords:
            # TODO: re-wrap with asyncio.coroutine if not native coroutine
            return list(collector._genfunctions(name, obj))
Exemple #6
0
    def make_function(testdir, **kwargs):
        from _pytest.fixtures import FixtureManager

        config = testdir.parseconfigure()
        session = testdir.Session(config)
        session._fixturemanager = FixtureManager(session)

        return pytest.Function(config=config, parent=session, **kwargs)
Exemple #7
0
def pytest_pycollect_makeitem(collector, name, obj):
    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif (inspect.isfunction(obj) and isinstance(collector, pytest.Instance)
          and plugin_base.want_method(collector.cls, obj)):
        return pytest.Function(name, parent=collector)
    else:
        return []
Exemple #8
0
def pytest_pycollect_makeitem(collector, name, obj):
    if collector.funcnamefilter(name) and inspect.isgeneratorfunction(obj):
        if _PYTEST_VERSION >= pkg_resources.parse_version("5.4.0"):
            item = pytest.Function.from_parent(collector, name=name)
        else:
            item = pytest.Function(name, parent=collector)
        if 'gen_test' in item.keywords:
            return list(collector._genfunctions(name, obj))
Exemple #9
0
 def collect(self):
     test_cases = yaml.load(self.fspath.open())
     for case in test_cases:
         module = py.path.local(str(self.fspath).replace(".yml", ".py"))
         yield pytest.Function(
             name=case['mapset'],
             # parent=pytest.Module(fspath=os.path.abspath(__file__), parent=self),
             parent=pytest.Module(fspath=module, parent=self),
             callobj=functools.partial(estimap_test_runner, case))
Exemple #10
0
def pytest_pycollect_makeitem(collector, name, obj):

    if inspect.isclass(obj) and plugin_base.want_class(obj):
        return pytest.Class(name, parent=collector)
    elif inspect.isfunction(obj) and \
            name.startswith("test_") and \
            isinstance(collector, pytest.Instance):
        return pytest.Function(name, parent=collector)
    else:
        return []
Exemple #11
0
def pytest_pycollect_makeitem(collector, name, obj):
    if collector.funcnamefilter(name) and inspect.isgeneratorfunction(obj):
        item = pytest.Function(name, parent=collector)
        if 'gen_test' in item.keywords:
            return list(collector._genfunctions(name, obj))
Exemple #12
0
def pytest_pycollect_makeitem(collector, name, obj):
    if collector.funcnamefilter(name) and inspect.iscoroutinefunction(obj):
        item = pytest.Function(name, parent=collector)
        if MARKER in item.keywords:
            return list(collector._genfunctions(name, obj))
Exemple #13
0
 def _dummy_item(self, item, context_param=""):
     # TODO support class methods
     def dummy(request):
         pass
     fixtureinfo = self.session._fixturemanager.getfixtureinfo(item, dummy, cls=None)
     if hasattr(pytest.Function, "from_parent"):
         func = pytest.Function.from_parent(
             item,
             name="dummy",
             callobj=dummy,
             fixtureinfo=fixtureinfo,
         )
     else:  # TODO remove with pytest >= 5.4
         func = pytest.Function(
             name="dummy",
             parent=item,
             callobj=dummy,
             fixtureinfo=fixtureinfo,
         )
     if hasattr(FunctionDefinition, "from_parent"):
         definition = FunctionDefinition.from_parent(
             item,
             name="dummy",
             callobj=dummy,
         )
     else:  # TODO remove with pytest >= 5.4
         definition = FunctionDefinition(
             name="dummy",
             parent=item,
             callobj=dummy,
         )
     metafunc = Metafunc(
         definition,
         fixtureinfo,
         self.config,
         cls=None,
         module=item.getparent(pytest.Module).obj,
     )
     func.callspec = CallSpec2(metafunc)
     self.config.hook.pytest_generate_tests(metafunc=metafunc)
     if context_param != "":
         for callspec in metafunc._calls:
             if callspec.id == context_param:
                 if hasattr(pytest.Function, "from_parent"):
                     return pytest.Function.from_parent(
                         item,
                         name=f"{func.name}[{context_param}]",
                         callspec=callspec,
                         callobj=dummy,
                         fixtureinfo=fixtureinfo,
                         keywords={callspec.id: True},
                         originalname=func.name,
                     )
                 else:  # TODO remove with pytest >= 5.4
                     return pytest.Function(
                         name=f"{func.name}[{context_param}]",
                         parent=item,
                         callspec=callspec,
                         callobj=dummy,
                         fixtureinfo=fixtureinfo,
                         keywords={callspec.id: True},
                         originalname=func.name,
                     )
         suggestions = [callspec.id for callspec in metafunc._calls]
         raise ValueError(
             f"Could not find context parametrization {context_param}, possible values: {suggestions}"
         )
     return func
Exemple #14
0
def pytest_pycollect_makeitem(collector, name, obj):
    """A pytest hook to collect coroutines in a test module."""
    if collector.funcnamefilter(name) and _is_coroutine(obj):
        item = pytest.Function(name, parent=collector)
        if 'curio' in item.keywords:
            return list(collector._genfunctions(name, obj))
Exemple #15
0
def pytest_pycollect_makeitem(collector, name, obj):
    if collector.funcnamefilter(name) and inspect.isgeneratorfunction(obj):
        item = pytest.Function(name, parent=collector)
        if ('asyncio' in item.keywords
                or 'asyncio_process_pool' in item.keywords):
            return list(collector._genfunctions(name, obj))
Exemple #16
0
def pytest_pycollect_makeitem(collector, name, obj):
    """Support for async tests."""
    if collector.funcnamefilter(name) and inspect.isgeneratorfunction(obj):
        item = pytest.Function(name, parent=collector)
        if 'async' in item.keywords:
            return list(collector._genfunctions(name, obj))
Exemple #17
0
def create_test(metafunc, func):
    if pytest.__version__ >= "5.":
        return pytest.Function.from_parent(metafunc,
                                           name=func.__name__,
                                           callobj=func)
    return pytest.Function(do_deploy.__name__, metafunc, callobj=do_deploy)