Best Python code snippet using avocado_python
test_loader.py
Source:test_loader.py
...114 def test(self):115 pass116"""117class LoaderTest(unittest.TestCase):118 def _check_discovery(self, exps, tests):119 self.assertEqual(len(exps), len(tests), "Total count of tests not "120 "as expected (%s != %s)\nexps: %s\ntests: %s"121 % (len(exps), len(tests), exps, tests))122 try:123 for exp, tst in zip(exps, tests):124 # Test class125 self.assertEqual(tst[0], exp[0])126 # Test name (path)127 # py2 reports relpath, py3 abspath128 self.assertEqual(os.path.abspath(tst[1]['name']),129 os.path.abspath(exp[1]))130 except AssertionError as details:131 raise AssertionError("%s\nexps: %s\ntests:%s"132 % (details, exps, tests))133 def setUp(self):134 self.loader = loader.FileLoader(None, {})135 prefix = temp_dir_prefix(__name__, self, 'setUp')136 self.tmpdir = tempfile.TemporaryDirectory(prefix=prefix)137 def test_load_simple(self):138 simple_test = script.TemporaryScript('simpletest.sh', SIMPLE_TEST,139 'avocado_loader_unittest')140 simple_test.save()141 test_class, test_parameters = (142 self.loader.discover(simple_test.path, loader.DiscoverMode.ALL)[0])143 self.assertTrue(test_class == test.SimpleTest, test_class)144 test_parameters['name'] = TestID(0, test_parameters['name'])145 test_parameters['base_logdir'] = self.tmpdir.name146 tc = test_class(**test_parameters)147 tc.run_avocado()148 suite = self.loader.discover(simple_test.path, loader.DiscoverMode.ALL)149 self.assertEqual(len(suite), 1)150 self.assertEqual(suite[0][1]["name"], simple_test.path)151 simple_test.remove()152 def test_load_simple_not_exec(self):153 simple_test = script.TemporaryScript('simpletest.sh', SIMPLE_TEST,154 'avocado_loader_unittest',155 mode=DEFAULT_NON_EXEC_MODE)156 simple_test.save()157 test_class, _ = self.loader.discover(simple_test.path, loader.DiscoverMode.ALL)[0]158 self.assertTrue(test_class == loader.NotATest, test_class)159 simple_test.remove()160 def test_load_pass(self):161 avocado_pass_test = script.TemporaryScript('passtest.py',162 AVOCADO_TEST_OK,163 'avocado_loader_unittest')164 avocado_pass_test.save()165 test_class, _ = self.loader.discover(avocado_pass_test.path,166 loader.DiscoverMode.ALL)[0]167 self.assertTrue(test_class == 'PassTest', test_class)168 avocado_pass_test.remove()169 def test_load_not_a_test(self):170 avocado_not_a_test = script.TemporaryScript('notatest.py',171 NOT_A_TEST,172 'avocado_loader_unittest',173 mode=DEFAULT_NON_EXEC_MODE)174 avocado_not_a_test.save()175 test_class, _ = self.loader.discover(avocado_not_a_test.path,176 loader.DiscoverMode.ALL)[0]177 self.assertTrue(test_class == loader.NotATest, test_class)178 avocado_not_a_test.remove()179 def test_load_not_a_test_exec(self):180 avocado_not_a_test = script.TemporaryScript('notatest.py', NOT_A_TEST,181 'avocado_loader_unittest')182 avocado_not_a_test.save()183 test_class, test_parameters = (184 self.loader.discover(avocado_not_a_test.path, loader.DiscoverMode.ALL)[0])185 self.assertTrue(test_class == test.SimpleTest, test_class)186 test_parameters['name'] = TestID(0, test_parameters['name'])187 test_parameters['base_logdir'] = self.tmpdir.name188 tc = test_class(**test_parameters)189 # The test can't be executed (no shebang), raising an OSError190 # (OSError: [Errno 8] Exec format error)191 self.assertRaises(OSError, tc.test)192 avocado_not_a_test.remove()193 def test_py_simple_test(self):194 with script.TemporaryScript(195 'simpletest.py',196 PY_SIMPLE_TEST,197 'avocado_loader_unittest') as avocado_simple_test:198 test_class, test_parameters = (199 self.loader.discover(avocado_simple_test.path, loader.DiscoverMode.ALL)[0])200 self.assertTrue(test_class == test.SimpleTest)201 test_parameters['name'] = TestID(0, test_parameters['name'])202 test_parameters['base_logdir'] = self.tmpdir.name203 tc = test_class(**test_parameters)204 tc.run_avocado()205 def test_py_simple_test_notexec(self):206 with script.TemporaryScript(207 'simpletest.py',208 PY_SIMPLE_TEST,209 'avocado_loader_unittest',210 mode=DEFAULT_NON_EXEC_MODE) as avocado_simple_test:211 test_class, _ = self.loader.discover(avocado_simple_test.path,212 loader.DiscoverMode.ALL)[0]213 self.assertTrue(test_class == loader.NotATest)214 def test_multiple_methods(self):215 avocado_multiple_tests = script.TemporaryScript('multipletests.py',216 AVOCADO_MULTIPLE_TESTS,217 'avocado_multiple_tests_unittest',218 mode=DEFAULT_NON_EXEC_MODE)219 avocado_multiple_tests.save()220 suite = self.loader.discover(avocado_multiple_tests.path, loader.DiscoverMode.ALL)221 self.assertEqual(len(suite), 2)222 # Try to load only some of the tests223 suite = self.loader.discover(avocado_multiple_tests.path +224 ':MultipleMethods.testTwo', loader.DiscoverMode.ALL)225 self.assertEqual(len(suite), 1)226 self.assertEqual(suite[0][1]["methodName"], 'testTwo')227 # Load using regex228 suite = self.loader.discover(avocado_multiple_tests.path +229 ':.*_one', loader.DiscoverMode.ALL)230 self.assertEqual(len(suite), 1)231 self.assertEqual(suite[0][1]["methodName"], 'test_one')232 # Load booth233 suite = self.loader.discover(avocado_multiple_tests.path +234 ':test.*', loader.DiscoverMode.ALL)235 self.assertEqual(len(suite), 2)236 # Load none should return no tests237 self.assertTrue(not self.loader.discover(avocado_multiple_tests.path +238 ":no_match", loader.DiscoverMode.ALL))239 avocado_multiple_tests.remove()240 def test_multiple_methods_same_name(self):241 avocado_multiple_tests = script.TemporaryScript('multipletests.py',242 AVOCADO_MULTIPLE_TESTS_SAME_NAME,243 'avocado_multiple_tests_unittest',244 mode=DEFAULT_NON_EXEC_MODE)245 avocado_multiple_tests.save()246 suite = self.loader.discover(avocado_multiple_tests.path, loader.DiscoverMode.ALL)247 self.assertEqual(len(suite), 1)248 # Try to load only some of the tests249 suite = self.loader.discover(avocado_multiple_tests.path +250 ':MultipleMethods.test', loader.DiscoverMode.ALL)251 self.assertEqual(len(suite), 1)252 self.assertEqual(suite[0][1]["methodName"], 'test')253 avocado_multiple_tests.remove()254 def test_load_foreign(self):255 avocado_pass_test = script.TemporaryScript('foreign.py',256 AVOCADO_FOREIGN_TAGGED_ENABLE,257 'avocado_loader_unittest')258 avocado_pass_test.save()259 test_class, _ = (260 self.loader.discover(avocado_pass_test.path, loader.DiscoverMode.ALL)[0])261 self.assertTrue(test_class == 'First', test_class)262 avocado_pass_test.remove()263 def test_load_pass_disable(self):264 avocado_pass_test = script.TemporaryScript('disable.py',265 AVOCADO_TEST_OK_DISABLED,266 'avocado_loader_unittest',267 DEFAULT_NON_EXEC_MODE)268 avocado_pass_test.save()269 test_class, _ = (270 self.loader.discover(avocado_pass_test.path, loader.DiscoverMode.ALL)[0])271 self.assertTrue(test_class == loader.NotATest)272 avocado_pass_test.remove()273 def test_load_tagged_nested(self):274 avocado_nested_test = script.TemporaryScript('nested.py',275 AVOCADO_TEST_NESTED_TAGGED,276 'avocado_loader_unittest',277 DEFAULT_NON_EXEC_MODE)278 avocado_nested_test.save()279 test_class, _ = self.loader.discover(avocado_nested_test.path,280 loader.DiscoverMode.ALL)[0]281 self.assertTrue(test_class == loader.NotATest)282 avocado_nested_test.remove()283 def test_load_multiple_imports(self):284 avocado_multiple_imp_test = script.TemporaryScript(285 'multipleimports.py',286 AVOCADO_TEST_MULTIPLE_IMPORTS,287 'avocado_loader_unittest')288 avocado_multiple_imp_test.save()289 test_class, _ = (290 self.loader.discover(avocado_multiple_imp_test.path, loader.DiscoverMode.ALL)[0])291 self.assertTrue(test_class == 'Second', test_class)292 avocado_multiple_imp_test.remove()293 def test_python_unittest(self):294 disabled_test = script.TemporaryScript("disabled.py",295 AVOCADO_TEST_OK_DISABLED,296 mode=DEFAULT_NON_EXEC_MODE)297 python_unittest = script.TemporaryScript("python_unittest.py",298 PYTHON_UNITTEST)299 disabled_test.save()300 python_unittest.save()301 tests = self.loader.discover(disabled_test.path)302 self.assertEqual(tests, [])303 tests = self.loader.discover(python_unittest.path)304 exp = [(test.PythonUnittest,305 {"name": "python_unittest.SampleTest.test",306 "tags": {"flattag": None, "foo": {"bar"}},307 "test_dir": os.path.dirname(python_unittest.path)})]308 self.assertEqual(tests, exp)309 def test_mod_import_and_classes(self):310 path = os.path.join(os.path.dirname(os.path.dirname(__file__)),311 '.data', 'loader_instrumented', 'dont_crash.py')312 tests = self.loader.discover(path)313 exps = [('DiscoverMe', 'selftests/.data/loader_instrumented/dont_crash.py:DiscoverMe.test'),314 ('DiscoverMe2', 'selftests/.data/loader_instrumented/dont_crash.py:DiscoverMe2.test'),315 ('DiscoverMe3', 'selftests/.data/loader_instrumented/dont_crash.py:DiscoverMe3.test'),316 ('DiscoverMe4', 'selftests/.data/loader_instrumented/dont_crash.py:DiscoverMe4.test')]317 self._check_discovery(exps, tests)318 def test_imports(self):319 path = os.path.join(os.path.dirname(os.path.dirname(__file__)),320 '.data', 'loader_instrumented', 'imports.py')321 tests = self.loader.discover(path)322 exps = [('Test1', 'selftests/.data/loader_instrumented/imports.py:Test1.test'),323 ('Test3', 'selftests/.data/loader_instrumented/imports.py:Test3.test'),324 ('Test4', 'selftests/.data/loader_instrumented/imports.py:Test4.test'),325 ('Test5', 'selftests/.data/loader_instrumented/imports.py:Test5.test'),326 ('Test6', 'selftests/.data/loader_instrumented/imports.py:Test6.test'),327 ('Test8', 'selftests/.data/loader_instrumented/imports.py:Test8.test'),328 ('Test9', 'selftests/.data/loader_instrumented/imports.py:Test9.test'),329 ('Test10', 'selftests/.data/loader_instrumented/imports.py:Test10.test')]330 self._check_discovery(exps, tests)331 def test_dont_detect_non_avocado(self):332 path = os.path.join(os.path.dirname(os.path.dirname(__file__)),333 '.data', 'loader_instrumented', 'dont_detect_non_avocado.py')334 tests = self.loader.discover(path)335 self._check_discovery([], tests)336 def test_infinite_recurse(self):337 """Checks we don't crash on infinite recursion"""338 path = os.path.join(os.path.dirname(os.path.dirname(__file__)),339 '.data', 'loader_instrumented',340 'infinite_recurse.py')341 tests = self.loader.discover(path)342 self.assertEqual(tests, [])343 def test_double_import(self):344 path = os.path.join(os.path.dirname(os.path.dirname(__file__)),345 '.data', 'loader_instrumented', 'double_import.py')346 tests = self.loader.discover(path)347 exps = [('Test1', 'selftests/.data/loader_instrumented/double_import.py:Test1.test1'),348 ('Test2', 'selftests/.data/loader_instrumented/double_import.py:Test2.test2'),349 ('Test3', 'selftests/.data/loader_instrumented/double_import.py:Test3.test3'),350 ('Test4', 'selftests/.data/loader_instrumented/double_import.py:Test4.test4')]351 self._check_discovery(exps, tests)352 def test_list_raising_exception(self):353 with script.TemporaryScript('simpletest.py', PY_SIMPLE_TEST) as simple_test:354 with unittest.mock.patch('avocado.core.loader.safeloader.find_avocado_tests') as _mock:355 _mock.side_effect = BaseException()356 tests = self.loader.discover(simple_test.path)357 self.assertEqual(tests[0][1]["name"], simple_test.path)358 def tearDown(self):359 self.tmpdir.cleanup()360if __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!!