Best Python code snippet using Airtest
test_resource_path.py
Source:test_resource_path.py
1import sublime2import shutil3import tempfile4from sublime_lib import ResourcePath5from sublime_lib._compat.pathlib import Path6from unittesting import DeferrableTestCase7class TestResourcePath(DeferrableTestCase):8 def setUp(self):9 shutil.copytree(10 src=str(ResourcePath("Packages/sublime_lib/tests/test_package").file_path()),11 dst=str(ResourcePath("Packages/test_package").file_path()),12 )13 yield ResourcePath("Packages/test_package/.test_package_exists").exists14 def tearDown(self):15 shutil.rmtree(16 str(ResourcePath("Packages/test_package").file_path()),17 ignore_errors=True18 )19 def test_glob_resources(self):20 self.assertEqual(21 ResourcePath.glob_resources("Packages/test_package/*.txt"),22 [23 ResourcePath("Packages/test_package/helloworld.txt"),24 ResourcePath("Packages/test_package/UTF-8-test.txt"),25 ]26 )27 self.assertEqual(28 ResourcePath.glob_resources("ks27jArEz4"),29 []30 )31 self.assertEqual(32 ResourcePath.glob_resources("*ks27jArEz4"),33 [34 ResourcePath('Packages/sublime_lib/tests/uniquely_named_file_ks27jArEz4')35 ]36 )37 def test_from_file_path_packages(self):38 self.assertEqual(39 ResourcePath.from_file_path(Path(sublime.packages_path(), 'test_package')),40 ResourcePath("Packages/test_package")41 )42 def test_from_file_path_cache(self):43 self.assertEqual(44 ResourcePath.from_file_path(Path(sublime.cache_path(), 'test_package')),45 ResourcePath("Cache/test_package")46 )47 def test_from_file_path_installed_packages(self):48 self.assertEqual(49 ResourcePath.from_file_path(50 Path(sublime.installed_packages_path(), 'test_package.sublime-package', 'foo.py')51 ),52 ResourcePath("Packages/test_package/foo.py")53 )54 def test_from_file_path_installed_packages_not_installed(self):55 with self.assertRaises(ValueError):56 ResourcePath.from_file_path(57 Path(sublime.installed_packages_path(), 'test_package', 'foo.py')58 ),59 def test_from_file_path_installed_packages_root(self):60 self.assertEqual(61 ResourcePath.from_file_path(Path(sublime.installed_packages_path())),62 ResourcePath("Packages")63 )64 def test_from_file_path_default_packages(self):65 self.assertEqual(66 ResourcePath.from_file_path(67 Path(sublime.executable_path()).parent.joinpath(68 'Packages', 'test_package.sublime-package', 'foo.py'69 )70 ),71 ResourcePath("Packages/test_package/foo.py")72 )73 def test_from_file_path_default_packages_root(self):74 self.assertEqual(75 ResourcePath.from_file_path(76 Path(sublime.executable_path()).parent / 'Packages'77 ),78 ResourcePath("Packages")79 )80 def test_from_file_path_error(self):81 with self.assertRaises(ValueError):82 ResourcePath.from_file_path(Path('/test_package')),83 def test_from_file_path_relative(self):84 with self.assertRaises(ValueError):85 ResourcePath.from_file_path(Path('test_package')),86 def test_file_path_packages(self):87 self.assertEqual(88 ResourcePath("Packages/Foo/bar.py").file_path(),89 Path(sublime.packages_path(), 'Foo/bar.py')90 )91 def test_file_path_packages_root(self):92 self.assertEqual(93 ResourcePath("Packages").file_path(),94 Path(sublime.packages_path())95 )96 def test_file_path_cache(self):97 self.assertEqual(98 ResourcePath("Cache/Foo/bar.py").file_path(),99 Path(sublime.cache_path(), 'Foo/bar.py')100 )101 def test_file_path_error(self):102 with self.assertRaises(ValueError):103 ResourcePath("Elsewhere/Foo/bar.py").file_path(),104 def test_exists(self):105 self.assertTrue(106 ResourcePath("Packages/test_package/helloworld.txt").exists()107 )108 def test_not_exists(self):109 self.assertFalse(110 ResourcePath("Packages/test_package/nonexistentfile.txt").exists()111 )112 def test_read_text(self):113 self.assertEqual(114 ResourcePath("Packages/test_package/helloworld.txt").read_text(),115 "Hello, World!\n"116 )117 def test_read_text_missing(self):118 with self.assertRaises(FileNotFoundError):119 ResourcePath("Packages/test_package/nonexistentfile.txt").read_text()120 def test_read_text_invalid_unicode(self):121 with self.assertRaises(UnicodeDecodeError):122 ResourcePath("Packages/test_package/UTF-8-test.txt").read_text()123 def test_read_bytes(self):124 self.assertEqual(125 ResourcePath("Packages/test_package/helloworld.txt").read_bytes(),126 b"Hello, World!\n"127 )128 def test_read_bytes_missing(self):129 with self.assertRaises(FileNotFoundError):130 ResourcePath("Packages/test_package/nonexistentfile.txt").read_bytes()131 def test_read_bytes_invalid_unicode(self):132 # Should not raise UnicodeDecodeError133 ResourcePath("Packages/test_package/UTF-8-test.txt").read_bytes()134 def test_glob(self):135 self.assertEqual(136 ResourcePath("Packages/test_package").glob('*.txt'),137 [138 ResourcePath("Packages/test_package/helloworld.txt"),139 ResourcePath("Packages/test_package/UTF-8-test.txt"),140 ]141 )142 def test_rglob(self):143 self.assertEqual(144 ResourcePath("Packages/test_package").rglob('*.txt'),145 [146 ResourcePath("Packages/test_package/helloworld.txt"),147 ResourcePath("Packages/test_package/UTF-8-test.txt"),148 ResourcePath("Packages/test_package/directory/goodbyeworld.txt"),149 ]150 )151 def test_rglob_error(self):152 with self.assertRaises(NotImplementedError):153 ResourcePath("Packages/test_package").rglob('/*.txt')154 def test_children(self):155 self.assertEqual(156 ResourcePath("Packages/test_package").children(),157 [158 ResourcePath("Packages/test_package/.test_package_exists"),159 ResourcePath("Packages/test_package/helloworld.txt"),160 ResourcePath("Packages/test_package/UTF-8-test.txt"),161 ResourcePath("Packages/test_package/directory"),162 ]163 )164 def test_copy_text(self):165 with tempfile.TemporaryDirectory() as directory:166 source = ResourcePath("Packages/test_package/helloworld.txt")167 destination = Path(directory) / 'helloworld.txt'168 source.copy(destination)169 self.assertTrue(destination.is_file())170 with open(str(destination), 'r') as file:171 text = file.read()172 self.assertEqual(text, source.read_text())173 def test_copy_binary(self):174 with tempfile.TemporaryDirectory() as directory:175 source = ResourcePath("Packages/test_package/UTF-8-test.txt")176 destination = Path(directory) / 'UTF-8-test.txt'177 source.copy(destination)178 self.assertTrue(destination.is_file())179 with open(str(destination), 'rb') as file:180 data = file.read()181 self.assertEqual(data, source.read_bytes())182 def test_copy_existing(self):183 with tempfile.TemporaryDirectory() as directory:184 source = ResourcePath("Packages/test_package/helloworld.txt")185 destination = Path(directory) / 'helloworld.txt'186 with open(str(destination), 'w') as file:187 file.write("Nothing to see here.\n")188 source.copy(destination)189 self.assertTrue(destination.is_file())190 with open(str(destination), 'r') as file:191 text = file.read()192 self.assertEqual(text, source.read_text())193 def test_copy_existing_error(self):194 with tempfile.TemporaryDirectory() as directory:195 source = ResourcePath("Packages/test_package/helloworld.txt")196 destination = Path(directory) / 'helloworld.txt'197 text = "Nothing to see here.\n"198 with open(str(destination), 'w') as file:199 file.write(text)200 with self.assertRaises(FileExistsError):201 source.copy(destination, False)202 def test_copy_directory_error(self):203 with tempfile.TemporaryDirectory() as directory:204 source = ResourcePath("Packages/test_package/helloworld.txt")205 destination = Path(directory) / 'helloworld.txt'206 destination.mkdir()207 with self.assertRaises(IsADirectoryError):208 source.copy(destination)209 self.assertTrue(destination.is_dir())210 def test_copytree(self):211 with tempfile.TemporaryDirectory() as directory:212 source = ResourcePath("Packages/test_package")213 destination = Path(directory) / 'tree'214 source.copytree(destination)215 self.assertEqual(216 {217 path.relative_to(destination).parts218 for path in destination.rglob('*')219 if path.is_file()220 },221 {222 path.relative_to(source)223 for path in source.rglob('*')224 }225 )226 def test_copytree_exists_error(self):227 with tempfile.TemporaryDirectory() as directory:228 source = ResourcePath("Packages/test_package")229 destination = Path(directory) / 'tree'230 destination.mkdir()231 with self.assertRaises(FileExistsError):232 source.copytree(destination)233 def test_copytree_exists(self):234 with tempfile.TemporaryDirectory() as directory:235 source = ResourcePath("Packages/test_package")236 destination = Path(directory) / 'tree'237 destination.mkdir()238 helloworld_file = destination / 'helloworld.txt'239 with open(str(helloworld_file), 'w') as file:240 file.write("Nothing to see here.\n")241 source.copytree(destination, exist_ok=True)242 self.assertEqual(243 {244 path.relative_to(destination).parts245 for path in destination.rglob('*')246 if path.is_file()247 },248 {249 path.relative_to(source)250 for path in source.rglob('*')251 }252 )253 with open(str(helloworld_file)) as file:254 helloworld_contents = file.read()255 self.assertEqual(256 helloworld_contents,257 (source / 'helloworld.txt').read_text()...
test_generator.py
Source:test_generator.py
1import unittest2import os3import shutil4from crudgen.code_generation.packages import generator5class TestPackageGenerator(unittest.TestCase):6 def test_create_package(self):7 """8 Test single package generation9 Should create a directory & __init__.py file10 inside11 """12 test_package = "test_package"13 generator.create_package(test_package, True, "")14 # Check test_package has been created15 elem_in_dir = os.listdir()16 self.assertTrue(test_package in elem_in_dir)17 # Check test_package contains __init__.py18 files_in_test_package = os.listdir(test_package)19 self.assertTrue("__init__.py" in files_in_test_package)20 shutil.rmtree("test_package")21 def test_create_api_structure(self):22 """23 Test full api structure generation24 Generated structure should contains all packages25 defined inside code_generation.PACKAGES26 """27 generator.create_api_structure("generated_api")28 files_in_generated = os.listdir("generated_api")29 [self.assertTrue(package in files_in_generated) for package in generator.PACKAGES]...
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!!