Best Python code snippet using avocado_python
test_output_check.py
Source:test_output_check.py  
...89"""90class RunnerSimpleTest(TestCaseTmpDir):91    def assertIsFile(self, path):92        self.assertTrue(os.path.isfile(path))93    def assertIsNotFile(self, path):94        self.assertFalse(os.path.isfile(path))95    def setUp(self):96        super(RunnerSimpleTest, self).setUp()97        content = b"#!/bin/sh\necho -n '%s';echo -n '%s'>&2" % (STDOUT, STDERR)98        self.output_script = script.TemporaryScript(99            'output_check.sh',100            content,101            'avocado_output_check_functional',102            open_mode='wb')103        self.output_script.save()104    def _check_output_record_all(self):105        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '106                    '--output-check-record all'107                    % (AVOCADO, self.tmpdir.name, self.output_script.path))108        result = process.run(cmd_line, ignore_status=True)109        expected_rc = exit_codes.AVOCADO_ALL_OK110        self.assertEqual(result.exit_status, expected_rc,111                         "Avocado did not return rc %d:\n%s" %112                         (expected_rc, result))113        stdout_file = "%s.data/stdout.expected" % self.output_script114        stderr_file = "%s.data/stderr.expected" % self.output_script115        with open(stdout_file, 'rb') as fd_stdout:116            self.assertEqual(fd_stdout.read(), STDOUT)117        with open(stderr_file, 'rb') as fd_stderr:118            self.assertEqual(fd_stderr.read(), STDERR)119    def _check_output_record_combined(self):120        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '121                    '--output-check-record combined'122                    % (AVOCADO, self.tmpdir.name, self.output_script.path))123        result = process.run(cmd_line, ignore_status=True)124        expected_rc = exit_codes.AVOCADO_ALL_OK125        self.assertEqual(result.exit_status, expected_rc,126                         "Avocado did not return rc %d:\n%s" %127                         (expected_rc, result))128        output_file = "%s.data/output.expected" % self.output_script129        with open(output_file, 'rb') as fd_output:130            self.assertEqual(fd_output.read(), STDOUT + STDERR)131    def _setup_simple_test(self, simple_test_content):132        variants_file = os.path.join(self.tmpdir.name, 'variants.json')133        with open(variants_file, 'w') as file_obj:134            file_obj.write(JSON_VARIANTS)135        simple_test = os.path.join(self.tmpdir.name, 'simpletest.py')136        with open(simple_test, 'w') as file_obj:137            file_obj.write(simple_test_content)138        return (simple_test, variants_file)139    def test_output_record_none(self):140        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '141                    '--output-check-record none'142                    % (AVOCADO, self.tmpdir.name, self.output_script.path))143        result = process.run(cmd_line, ignore_status=True)144        expected_rc = exit_codes.AVOCADO_ALL_OK145        self.assertEqual(result.exit_status, expected_rc,146                         "Avocado did not return rc %d:\n%s" %147                         (expected_rc, result))148        self.assertIsNotFile("%s.data/stdout.expected" % self.output_script)149        self.assertIsNotFile("%s.data/stderr.expected" % self.output_script)150    def test_output_record_stdout(self):151        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '152                    '--output-check-record stdout'153                    % (AVOCADO, self.tmpdir.name, self.output_script.path))154        result = process.run(cmd_line, ignore_status=True)155        expected_rc = exit_codes.AVOCADO_ALL_OK156        self.assertEqual(result.exit_status, expected_rc,157                         "Avocado did not return rc %d:\n%s" %158                         (expected_rc, result))159        stdout_file = "%s.data/stdout.expected" % self.output_script160        stderr_file = "%s.data/stderr.expected" % self.output_script161        with open(stdout_file, 'rb') as fd_stdout:162            self.assertEqual(fd_stdout.read(), STDOUT)163        self.assertIsNotFile(stderr_file)164    def test_output_record_and_check(self):165        self._check_output_record_all()166        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s'167                    % (AVOCADO, self.tmpdir.name, self.output_script.path))168        result = process.run(cmd_line, ignore_status=True)169        expected_rc = exit_codes.AVOCADO_ALL_OK170        self.assertEqual(result.exit_status, expected_rc,171                         "Avocado did not return rc %d:\n%s" %172                         (expected_rc, result))173    def test_output_record_and_check_combined(self):174        self._check_output_record_combined()175        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s'176                    % (AVOCADO, self.tmpdir.name, self.output_script.path))177        result = process.run(cmd_line, ignore_status=True)178        expected_rc = exit_codes.AVOCADO_ALL_OK179        self.assertEqual(result.exit_status, expected_rc,180                         "Avocado did not return rc %d:\n%s" %181                         (expected_rc, result))182    def test_output_tamper_stdout(self):183        self._check_output_record_all()184        tampered_msg = b"I PITY THE FOOL THAT STANDS ON MY WAY!"185        stdout_file = "%s.data/stdout.expected" % self.output_script.path186        with open(stdout_file, 'wb') as stdout_file_obj:187            stdout_file_obj.write(tampered_msg)188        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s --xunit -'189                    % (AVOCADO, self.tmpdir.name, self.output_script.path))190        result = process.run(cmd_line, ignore_status=True)191        expected_rc = exit_codes.AVOCADO_TESTS_FAIL192        self.assertEqual(result.exit_status, expected_rc,193                         "Avocado did not return rc %d:\n%s" %194                         (expected_rc, result))195        self.assertIn(tampered_msg, result.stdout)196    def test_output_tamper_combined(self):197        self._check_output_record_combined()198        tampered_msg = b"I PITY THE FOOL THAT STANDS ON MY WAY!"199        output_file = "%s.data/output.expected" % self.output_script.path200        with open(output_file, 'wb') as output_file_obj:201            output_file_obj.write(tampered_msg)202        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s --xunit -'203                    % (AVOCADO, self.tmpdir.name, self.output_script.path))204        result = process.run(cmd_line, ignore_status=True)205        expected_rc = exit_codes.AVOCADO_TESTS_FAIL206        self.assertEqual(result.exit_status, expected_rc,207                         "Avocado did not return rc %d:\n%s" %208                         (expected_rc, result))209        self.assertIn(tampered_msg, result.stdout)210    def test_output_diff(self):211        self._check_output_record_all()212        tampered_msg_stdout = b"I PITY THE FOOL THAT STANDS ON STDOUT!"213        tampered_msg_stderr = b"I PITY THE FOOL THAT STANDS ON STDERR!"214        stdout_file = "%s.data/stdout.expected" % self.output_script.path215        with open(stdout_file, 'wb') as stdout_file_obj:216            stdout_file_obj.write(tampered_msg_stdout)217        stderr_file = "%s.data/stderr.expected" % self.output_script.path218        with open(stderr_file, 'wb') as stderr_file_obj:219            stderr_file_obj.write(tampered_msg_stderr)220        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s --json -'221                    % (AVOCADO, self.tmpdir.name, self.output_script.path))222        result = process.run(cmd_line, ignore_status=True)223        expected_rc = exit_codes.AVOCADO_TESTS_FAIL224        self.assertEqual(result.exit_status, expected_rc,225                         "Avocado did not return rc %d:\n%s" %226                         (expected_rc, result))227        json_result = json.loads(result.stdout_text)228        job_log = json_result['debuglog']229        stdout_diff = os.path.join(json_result['tests'][0]['logdir'],230                                   'stdout.diff')231        stderr_diff = os.path.join(json_result['tests'][0]['logdir'],232                                   'stderr.diff')233        with open(stdout_diff, 'rb') as stdout_diff_obj:234            stdout_diff_content = stdout_diff_obj.read()235        self.assertIn(b'-I PITY THE FOOL THAT STANDS ON STDOUT!',236                      stdout_diff_content)237        self.assertIn(b'+' + STDOUT, stdout_diff_content)238        with open(stderr_diff, 'rb') as stderr_diff_obj:239            stderr_diff_content = stderr_diff_obj.read()240        self.assertIn(b'-I PITY THE FOOL THAT STANDS ON STDERR!',241                      stderr_diff_content)242        self.assertIn(b'+Hello, stderr!', stderr_diff_content)243        with open(job_log, 'rb') as job_log_obj:244            job_log_content = job_log_obj.read()245        self.assertIn(b'Stdout Diff:', job_log_content)246        self.assertIn(b'-I PITY THE FOOL THAT STANDS ON STDOUT!', job_log_content)247        self.assertIn(b'+' + STDOUT, job_log_content)248        self.assertIn(b'Stdout Diff:', job_log_content)249        self.assertIn(b'-I PITY THE FOOL THAT STANDS ON STDERR!', job_log_content)250        self.assertIn(b'+' + STDERR, job_log_content)251    def test_disable_output_check(self):252        self._check_output_record_all()253        tampered_msg = b"I PITY THE FOOL THAT STANDS ON MY WAY!"254        stdout_file = "%s.data/stdout.expected" % self.output_script.path255        with open(stdout_file, 'wb') as stdout_file_obj:256            stdout_file_obj.write(tampered_msg)257        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '258                    '--disable-output-check --xunit -'259                    % (AVOCADO, self.tmpdir.name, self.output_script.path))260        result = process.run(cmd_line, ignore_status=True)261        expected_rc = exit_codes.AVOCADO_ALL_OK262        self.assertEqual(result.exit_status, expected_rc,263                         "Avocado did not return rc %d:\n%s" %264                         (expected_rc, result))265        self.assertNotIn(tampered_msg, result.stdout)266    def test_merge_records_same_output(self):267        variants_file = os.path.join(self.tmpdir.name, 'variants.json')268        with open(variants_file, 'w') as file_obj:269            file_obj.write(JSON_VARIANTS)270        simple_test = os.path.join(self.tmpdir.name, 'simpletest.py')271        with open(simple_test, 'w') as file_obj:272            file_obj.write(TEST_WITH_SAME_EXPECTED_OUTPUT)273        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '274                    '--output-check-record both --json-variants-load %s' %275                    (AVOCADO, self.tmpdir.name, simple_test, variants_file))276        process.run(cmd_line, ignore_status=True)277        self.assertIsFile("%s.data/stdout.expected" % simple_test)278        self.assertIsFile("%s.data/stderr.expected" % simple_test)279    def test_merge_records_different_output(self):280        simple_test, variants_file = self._setup_simple_test(281            TEST_WITH_DIFFERENT_EXPECTED_OUTPUT)282        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '283                    '--output-check-record both --json-variants-load %s' %284                    (AVOCADO, self.tmpdir.name, simple_test, variants_file))285        process.run(cmd_line, ignore_status=True)286        self.assertIsNotFile("%s.data/stdout.expected" % simple_test)287        self.assertIsNotFile("%s.data/stderr.expected" % simple_test)288        self.assertIsFile("%s.data/PassTest.test_1/stdout.expected" % simple_test)289        self.assertIsFile("%s.data/PassTest.test_1/stderr.expected" % simple_test)290        self.assertIsFile("%s.data/PassTest.test_2/stdout.expected" % simple_test)291        self.assertIsFile("%s.data/PassTest.test_2/stderr.expected" % simple_test)292    def test_merge_records_different_output_variants(self):293        simple_test, variants_file = self._setup_simple_test(294            TEST_WITH_DIFFERENT_EXPECTED_OUTPUT_VARIANTS)295        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '296                    '--output-check-record both --json-variants-load %s' %297                    (AVOCADO, self.tmpdir.name, simple_test, variants_file))298        process.run(cmd_line, ignore_status=True)299        self.assertIsNotFile("%s.data/stdout.expected" % simple_test)300        self.assertIsNotFile("%s.data/stderr.expected" % simple_test)301        self.assertIsNotFile("%s.data/PassTest.test_1/stdout.expected" % simple_test)302        self.assertIsNotFile("%s.data/PassTest.test_1/stderr.expected" % simple_test)303        self.assertIsNotFile("%s.data/PassTest.test_2/stdout.expected" % simple_test)304        self.assertIsNotFile("%s.data/PassTest.test_2/stderr.expected" % simple_test)305        self.assertIsFile("%s.data/PassTest.test_2/bar/stderr.expected" % simple_test)306        self.assertIsFile("%s.data/PassTest.test_2/foo/stderr.expected" % simple_test)307    def test_merge_records_different_and_same_output(self):308        simple_test, variants_file = self._setup_simple_test(309            TEST_WITH_DIFFERENT_AND_SAME_EXPECTED_OUTPUT)310        cmd_line = ('%s run --job-results-dir %s --disable-sysinfo %s '311                    '--output-check-record both --json-variants-load %s' %312                    (AVOCADO, self.tmpdir.name, simple_test, variants_file))313        process.run(cmd_line, ignore_status=True)314        self.assertIsFile("%s.data/stdout.expected" % simple_test)315        self.assertIsFile("%s.data/stderr.expected" % simple_test)316        self.assertIsFile("%s.data/PassTest.test_1/stdout.expected" % simple_test)317        self.assertIsFile("%s.data/PassTest.test_1/stderr.expected" % simple_test)318        self.assertIsNotFile("%s.data/PassTest.test_2/stdout.expected" % simple_test)319        self.assertIsNotFile("%s.data/PassTest.test_2/stderr.expected" % simple_test)320    def tearDown(self):321        super(RunnerSimpleTest, self).tearDown()322        self.output_script.remove()323if __name__ == '__main__':...test_rsync.py
Source:test_rsync.py  
...10 11    def assertIsFile(self, path):12        if not pl.Path(path).resolve().is_file():13            raise AssertionError("File does not exist: {}".format(path))14    def assertIsNotFile(self, path):15        if pl.Path(path).resolve().is_file():16            raise AssertionError("File exist: {}".format(path))17    def assertIsDir(self, path):18        if not pl.Path(path).resolve().is_dir():19            raise AssertionError("Directory does not exist: {}".format(path))20    def assertIsNotDir(self, path):21        if pl.Path(path).resolve().is_dir():22            raise AssertionError("Directory exist: {}".format(path))23class TestRsync(TestCaseBase):24    @staticmethod25    def remove_files_and_dirs(folder):26        """Input dir (str) to remove all files and dir in that location."""27        for filename in os.listdir(folder):28            file_path = os.path.join(folder, filename)29            try:30                if os.path.isfile(file_path) or os.path.islink(file_path):31                    os.unlink(file_path)32                elif os.path.isdir(file_path):33                    shutil.rmtree(file_path)34            except Exception as e:35                print('Failed to delete %s. Reason: %s' % (file_path, e))36    @classmethod37    def setUpClass(cls): 38        try:39            f = open('test_source/file0.txt', 'x')40            f.close()41            f = open('test_source/file1.txt', 'x')42            f.close()43            f = open('test_source/file2.txt', 'x')44            f.close()45            f = open('test_source/file3.png', 'x')46            f.close()47            f = open('test_source/file4.png', 'x')48            f.close()49            os.makedirs('test_source/testdir0/')50            os.makedirs('test_source/testdir1/')51        except Exception:52            pass53    @classmethod54    def tearDownClass(cls):55        """Delete all files from source after tests."""56        folder = 'test_source/'57        cls.remove_files_and_dirs(folder)        58        59    def setUp(self):60        '''Delete all files from test_source/ before test.'''61        folder = 'test_dest/'62        self.remove_files_and_dirs(folder)63    def tearDown(self):64        '''Delete all files from test_source/ after test.'''65        folder = 'test_dest/'66        self.remove_files_and_dirs(folder)67    def test_single_file(self):68        # Testing single file transfer69        print("this test")70        rsync.main(['test_source/file0.txt', 'test_dest/'])71        path = pl.Path("test_dest/file0.txt")72        print("end of test")73        print()74        self.assertIsFile(path)75        76    def test_single_dir(self):77        # Testing single directory transfer with option78        rsync.main(['-d', 'test_source/testdir0/', 'test_dest/testdir0/'])79        path = pl.Path("test_dest/testdir0/")80        self.assertIsDir(path)81    def test_single_dir_no_flag(self):82        # Testing single file transfer without option83        rsync.main(['test_source/testdir0/', 'test_dest/testdir0/'])84        path = pl.Path("test_dest/testdir0/")85        self.assertIsNotDir(path)86    def test_non_exist_file(self):87        # Testing single non-existent file88        rsync.main(['test_source/file10.txt', 'test_dest/'])89        path = pl.Path("test_dest/file10.txt")90        self.assertIsNotFile(path)91    def test_non_exist_dir(self):92        # Testing single non-existent directory93        rsync.main(['test_source/testdir10', 'test_dest/'])94        path = pl.Path("test_dest/testdir10/")95        self.assertIsNotDir(path)96   97    def test_directory_to_file(self):98        # Test transferring an existent directory to a file - fails99        rsync.main(['test_source/testdir10/', 'test_dest/file0.txt'])100        path = pl.Path("test_dest/file0.txt")101        self.assertIsNotDir(path) 102        self.assertIsNotFile(path)103    def test_multiple_files(self):104        # Test mutliple files transfer105        rsync.main(['test_source/file0.txt', 'test_source/file1.txt', 'test_dest/'])106        path0 = pl.Path("test_dest/file0.txt")107        path1 = pl.Path("test_dest/file1.txt")108        self.assertIsFile(path0)109        self.assertIsFile(path1)110    def test_multiple_files_with_wildcard(self):111        # Test mutliple files transfer with wildcard112        # Note: glob is resolved in Bash not Python, so passing the string literal 'test_source/*' as113        # a test case will fail114        inputs = ['-d']115        inputs += glob.glob('test_source/*')116        inputs.append('test_dest/')117        rsync.main(inputs)118        path0 = pl.Path("test_dest/file0.txt")119        path1 = pl.Path("test_dest/file1.txt")120        path2 = pl.Path("test_dest/file2.txt")121        path3 = pl.Path("test_dest/file3.png")122        path4 = pl.Path("test_dest/file4.png")123        path5 = pl.Path("test_dest/testdir0/")124        path6 = pl.Path("test_dest/testdir1/")125        self.assertIsFile(path0)126        self.assertIsFile(path1)127        self.assertIsFile(path2)128        self.assertIsFile(path3)129        self.assertIsFile(path4)130        self.assertIsDir(path5)131        self.assertIsDir(path6)132    def test_multiple_files_with_extension(self):133        # Test mutliple files transfer with wildcard134        # Note: glob is resolved in Bash not Python, so passing the string literal 'test_source/*' as135        # a test case will fail136        inputs = ['-d']137        inputs += glob.glob('test_source/*.txt')138        inputs.append('test_dest/')139        rsync.main(inputs)140        path0 = pl.Path("test_dest/file0.txt")141        path1 = pl.Path("test_dest/file1.txt")142        path2 = pl.Path("test_dest/file2.txt")143        path3 = pl.Path("test_dest/file3.png")144        path4 = pl.Path("test_dest/file4.png")145        path5 = pl.Path("test_dest/testdir0/")146        path6 = pl.Path("test_dest/testdir1/")147        self.assertIsFile(path0)148        self.assertIsFile(path1)149        self.assertIsFile(path2)150        self.assertIsNotFile(path3)151        self.assertIsNotFile(path4)152        self.assertIsNotDir(path5)153        self.assertIsNotDir(path6)154if __name__ == "__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!!
