def deactivate_module():
     """
     If next test is in new module - deactivate current one.
     """
     if ItemState.item.module != getattr(ItemState.nextitem, 'module',
                                         None):
         ItemState.deactivate(ItemState.current_module.name)
 def finish(self):
     """
     Finale for fixtures
     """
     while self._finalizer:
         func = self._finalizer.pop()
         old_std, old_err = sys.stdout, sys.stderr
         new_one_info, new_one_error = StringIO(), StringIO()
         sys.stdout, sys.stderr = new_one_info, new_one_error
         func()
         info = new_one_info.getvalue()
         err = new_one_error.getvalue()
         sys.stdout, sys.stderr = old_std, old_err
         if not type(func) is MethodType:
             _fixture = fixture_info(
                 scope=self.scope, action=ErpActions.start_teardown_fixture)
             item = ItemState(self.argname,
                              func.__doc__,
                              fixture_mapping=_fixture,
                              out=info,
                              err=err).activate()
             ErpActions.log(item)
             item.here_and_now_deactivate()
     try:
         del self.cached_result
     except AttributeError:
         pass
 def deactivate_class():
     """
     If next test is in new class - deactivate current class and switch active focus to module.
     """
     if ItemState.item.cls != getattr(ItemState.nextitem, 'cls', None):
         ItemState.deactivate(ItemState.current_class.name)
         ItemState.active_item_id = ItemState.current_module.id
 def check_next():
     """
     If after unbound method new module or nothing - deactivate current module and return to prevent
     further verification and double sending.
     """
     if not ItemState.nextitem:
         ItemState.deactivate(ItemState.current_module.name)
     deactivate_module()
     return False
 def pytest_runtest_call(self, item):
     """
     Send STEP item to report portal for each test, get function name and docstring.
     :param item: Test going to be executed.
     """
     self.active_test = ItemState(item.name, item.function.__doc__,
                                  'STEP').activate()
 def simulate_test_after_fixturefail():
     """
     Send info regarding skipped test to report portal.
     """
     self.active_test = ItemState(item.name, item.function.__doc__,
                                  'STEP').activate()
     self.active_test.stderr = failure
     self.active_test.failed = True
        def deactivate_unbound_method():
            """
            Next test is able to be in new module, new class, or just unbound method in the same class.
            For unbound method create envelop with the same name to fill in fixtures depends on this test.
            """
            def check_next():
                """
                If after unbound method new module or nothing - deactivate current module and return to prevent
                further verification and double sending.
                """
                if not ItemState.nextitem:
                    ItemState.deactivate(ItemState.current_module.name)
                deactivate_module()
                return False

            if ItemState.current_unbound_method:
                ItemState.deactivate(ItemState.current_unbound_method.name +
                                     ' envelop')
                ItemState.current_unbound_method = None
                ItemState.active_item_id = ItemState.current_module.id
                return check_next()
            return True
