Best Python code snippet using avocado_python
exec_test.py
Source:exec_test.py  
...20    """21    name = "exec-test"22    description = "Runner for standalone executables treated as tests"23    CONFIGURATION_USED = ["run.keep_tmp", "runner.exectest.exitcodes.skip"]24    def _process_final_status(25        self, process, runnable, stdout=None, stderr=None26    ):  # pylint: disable=W061327        # Since Runners are standalone, and could be executed on a remote28        # machine in an "isolated" way, there is no way to assume a default29        # value, at this moment.30        skip_codes = runnable.config.get("runner.exectest.exitcodes.skip", [])31        final_status = {}32        if skip_codes is None:33            skip_codes = []34        if process.returncode in skip_codes:35            final_status["result"] = "skip"36        elif process.returncode == 0:37            final_status["result"] = "pass"38        else:39            final_status["result"] = "fail"40        final_status["returncode"] = process.returncode41        return self.prepare_status("finished", final_status)42    def _cleanup(self, runnable):43        """Cleanup method for the exec-test tests"""44        # cleanup temporary directories45        workdir = runnable.kwargs.get("AVOCADO_TEST_WORKDIR")46        if (47            workdir is not None48            and runnable.config.get("run.keep_tmp") is not None49            and os.path.exists(workdir)50        ):51            shutil.rmtree(workdir)52    def _create_params(self, runnable):53        """Create params for the test"""54        if runnable.variant is None:55            return {}56        params = dict(57            [(str(key), str(val)) for _, key, val in runnable.variant["variant"][0][1]]58        )59        return params60    @staticmethod61    def _get_avocado_version():62        """Return the Avocado package version, if installed"""63        version = "unknown.unknown"64        try:65            version = pkg_resources.get_distribution("avocado-framework").version66        except pkg_resources.DistributionNotFound:67            pass68        return version69    def _get_env_variables(self, runnable):70        """Get the default AVOCADO_* environment variables71        These variables are available to the test environment during the test72        execution.73        """74        # create the temporary work dir75        workdir = tempfile.mkdtemp(prefix=".avocado-workdir-")76        # create the avocado environment variable dictionary77        avocado_test_env_variables = {78            "AVOCADO_VERSION": self._get_avocado_version(),79            "AVOCADO_TEST_WORKDIR": workdir,80        }81        if runnable.output_dir:82            avocado_test_env_variables["AVOCADO_TEST_OUTPUTDIR"] = runnable.output_dir83        return avocado_test_env_variables84    @staticmethod85    def _is_uri_a_file_on_cwd(uri):86        if (87            uri is not None88            and os.path.basename(uri) == uri89            and os.access(uri, os.R_OK | os.X_OK)90        ):91            return True92        return False93    def _get_env(self, runnable):94        env = dict(os.environ)95        if runnable.kwargs:96            env.update(runnable.kwargs)97        # set default Avocado environment variables if running on a valid Task98        if runnable.uri is not None:99            avocado_test_env_variables = self._get_env_variables(runnable)100            # save environment variables for further cleanup101            runnable.kwargs.update(avocado_test_env_variables)102            if env is None:103                env = avocado_test_env_variables104            else:105                env.update(avocado_test_env_variables)106        params = self._create_params(runnable)107        if params:108            env.update(params)109        if env and "PATH" not in env:110            env["PATH"] = os.environ.get("PATH")111        # Support for running executable tests in the current working directory112        if self._is_uri_a_file_on_cwd(runnable.uri):113            env["PATH"] += f":{os.getcwd()}"114        return env115    def _run_proc(self, runnable):116        return subprocess.Popen(117            [runnable.uri] + list(runnable.args),118            stdin=subprocess.DEVNULL,119            stdout=subprocess.PIPE,120            stderr=subprocess.PIPE,121            env=self._get_env(runnable),122        )123    def run(self, runnable):124        yield self.prepare_status("started")125        try:126            process = self._run_proc(runnable)127        except Exception as e:128            yield self.prepare_status(129                "finished", {"result": "error", "fail_reason": str(e)}130            )131            self._cleanup(runnable)132            return133        def poll_proc():134            return process.poll() is not None135        yield from self.running_loop(poll_proc)136        stdout = process.stdout.read()137        stderr = process.stderr.read()138        yield self.prepare_status("running", {"type": "stdout", "log": stdout})139        yield self.prepare_status("running", {"type": "stderr", "log": stderr})140        yield self._process_final_status(process, runnable, stdout, stderr)141        self._cleanup(runnable)142class RunnerApp(BaseRunnerApp):143    PROG_NAME = "avocado-runner-exec-test"144    PROG_DESCRIPTION = "nrunner application for exec-test tests"145    RUNNABLE_KINDS_CAPABLE = ["exec-test"]146def main():147    app = RunnerApp(print)148    app.run()149if __name__ == "__main__":...tap.py
Source:tap.py  
...45                    break46                else:47                    result = "pass"48        return result49    def _process_final_status(50        self, process, runnable, stdout=None, stderr=None51    ):  # pylint: disable=W061352        return FinishedMessage.get(53            self._get_tap_result(stdout), returncode=process.returncode54        )55class RunnerApp(BaseRunnerApp):56    PROG_NAME = "avocado-runner-tap"57    PROG_DESCRIPTION = "nrunner application for executable tests that produce TAP"58    RUNNABLE_KINDS_CAPABLE = ["tap"]59def main():60    app = RunnerApp(print)61    app.run()62if __name__ == "__main__":63    main()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!!
