Best Python code snippet using avocado_python
test_output.py
Source:test_output.py  
...125    suite = TestSuite.from_config(config)126    with Job(config, [suite]) as j:127        sys.exit(j.run())128"""129def perl_tap_parser_uncapable():130    return os.system("perl -e 'use TAP::Parser;'") != 0131def missing_binary(binary):132    try:133        utils_path.find_command(binary)134        return False135    except utils_path.CmdNotFoundError:136        return True137class OutputTest(TestCaseTmpDir):138    @unittest.skipIf(missing_binary('cc'),139                     "C compiler is required by the underlying doublefree.py test")140    def test_output_doublefree(self):141        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo '142                    'doublefree.py' % (AVOCADO, self.tmpdir.name))143        result = process.run(cmd_line, ignore_status=True)144        expected_rc = exit_codes.AVOCADO_ALL_OK145        output = result.stdout + result.stderr146        self.assertEqual(result.exit_status, expected_rc,147                         "Avocado did not return rc %d:\n%s" %148                         (expected_rc, result))149        bad_string = b'double free or corruption'150        self.assertNotIn(bad_string, output,151                         "Libc double free can be seen in avocado "152                         "doublefree output:\n%s" % output)153    def test_print_to_std(self):154        def _check_output(path, exps, name):155            i = 0156            end = len(exps)157            with open(path, 'rb') as output_file:158                output_file_content = output_file.read()159                output_file.seek(0)160                for line in output_file:161                    if exps[i] in line:162                        i += 1163                        if i == end:164                            break165                exps_text = "\n".join([exp.decode() for exp in exps])166                error_msg = ("Failed to find message in position %s from\n%s\n"167                             "\nin the %s. Either it's missing or in wrong "168                             "order.\n%s" % (i, exps_text, name,169                                             output_file_content))170                self.assertEqual(i, end, error_msg)171        test = script.Script(os.path.join(self.tmpdir.name, "output_test.py"),172                             OUTPUT_TEST_CONTENT)173        test.save()174        result = process.run("%s run --job-results-dir %s --disable-sysinfo "175                             "--json - -- %s" % (AVOCADO, self.tmpdir.name, test))176        res = json.loads(result.stdout_text)177        joblog = res["debuglog"]178        exps = [b"[stdout] top_print", b"[stdout] top_stdout",179                b"[stderr] top_stderr", b"[stdout] top_process",180                b"[stdout] init_print", b"[stdout] init_stdout",181                b"[stderr] init_stderr", b"[stdout] init_process",182                b"[stdout] test_print", b"[stdout] test_stdout",183                b"[stderr] test_stderr", b"[stdout] test_process"]184        _check_output(joblog, exps, "job.log")185        testdir = res["tests"][0]["logdir"]186        with open(os.path.join(testdir, "stdout"), 'rb') as stdout_file:187            self.assertEqual(b"test_print\ntest_stdout\ntest_process__test_stdout__",188                             stdout_file.read())189        with open(os.path.join(testdir, "stderr"), 'rb') as stderr_file:190            self.assertEqual(b"test_stderr\n__test_stderr__",191                             stderr_file.read())192        # Now run the same test, but with combined output193        # combined output can not keep track of sys.stdout and sys.stdout194        # writes, as they will eventually be out of sync.  In fact,195        # the correct fix is to run the entire test process with redirected196        # stdout and stderr, and *not* play with sys.stdout and sys.stderr.197        # But this change will come later198        result = process.run("%s run --job-results-dir %s --disable-sysinfo "199                             "--output-check-record=combined "200                             "--json - -- %s" % (AVOCADO, self.tmpdir.name, test))201        res = json.loads(result.stdout_text)202        testdir = res["tests"][0]["logdir"]203        with open(os.path.join(testdir, "output")) as output_file:204            self.assertEqual("test_process__test_stderr____test_stdout__",205                             output_file.read())206    def test_check_record_no_module_default(self):207        """208        Checks that the `avocado.utils.process` module won't have a output209        check record mode (`OUTPUT_CHECK_RECORD_MODE`) set by default.210        The reason is that, if this is always set from the command211        line runner, we can't distinguish from a situation where the212        module level configuration should be applied as a fallback to213        the API parameter.  By leaving it unset by default, the command line214        option parameter value `none` will slightly change its behavior,215        meaning that it will explicitly disable output check record when216        asked to do so.217        """218        with script.Script(os.path.join(self.tmpdir.name, "output_mode_none.py"),219                           OUTPUT_MODE_NONE_CONTENT,220                           script.READ_ONLY_MODE) as test:221            command = ("%s run --job-results-dir %s --disable-sysinfo "222                       "--json - --output-check-record none -- %s") % (AVOCADO,223                                                                       self.tmpdir.name,224                                                                       test.path)225            result = process.run(command)226            res = json.loads(result.stdout_text)227            testdir = res["tests"][0]["logdir"]228            for output_file in ('stdout', 'stderr', 'output'):229                output_file_path = os.path.join(testdir, output_file)230                self.assertTrue(os.path.exists(output_file_path))231                with open(output_file_path, 'r') as output:232                    self.assertEqual(output.read(), '')233    def test_check_on_off(self):234        """235        Checks that output will always be kept, but it will only make into236        the *test* stdout/stderr/output files when it's not explicitly disabled237        This control is defined as an API parameter, `allow_output_check`, so238        it should be possible to enable/disable it on each call.239        """240        with script.Script(os.path.join(self.tmpdir.name, "test_check_on_off.py"),241                           OUTPUT_CHECK_ON_OFF_CONTENT,242                           script.READ_ONLY_MODE) as test:243            command = ("%s run --job-results-dir %s --disable-sysinfo "244                       "--json - -- %s") % (AVOCADO, self.tmpdir.name, test.path)245            result = process.run(command)246            res = json.loads(result.stdout_text)247            testdir = res["tests"][0]["logdir"]248            stdout_path = os.path.join(testdir, 'stdout')249            self.assertTrue(os.path.exists(stdout_path))250            with open(stdout_path, 'r') as stdout:251                self.assertEqual(stdout.read(),252                                 '__STDOUT_CONTENT____STDOUT_DO_RECORD_CONTENT__')253            stderr_path = os.path.join(testdir, 'stderr')254            self.assertTrue(os.path.exists(stderr_path))255            with open(stderr_path, 'r') as stderr:256                self.assertEqual(stderr.read(),257                                 '__STDERR_CONTENT____STDERR_DO_RECORD_CONTENT__')258    @skipUnlessPathExists('/bin/true')259    def test_show(self):260        """261        Checks if `core.show` is respected in different configurations.262        """263        with script.Script(os.path.join(self.tmpdir.name, "test_show.py"),264                           OUTPUT_SHOW_TEST, script.READ_ONLY_MODE) as test:265            cmd = "%s run %s" % (AVOCADO, test.path)266            result = process.run(cmd)267            expected_job_id_number = 2268            expected_bin_true_number = 1269            job_id_number = result.stdout_text.count('JOB ID')270            bin_true_number = result.stdout_text.count('/bin/true')271            self.assertEqual(expected_job_id_number, job_id_number)272            self.assertEqual(expected_bin_true_number, bin_true_number)273    def tearDown(self):274        self.tmpdir.cleanup()275class OutputPluginTest(TestCaseTmpDir):276    def check_output_files(self, debug_log):277        base_dir = os.path.dirname(debug_log)278        json_output_path = os.path.join(base_dir, 'results.json')279        self.assertTrue(os.path.isfile(json_output_path))280        with open(json_output_path, 'r') as fp:281            json.load(fp)282        xunit_output_path = os.path.join(base_dir, 'results.xml')283        self.assertTrue(os.path.isfile(json_output_path))284        try:285            minidom.parse(xunit_output_path)286        except Exception as details:287            xunit_output_content = genio.read_file(xunit_output_path)288            raise AssertionError("Unable to parse xunit output: %s\n\n%s"289                                 % (details, xunit_output_content))290        tap_output = os.path.join(base_dir, "results.tap")291        self.assertTrue(os.path.isfile(tap_output))292        tap = genio.read_file(tap_output)293        self.assertIn("..", tap)294        self.assertIn("\n# debug.log of ", tap)295    def test_output_incompatible_setup(self):296        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo '297                    '--xunit - --json - passtest.py' % (AVOCADO, self.tmpdir.name))298        result = process.run(cmd_line, ignore_status=True)299        expected_rc = exit_codes.AVOCADO_FAIL300        self.assertEqual(result.exit_status, expected_rc,301                         "Avocado did not return rc %d:\n%s" %302                         (expected_rc, result))303        error_regex = re.compile(r'.*Options ((--xunit --json)|'304                                 '(--json --xunit)) are trying to use stdout '305                                 'simultaneously.', re.MULTILINE | re.DOTALL)306        self.assertIsNotNone(error_regex.match(result.stderr_text),307                             "Missing error message from output:\n%s" %308                             result.stderr)309    def test_output_compatible_setup(self):310        tmpfile = tempfile.mktemp(dir=self.tmpdir.name)311        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo '312                    '--journal --xunit %s --json - passtest.py' %313                    (AVOCADO, self.tmpdir.name, tmpfile))314        result = process.run(cmd_line, ignore_status=True)315        expected_rc = exit_codes.AVOCADO_ALL_OK316        self.assertEqual(result.exit_status, expected_rc,317                         "Avocado did not return rc %d:\n%s" %318                         (expected_rc, result))319        # Check if we are producing valid outputs320        json.loads(result.stdout_text)321        minidom.parse(tmpfile)322    def test_output_compatible_setup_2(self):323        tmpfile = tempfile.mktemp(dir=self.tmpdir.name)324        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo '325                    '--xunit - --json %s --tap-include-logs passtest.py'326                    % (AVOCADO, self.tmpdir.name, tmpfile))327        result = process.run(cmd_line, ignore_status=True)328        expected_rc = exit_codes.AVOCADO_ALL_OK329        self.assertEqual(result.exit_status, expected_rc,330                         "Avocado did not return rc %d:\n%s" %331                         (expected_rc, result))332        # Check if we are producing valid outputs333        with open(tmpfile, 'r') as fp:334            json_results = json.load(fp)335            debug_log = json_results['debuglog']336            self.check_output_files(debug_log)337        minidom.parseString(result.stdout_text)338    def test_output_compatible_setup_nooutput(self):339        tmpfile = tempfile.mktemp(dir=self.tmpdir.name)340        tmpfile2 = tempfile.mktemp(dir=self.tmpdir.name)341        # Verify --show=none can be supplied as app argument342        cmd_line = ('%s --show=none run --job-results-dir %s '343                    '--disable-sysinfo --xunit %s --json %s --tap-include-logs '344                    'passtest.py' % (AVOCADO, self.tmpdir.name, tmpfile, tmpfile2))345        result = process.run(cmd_line, ignore_status=True)346        expected_rc = exit_codes.AVOCADO_ALL_OK347        self.assertEqual(result.exit_status, expected_rc,348                         "Avocado did not return rc %d:\n%s" %349                         (expected_rc, result))350        self.assertEqual(result.stdout, b"",351                         "Output is not empty:\n%s" % result.stdout)352        # Check if we are producing valid outputs353        with open(tmpfile2, 'r') as fp:354            json_results = json.load(fp)355            debug_log = json_results['debuglog']356            self.check_output_files(debug_log)357        minidom.parse(tmpfile)358    def test_nonprintable_chars(self):359        cmd_line = ("%s run --external-runner /bin/ls "360                    "'NON_EXISTING_FILE_WITH_NONPRINTABLE_CHARS_IN_HERE\x1b' "361                    "--job-results-dir %s --disable-sysinfo --tap-include-logs"362                    % (AVOCADO, self.tmpdir.name))363        result = process.run(cmd_line, ignore_status=True)364        output = result.stdout_text + result.stderr_text365        expected_rc = exit_codes.AVOCADO_TESTS_FAIL366        self.assertEqual(result.exit_status, expected_rc,367                         "Avocado did not return rc %d:\n%s" %368                         (expected_rc, result))369        debug_log = None370        for line in output.splitlines():371            if "JOB LOG" in line:372                debug_log = line.split(':', 1)[-1].strip()373                break374        self.assertTrue(debug_log, "Unable to get JOB LOG from output:\n%s"375                        % output)376        self.check_output_files(debug_log)377    def test_show_test(self):378        cmd_line = ('%s --show=test run --job-results-dir %s --disable-sysinfo '379                    'passtest.py' % (AVOCADO, self.tmpdir.name))380        result = process.run(cmd_line, ignore_status=True)381        expected_rc = exit_codes.AVOCADO_ALL_OK382        self.assertEqual(result.exit_status, expected_rc,383                         "Avocado did not return rc %d:\n%s" %384                         (expected_rc, result))385        job_id_list = re.findall('Job ID: (.*)', result.stdout_text,386                                 re.MULTILINE)387        self.assertTrue(job_id_list, 'No Job ID in stdout:\n%s' %388                        result.stdout)389        job_id = job_id_list[0]390        self.assertEqual(len(job_id), 40)391    def test_silent_trumps_test(self):392        # Also verify --show=none can be supplied as run option393        cmd_line = ('%s --show=test --show=none run --job-results-dir %s '394                    '--disable-sysinfo passtest.py'395                    % (AVOCADO, self.tmpdir.name))396        result = process.run(cmd_line, ignore_status=True)397        expected_rc = exit_codes.AVOCADO_ALL_OK398        self.assertEqual(result.exit_status, expected_rc,399                         "Avocado did not return rc %d:\n%s" %400                         (expected_rc, result))401        self.assertEqual(result.stdout, b"")402    def test_default_enabled_plugins(self):403        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo '404                    '--tap-include-logs passtest.py'405                    % (AVOCADO, self.tmpdir.name))406        result = process.run(cmd_line, ignore_status=True)407        output = result.stdout_text + result.stderr_text408        expected_rc = exit_codes.AVOCADO_ALL_OK409        self.assertEqual(result.exit_status, expected_rc,410                         "Avocado did not return rc %d:\n%s" %411                         (expected_rc, result))412        output_lines = output.splitlines()413        # The current human output produces 6 lines when running a single test,414        # with an optional 7th line when the HTML report generation is enabled415        self.assertGreaterEqual(len(output_lines), 6,416                                ('Basic human interface did not produce the '417                                 'expected output. Output produced: "%s"' % output))418        second_line = output_lines[1]419        debug_log = second_line.split()[-1]420        self.check_output_files(debug_log)421    def test_verify_whiteboard_save(self):422        tmpfile = tempfile.mktemp(dir=self.tmpdir.name)423        config = os.path.join(self.tmpdir.name, "conf.ini")424        content = ("[datadir.paths]\nlogs_dir = %s"425                   % os.path.relpath(self.tmpdir.name, "."))426        script.Script(config, content).save()427        cmd_line = ('%s --config %s --show all run '428                    '--job-results-dir %s --disable-sysinfo whiteboard.py '429                    '--json %s' % (AVOCADO, config, self.tmpdir.name, tmpfile))430        result = process.run(cmd_line, ignore_status=True)431        expected_rc = exit_codes.AVOCADO_ALL_OK432        self.assertEqual(result.exit_status, expected_rc,433                         "Avocado did not return rc %d:\n%s" %434                         (expected_rc, result))435        with open(tmpfile, 'r') as fp:436            json_results = json.load(fp)437            logfile = json_results['tests'][0]['logfile']438            debug_dir = os.path.dirname(logfile)439            whiteboard_path = os.path.join(debug_dir, 'whiteboard')440            self.assertTrue(os.path.exists(whiteboard_path),441                            'Missing whiteboard file %s' % whiteboard_path)442    def test_gendata(self):443        tmpfile = tempfile.mktemp(dir=self.tmpdir.name)444        cmd_line = ("%s run --job-results-dir %s "445                    "--disable-sysinfo gendata.py --json %s" %446                    (AVOCADO, self.tmpdir.name, tmpfile))447        result = process.run(cmd_line, ignore_status=True)448        expected_rc = exit_codes.AVOCADO_ALL_OK449        self.assertEqual(result.exit_status, expected_rc,450                         "Avocado did not return rc %d:\n%s" %451                         (expected_rc, result))452        with open(tmpfile, 'r') as fp:453            json_results = json.load(fp)454            json_dir = None455            test = json_results['tests'][0]456            if "test_json" in test['id']:457                json_dir = test['logfile']458            self.assertTrue(json_dir, "Failed to get test_json output "459                            "directory")460            json_dir = os.path.join(os.path.dirname(json_dir), "data",461                                    "test.json")462            self.assertTrue(os.path.exists(json_dir), "File %s produced by"463                            "test does not exist" % json_dir)464    def test_redirect_output(self):465        redirected_output_path = tempfile.mktemp(dir=self.tmpdir.name)466        cmd_line = ('%s run --job-results-dir %s '467                    '--disable-sysinfo passtest.py > %s'468                    % (AVOCADO, self.tmpdir.name, redirected_output_path))469        result = process.run(cmd_line, ignore_status=True, shell=True)470        expected_rc = exit_codes.AVOCADO_ALL_OK471        self.assertEqual(result.exit_status, expected_rc,472                         "Avocado did not return rc %d:\n%s" %473                         (expected_rc, result))474        self.assertEqual(result.stdout, b'',475                         'After redirecting to file, output is not empty: %s' % result.stdout)476        with open(redirected_output_path, 'r') as redirected_output_file_obj:477            redirected_output = redirected_output_file_obj.read()478            for code in TermSupport.ESCAPE_CODES:479                self.assertNotIn(code, redirected_output,480                                 'Found terminal support code %s in redirected output\n%s' %481                                 (code, redirected_output))482    @unittest.skipIf(perl_tap_parser_uncapable(),483                     "Uncapable of using Perl TAP::Parser library")484    def test_tap_parser(self):485        with script.TemporaryScript(486                "tap_parser.pl",487                PERL_TAP_PARSER_SNIPPET % self.tmpdir.name,488                self.tmpdir.name) as perl_script:489            process.run("perl %s" % perl_script)490    @unittest.skipIf(perl_tap_parser_uncapable(),491                     "Uncapable of using Perl TAP::Parser library")492    def test_tap_parser_failfast(self):493        with script.TemporaryScript(494                "tap_parser.pl",495                PERL_TAP_PARSER_FAILFAST_SNIPPET % self.tmpdir.name,496                self.tmpdir.name) as perl_script:497            process.run("perl %s" % perl_script)498    def test_tap_totaltests(self):499        cmd_line = ("%s run passtest.py passtest.py passtest.py passtest.py "500                    "--job-results-dir %s "501                    "--tap -" % (AVOCADO, self.tmpdir.name))502        result = process.run(cmd_line)503        expr = b'1..4'504        self.assertIn(expr, result.stdout, "'%s' not found in:\n%s"...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!!