Beispiel #8
0
    def fixture(cls, item):
        from pytest_plugin.mapping_fixture_action import ItemState

        if item.fixture.scope is 'session':
            if not ItemState.current_session:
                ItemState.current_session = ItemState('Global events', 'Holder for global objects',
                                                      type='SUITE').activate().id

        send_to = {'session': [ItemState.current_session, 'BEFORE_SUITE'],
                   'module': [ItemState.current_module.id, 'BEFORE_SUITE'],
                   'class': [ItemState.active_item_id, 'BEFORE_CLASS'],
                   'function': [ItemState.active_item_id, 'BEFORE_METHOD']}

        item.type = send_to[item.fixture.scope][1]
        cls.start(item, query_s={'item_id': send_to[item.fixture.scope][0]})
 def pytest_sessionstart(self, session):
     """
     Event to start launch on report portal.
     Shadows base pytest_sessionstart realization.
     """
     session._setupstate = ExtSetupState()
     session._fixturemanager = ExtFixtureManager(session)
     session.config.pluginmanager.import_plugin('python')
     self.project = session.config.option.erp_proj
     ItemState.project_name = self.project
     self.launch = session.config.option.launch_id
     self.suite_name = session.config.option.suite_name
     self.launch_id = ItemState(self.launch, self.suite_name,
                                'LAUNCH').activate().id
     LOGGER.info(
         'Session is started. Launch num {}, project name {}, suite name {}'
         .format(self.launch_id, self.project, self.suite_name))
 def _activate_class(self, item):
     """
     Check if test belongs either current class or new one or just unbound function in class.
     If new class then create new Test item and send to report portal.
     Store class object to ItemState.active_item_id.
     :param item: Test going to be executed.
     """
     get_class_name = lambda: ' '.join((str(item.cls).split('.')[-1:]))[:-2]
     if item.cls:
         if ItemState.current_class is None or item.cls is not ItemState.current_class.link:
             ItemState.current_class = ItemState(get_class_name(),
                                                 item.cls.__doc__,
                                                 'TEST').activate()
             ItemState.active_item_id = ItemState.current_class.id
             ItemState.current_class.link = item.cls
     else:
         ItemState.current_class = None
     return self
    def _activate_module(self, item):
        """
        Check if test belongs either current module or new one.
        If new module then create new suite instance and send to report portal.
        Store module object to ItemState.active_item_id.
        :param item: Test going to be executed.
        """
        get_module_name = lambda: ' '.join(
            str(item.module).split(os.path.sep)[-2:])[:-2]
        module = getattr(ItemState, 'current_module')
        if module and item.module is not ItemState.current_module.link:
            ItemState.current_class, ItemState.current_module = None, None

        if ItemState.current_module is None or item.module is not ItemState.current_module.link:
            name = get_module_name()
            ItemState.current_module = ItemState(name, item.module.__doc__,
                                                 'SUITE').activate()
            ItemState.current_module.link = item.module
            ItemState.active_item_id = ItemState.current_module.id
        return self
 def pytest_runtest_teardown(self, item, nextitem):
     ItemState.item, ItemState.nextitem = item, nextitem
     self.active_test.stdout = self.active_test.item.outerr
     ErpActions.log(self.active_test)
     ItemState.here_and_now_deactivate(self.active_test)
 def _activate_unbound_function(self, item):
     if not getattr(item, 'instance', None):
         item_wrap = ItemState(item.name + ' envelop',
                               item.function.__doc__, 'TEST').activate()
         ItemState.current_unbound_method = item
         ItemState.active_item_id = item_wrap.id
    def execute(self, request):
        # get required arguments and register our own finish()
        # with their finalization
        kwargs = {}
        for argname in self.argnames:
            fixturedef = request._get_active_fixturedef(argname)
            result, arg_cache_key = fixturedef.cached_result
            kwargs[argname] = result
            if argname != "request":
                fixturedef.addfinalizer(self.finish)

        my_cache_key = request.param_index
        cached_result = getattr(self, "cached_result", None)
        if cached_result is not None:
            # print argname, "Found cached_result", cached_result
            # print argname, "param_index", param_index
            result, cache_key = cached_result
            if my_cache_key == cache_key:
                # print request.fixturename, "CACHE HIT", repr(my_cache_key)
                return result
            # print request.fixturename, "CACHE MISS"
            # we have a previous but differently parametrized fixture instance
            # so we need to tear it down before creating a new one
            self.finish()
            assert not hasattr(self, "cached_result")

        if self.unittest:
            result = self.func(request.instance, **kwargs)
        else:
            fixturefunc = self.func
            # the fixture function needs to be bound to the actual
            # request.instance so that code working with "self" behaves
            # as expected.
            if request.instance is not None:
                fixturefunc = getimfunc(self.func)
                if fixturefunc != self.func:
                    fixturefunc = fixturefunc.__get__(request.instance)

            old_std, old_err = sys.stdout, sys.stderr
            new_one_info, new_one_error = StringIO(), StringIO()
            sys.stdout, sys.stderr = new_one_info, new_one_error

            _fixture = fixture_info(self.scope, ErpActions.fixture, None,
                                    fixturefunc._pytestfixturefunction.autouse)
            item = ItemState(self.argname,
                             fixturefunc.__doc__,
                             fixture_mapping=_fixture).activate()
            ItemState.current_fixture = item
            result = call_fixture_func(fixturefunc, request, kwargs,
                                       self.yieldctx)
            # _fixture(result)

            info = new_one_info.getvalue()
            err = new_one_error.getvalue()
            sys.stdout, sys.stderr = old_std, old_err
            item.stdout, item.stderr = info, err
            # item = ItemState(self.argname, fixturefunc.__doc__, fixture_mapping=_fixture, out=info, err=err).activate()
            ErpActions.log(item)
            item.here_and_now_deactivate()
        self.cached_result = (result, my_cache_key)
        return result