Best Python code snippet using avocado_python
diff_validator.py
Source:diff_validator.py
...18>>> change = diff_validator.Change()19>>> change.add_validated_files(["/etc/somerc"])20>>> change.append_expected_add("/etc/somerc", "this is a new line")21>>> change.append_expected_remove("/etc/somerc", "this line is removed")22>>> diff_validator.make_temp_file_copies(change.get_target_files())23After making changes through some in-test operation:24>>> changes = diff_validator.extract_changes(change.get_target_files())25>>> change_success = diff_validator.assert_change(changes, change.files_dict)26If test fails due to invalid change on the system:27>>> if not change_success:28>>> changes = diff_validator.assert_change_dict(changes, change.files_dict)29>>> raise DiffValidationError("Change is different than expected:\n%s" % diff_validator.create_diff_report(changes))30>>> else:31>>> logging.info("Change made successfully")32>>> diff_validator.del_temp_file_copies(change.get_target_files())33"""34import difflib35import os36import shutil37class DiffValidationError(Exception):38 pass39def get_temp_file_path(file_path):40 """41 Generates a temporary filename.42 :param str file_path: file path prefix43 :returns: appended file path44 :rtype: str45 """46 return file_path + '.tmp'47def make_temp_file_copies(file_paths):48 """49 Creates temporary copies of the provided files.50 :param file_paths: file paths to be copied51 :type file_paths: [str]52 """53 for file_path in file_paths:54 temp_file_path = get_temp_file_path(file_path)55 shutil.copyfile(file_path, temp_file_path)56def del_temp_file_copies(file_paths):57 """58 Deletes all the provided files.59 :param file_paths: deleted file paths (their temporary versions)60 :type file_paths: [str]61 """...
test_diff_validator.py
Source:test_diff_validator.py
...30 change.add_validated_files(files)31 change.append_expected_add(files[0], "this is a new line")32 change.append_expected_remove(files[0], "this line is removed")33 change.append_expected_add(files[1], "this is a new line again")34 diff_validator.make_temp_file_copies(change.get_target_files())35 with open(files[0], "w", encoding="utf-8") as f:36 f.write("this is a new line")37 with open(files[1], "w", encoding="utf-8") as f:38 f.write("this line is not removed\nthis is a new line again\n")39 changes = diff_validator.extract_changes(change.get_target_files())40 change_success = diff_validator.assert_change(changes, change.files_dict)41 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)42 self.assertTrue(43 change_success,44 f"The change must be valid:\n{diff_validator.create_diff_report(change_dict)}",45 )46 def test_change_wrong_no_change(self):47 files = self.files48 with open(files[0], "w", encoding="utf-8") as f:49 f.write("this line is removed\n")50 change = self.change51 change.add_validated_files(files)52 change.append_expected_add(files[0], "this is a new line")53 change.append_expected_remove(files[0], "this line is removed")54 diff_validator.make_temp_file_copies(change.get_target_files())55 changes = diff_validator.extract_changes(change.get_target_files())56 change_success = diff_validator.assert_change(changes, change.files_dict)57 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)58 self.assertFalse(59 change_success,60 f"The change must not be valid:\n{diff_validator.create_diff_report(change_dict)}",61 )62 def test_change_wrong_add(self):63 files = self.files64 with open(files[0], "w", encoding="utf-8") as f:65 f.write("this line is removed\n")66 change = self.change67 change.add_validated_files(files)68 change.append_expected_add(files[0], "this is a new line")69 change.append_expected_remove(files[0], "this line is removed")70 diff_validator.make_temp_file_copies(change.get_target_files())71 with open(files[0], "w", encoding="utf-8") as f:72 f.write("this is a wrong new line\n")73 changes = diff_validator.extract_changes(change.get_target_files())74 change_success = diff_validator.assert_change(changes, change.files_dict)75 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)76 self.assertFalse(77 change_success,78 f"The change must not be valid:\n{diff_validator.create_diff_report(change_dict)}",79 )80 def test_change_unexpected_remove(self):81 files = self.files82 with open(files[0], "w", encoding="utf-8") as f:83 f.write("this line is removed\n")84 change = self.change85 change.add_validated_files(files)86 change.append_expected_add(files[0], "this is a new line")87 diff_validator.make_temp_file_copies(change.get_target_files())88 with open(files[0], "w", encoding="utf-8") as f:89 f.write("this is a new line\n")90 changes = diff_validator.extract_changes(change.get_target_files())91 change_success = diff_validator.assert_change(changes, change.files_dict)92 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)93 self.assertFalse(94 change_success,95 f"The change must not be valid:\n{diff_validator.create_diff_report(change_dict)}",96 )97 def test_change_unexpected_add(self):98 files = self.files99 with open(files[0], "w", encoding="utf-8") as f:100 f.write("this line is removed\n")101 change = self.change102 change.add_validated_files(files)103 change.append_expected_remove(files[0], "this line is removed")104 diff_validator.make_temp_file_copies(change.get_target_files())105 with open(files[0], "w", encoding="utf-8") as f:106 f.write("this is an unexpected new line\n")107 changes = diff_validator.extract_changes(change.get_target_files())108 change_success = diff_validator.assert_change(changes, change.files_dict)109 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)110 self.assertFalse(111 change_success,112 f"The change must not be valid:\n{diff_validator.create_diff_report(change_dict)}",113 )114if __name__ == "__main__":...
test_utils_diff_validator.py
Source:test_utils_diff_validator.py
...28 change.add_validated_files(files)29 change.append_expected_add(files[0], "this is a new line")30 change.append_expected_remove(files[0], "this line is removed")31 change.append_expected_add(files[1], "this is a new line again")32 diff_validator.make_temp_file_copies(change.get_target_files())33 with open(files[0], "w") as f:34 f.write("this is a new line")35 with open(files[1], "w") as f:36 f.write("this line is not removed\nthis is a new line again\n")37 changes = diff_validator.extract_changes(change.get_target_files())38 change_success = diff_validator.assert_change(changes, change.files_dict)39 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)40 self.assertTrue(change_success, "The change must be valid:\n%s" % diff_validator.create_diff_report(change_dict))41 def test_change_wrong_no_change(self):42 files = self.files43 with open(files[0], "w") as f:44 f.write("this line is removed\n")45 change = self.change46 change.add_validated_files(files)47 change.append_expected_add(files[0], "this is a new line")48 change.append_expected_remove(files[0], "this line is removed")49 diff_validator.make_temp_file_copies(change.get_target_files())50 changes = diff_validator.extract_changes(change.get_target_files())51 change_success = diff_validator.assert_change(changes, change.files_dict)52 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)53 self.assertFalse(change_success, "The change must not be valid:\n%s" % diff_validator.create_diff_report(change_dict))54 def test_change_wrong_add(self):55 files = self.files56 with open(files[0], "w") as f:57 f.write("this line is removed\n")58 change = self.change59 change.add_validated_files(files)60 change.append_expected_add(files[0], "this is a new line")61 change.append_expected_remove(files[0], "this line is removed")62 diff_validator.make_temp_file_copies(change.get_target_files())63 with open(files[0], "w") as f:64 f.write("this is a wrong new line\n")65 changes = diff_validator.extract_changes(change.get_target_files())66 change_success = diff_validator.assert_change(changes, change.files_dict)67 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)68 self.assertFalse(change_success, "The change must not be valid:\n%s" % diff_validator.create_diff_report(change_dict))69 def test_change_unexpected_remove(self):70 files = self.files71 with open(files[0], "w") as f:72 f.write("this line is removed\n")73 change = self.change74 change.add_validated_files(files)75 change.append_expected_add(files[0], "this is a new line")76 diff_validator.make_temp_file_copies(change.get_target_files())77 with open(files[0], "w") as f:78 f.write("this is a new line\n")79 changes = diff_validator.extract_changes(change.get_target_files())80 change_success = diff_validator.assert_change(changes, change.files_dict)81 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)82 self.assertFalse(change_success, "The change must not be valid:\n%s" % diff_validator.create_diff_report(change_dict))83 def test_change_unexpected_add(self):84 files = self.files85 with open(files[0], "w") as f:86 f.write("this line is removed\n")87 change = self.change88 change.add_validated_files(files)89 change.append_expected_remove(files[0], "this line is removed")90 diff_validator.make_temp_file_copies(change.get_target_files())91 with open(files[0], "w") as f:92 f.write("this is an unexpected new line\n")93 changes = diff_validator.extract_changes(change.get_target_files())94 change_success = diff_validator.assert_change(changes, change.files_dict)95 change_dict = diff_validator.assert_change_dict(changes, change.files_dict)96 self.assertFalse(change_success, "The change must not be valid:\n%s" % diff_validator.create_diff_report(change_dict))97if __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!!