Best Python code snippet using green
jb_behave_formatter.py
Source:jb_behave_formatter.py  
...13from teamcity.messages import TeamcityServiceMessages14def _step_name(step):15    assert isinstance(step, Step)16    return step.keyword + " " + step.name.strip()17def _suite_name(suite):18    return suite.name.strip()19class TeamcityFormatter(Formatter):20    """21    Stateful TC reporter.22    Since we can't fetch all steps from the very beginning (even skipped tests are reported)23    we store tests and features on each call.24    To hook into test reporting override _report_suite_started and/or _report_test_started25    """26    def __init__(self, stream_opener, config):27        super(TeamcityFormatter, self).__init__(stream_opener, config)28        assert version.LooseVersion(behave_version) >= version.LooseVersion("1.2.6"), "Only 1.2.6+ is supported"29        self._messages = TeamcityServiceMessages()30        self.__feature = None31        self.__scenario = None32        self.__steps = deque()33        self.__scenario_opened = False34        self.__feature_opened = False35        self.__test_start_time = None36    def feature(self, feature):37        assert isinstance(feature, Feature)38        assert not self.__feature, "Prev. feature not closed"39        self.__feature = feature40    def scenario(self, scenario):41        assert isinstance(scenario, Scenario)42        self.__scenario = scenario43        self.__scenario_opened = False44        self.__steps.clear()45    def step(self, step):46        assert isinstance(step, Step)47        self.__steps.append(step)48    def match(self, match):49        if not self.__feature_opened:50            self._report_suite_started(self.__feature, _suite_name(self.__feature))51            self.__feature_opened = True52        if not self.__scenario_opened:53            self._report_suite_started(self.__scenario, _suite_name(self.__scenario))54            self.__scenario_opened = True55        assert self.__steps, "No steps left"56        step = self.__steps.popleft()57        self._report_test_started(step, _step_name(step))58        self.__test_start_time = datetime.datetime.now()59    def _report_suite_started(self, suite, suite_name):60        """61        :param suite: behave suite62        :param suite_name: suite name that must be reported, be sure to use it instead of suite.name63        """64        self._messages.testSuiteStarted(suite_name)65    def _report_test_started(self, test, test_name):66        """67        Suite name is always stripped, be sure to strip() it too68        :param test: behave test69        :param test_name: test name that must be reported, be sure to use it instead of test.name70        """71        self._messages.testStarted(test_name)72    def result(self, step):73        assert isinstance(step, Step)74        step_name = _step_name(step)75        if step.status == Status.failed:76            try:77                error = traceback.format_exc(step.exc_traceback)78                if error != step.error_message:79                    self._messages.testStdErr(step_name, error)80            except Exception:81                pass  # exception shall not prevent error message82            self._messages.testFailed(step_name, message=step.error_message)83        if step.status == Status.undefined:84            self._messages.testFailed(step_name, message="Undefined")85        if step.status == Status.skipped:86            self._messages.testIgnored(step_name)87        self._messages.testFinished(step_name, testDuration=datetime.datetime.now() - self.__test_start_time)88        if not self.__steps:89            self.__close_scenario()90        elif step.status in [Status.failed, Status.undefined]:91            # Broken background/undefined step stops whole scenario92            reason = "Undefined step" if step.status == Status.undefined else "Prev. step failed"93            self.__skip_rest_of_scenario(reason)94    def __skip_rest_of_scenario(self, reason):95        while self.__steps:96            step = self.__steps.popleft()97            self._report_test_started(step, _step_name(step))98            self._messages.testIgnored(_step_name(step),99                                       message="{0}. Rest part of scenario is skipped".format(reason))100            self._messages.testFinished(_step_name(step))101        self.__close_scenario()102    def __close_scenario(self):103        if self.__scenario:104            self._messages.testSuiteFinished(_suite_name(self.__scenario))105            self.__scenario = None106    def eof(self):107        self.__skip_rest_of_scenario("")108        self._messages.testSuiteFinished(_suite_name(self.__feature))109        self.__feature = None...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
