How to use find_avocado_tests method in avocado

Best Python code snippet using avocado_python

test_safeloader_core.py

Source:test_safeloader_core.py Github

copy

Full Screen

...166 "keepmethodsorder.py", KEEP_METHODS_ORDER167 )168 avocado_keep_methods_order.save()169 expected_order = ["test2", "testA", "test1", "testZZZ", "test"]170 tests = find_avocado_tests(avocado_keep_methods_order.path)[0]171 methods = [method[0] for method in tests["MyClass"]]172 self.assertEqual(expected_order, methods)173 avocado_keep_methods_order.remove()174 def test_import_not_on_parent(self):175 avocado_test = script.TemporaryScript(176 "import_not_not_parent_test.py", IMPORT_NOT_NOT_PARENT_TEST177 )178 avocado_test.save()179 expected = ["test_something"]180 tests = find_avocado_tests(avocado_test.path)[0]181 methods = [method[0] for method in tests["SomeClass"]]182 self.assertEqual(expected, methods)183 avocado_test.remove()184 def test_recursive_discovery(self):185 avocado_recursive_discovery_test1 = script.TemporaryScript(186 "recursive_discovery_test1.py", RECURSIVE_DISCOVERY_TEST1187 )188 avocado_recursive_discovery_test1.save()189 avocado_recursive_discovery_test2 = script.TemporaryScript(190 "recursive_discovery_test2.py", RECURSIVE_DISCOVERY_TEST2191 )192 avocado_recursive_discovery_test2.save()193 sys.path.append(os.path.dirname(avocado_recursive_discovery_test1.path))194 tests = find_avocado_tests(avocado_recursive_discovery_test2.path)[0]195 expected = {196 "ThirdChild": [197 ("test_third_child", {}, []),198 ("test_second_child", {}, []),199 ("test_first_child", {}, []),200 ("test_basic", {}, []),201 ]202 }203 self.assertEqual(expected, tests)204 def test_recursive_discovery_python_unittest(self):205 temp_test = script.TemporaryScript(206 "recursive_discovery_python_unittest.py",207 RECURSIVE_DISCOVERY_PYTHON_UNITTEST,208 )209 temp_test.save()210 tests = find_python_unittests(temp_test.path)211 expected = {212 "BaseClass": [213 (214 "test_maybe_replaced_by_child",215 {"base-tag": None, "base.tag": None},216 [],217 ),218 ("test_basic", {"base-tag": None, "base.tag": None}, []),219 ],220 "Child": [221 (222 "test_maybe_replaced_by_child",223 {"child-tag": None, "child.tag": None},224 [],225 ),226 ("test_child", {"child-tag": None, "child.tag": None}, []),227 ("test_basic", {"base-tag": None, "base.tag": None}, []),228 ],229 }230 self.assertEqual(expected, tests)231class MultiLevel(TestCaseTmpDir):232 def setUp(self):233 super().setUp()234 init = script.Script(235 os.path.join(self.tmpdir.name, "__init__.py"),236 "",237 mode=script.READ_ONLY_MODE,238 )239 init.save()240 l0 = script.Script(241 os.path.join(self.tmpdir.name, "l0lib.py"),242 L0_LIB,243 mode=script.READ_ONLY_MODE,244 )245 l0.save()246 l1_dir = os.path.join(self.tmpdir.name, "l1")247 os.mkdir(l1_dir)248 l11 = script.Script(249 os.path.join(l1_dir, "l1lib1.py"), L1_LIB1, mode=script.READ_ONLY_MODE250 )251 l11.save()252 l12 = script.Script(253 os.path.join(l1_dir, "l1lib2.py"), L1_LIB2, mode=script.READ_ONLY_MODE254 )255 l12.save()256 l2_dir = os.path.join(l1_dir, "l2")257 os.mkdir(l2_dir)258 l21 = script.Script(259 os.path.join(l2_dir, "l2lib1.py"), L2_LIB1, mode=script.READ_ONLY_MODE260 )261 l21.save()262 l22 = script.Script(263 os.path.join(l2_dir, "l2lib2.py"), L2_LIB2, mode=script.READ_ONLY_MODE264 )265 l22.save()266 l23 = script.Script(267 os.path.join(l2_dir, "l2lib3.py"), L2_LIB3, mode=script.READ_ONLY_MODE268 )269 l23.save()270 def test_base_level0(self):271 path = os.path.join(self.tmpdir.name, "l0lib.py")272 self.assertEqual(find_avocado_tests(path)[0], {"BaseL0": [("test_l0", {}, [])]})273 def test_relative_level0_name_from_level1(self):274 path = os.path.join(self.tmpdir.name, "l1", "l1lib1.py")275 self.assertEqual(276 find_avocado_tests(path)[0],277 {"BaseL1": [("test_l1", {}, []), ("test_l0", {}, [])]},278 )279 def test_relative_level0_from_level1(self):280 path = os.path.join(self.tmpdir.name, "l1", "l1lib2.py")281 self.assertEqual(282 find_avocado_tests(path)[0],283 {"BaseL1": [("test_l1", {}, []), ("test_l0", {}, [])]},284 )285 def test_relative_level0_name_from_level2(self):286 path = os.path.join(self.tmpdir.name, "l1", "l2", "l2lib1.py")287 self.assertEqual(288 find_avocado_tests(path)[0],289 {"BaseL2": [("test_l2", {}, []), ("test_l0", {}, [])]},290 )291 def test_relative_level0_from_level2(self):292 path = os.path.join(self.tmpdir.name, "l1", "l2", "l2lib2.py")293 self.assertEqual(294 find_avocado_tests(path)[0],295 {"BaseL2": [("test_l2", {}, []), ("test_l0", {}, [])]},296 )297 def test_non_relative_level0_from_level2(self):298 path = os.path.join(self.tmpdir.name, "l1", "l2", "l2lib3.py")299 sys_path = sys.path + [self.tmpdir.name]300 with unittest.mock.patch("sys.path", sys_path):301 self.assertEqual(302 find_avocado_tests(path)[0],303 {"BaseL2": [("test_l3", {}, []), ("test_l0", {}, [])]},304 )305if __name__ == "__main__":...

Full Screen

Full Screen

test_plugin_assets.py

Source:test_plugin_assets.py Github

copy

Full Screen

1"""2Assets plugin unit tests3"""4import ast5import unittest6from unittest.mock import mock_open, patch7from avocado.plugins import assets8class AssetsPlugin(unittest.TestCase):9 """10 Unit tests for Assets Plugin11 """12 @patch('avocado.plugins.assets.FetchAssetHandler')13 def test_fetch_assets_sucess_fail(self, mocked_fetch_asset_handler):14 """15 Exercise a normal fetch for a success and a fail assets.16 """17 mocked_fetch_asset_handler.return_value.calls = [18 {'name': 'success.tar.gz',19 'locations': 'https://localhost/success.tar.gz',20 'asset_hash': None, 'algorithm': None, 'expire': None},21 {'name': 'fail.tar.gz',22 'locations': 'https://localhost/fail.tar.gz',23 'asset_hash': None, 'algorithm': None, 'expire': None},24 ]25 with patch('avocado.plugins.assets.Asset') as mocked_asset:26 mocked_asset.return_value.fetch.side_effect = [27 True,28 OSError('Failed to fetch fail.tar.gz.')]29 success, fail = assets.fetch_assets('test.py')30 expected_success = ['success.tar.gz']31 expected_fail_exception = OSError32 self.assertEqual(expected_success, success)33 self.assertTrue(isinstance(fail[0], expected_fail_exception))34 @patch('avocado.plugins.assets.FetchAssetHandler')35 def test_fetch_assets_sucess(self, mocked_fetch_asset_handler):36 """37 Exercise a normal fetch for a success asset.38 """39 mocked_fetch_asset_handler.return_value.calls = [40 {'name': 'success.tar.gz',41 'locations': 'https://localhost/success.tar.gz',42 'asset_hash': None, 'algorithm': None, 'expire': None},43 ]44 with patch('avocado.plugins.assets.Asset') as mocked_asset:45 mocked_asset.return_value.fetch.side_effect = [46 True]47 success, fail = assets.fetch_assets('test.py')48 expected_success = ['success.tar.gz']49 expected_fail = []50 self.assertEqual(expected_success, success)51 self.assertEqual(fail, expected_fail)52 @patch('avocado.plugins.assets.FetchAssetHandler')53 def test_fetch_assets_fail(self, mocked_fetch_asset_handler):54 """55 Exercise a normal fetch for a fail asset.56 """57 mocked_fetch_asset_handler.return_value.calls = [58 {'name': 'fail.tar.gz',59 'locations': 'https://localhost/fail.tar.gz',60 'asset_hash': None, 'algorithm': None, 'expire': None},61 ]62 with patch('avocado.plugins.assets.Asset') as mocked_asset:63 mocked_asset.return_value.fetch.side_effect = [64 OSError('Failed to fetch fail.tar.gz.')]65 success, fail = assets.fetch_assets('test.py')66 expected_success = []67 expected_fail_exception = OSError68 self.assertEqual(expected_success, success)69 self.assertTrue(isinstance(fail[0], expected_fail_exception))70 @patch('avocado.plugins.assets.FetchAssetHandler')71 def test_fetch_assets_empty_calls(self, mocked_fetch_asset_handler):72 """73 Exercise a normal fetch_assets for an empty `calls` variable.74 """75 mocked_fetch_asset_handler.return_value.calls = []76 success, fail = assets.fetch_assets('test.py')77 expected_success = []78 expected_fail = []79 self.assertEqual(expected_success, success)80 self.assertEqual(expected_fail, fail)81TEST_CLASS_SOURCE = r"""82from avocado import Test83class FetchAssets(Test):84 def test_fetch_assets(self):85 foo = "bar"86"""87NOT_TEST_CLASS_SOURCE = r"""88from avocado import Test89class FetchAssets:90 def test_fetch_assets(self):91 foo = "bar"92"""93class AssetsClass(unittest.TestCase):94 """95 Unit tests for Asset Class96 """97 @patch('avocado.plugins.assets.safeloader')98 def test_visit_classdef_valid_class(self, mocked_safeloader):99 """100 Make sure that current_klass is correctly assigned with a class name101 """102 mocked_safeloader.find_avocado_tests.return_value = [103 'FetchAssets'104 ]105 tree = ast.parse(TEST_CLASS_SOURCE)106 node = tree.body[1]107 with patch("builtins.open", mock_open(read_data=TEST_CLASS_SOURCE)):108 with patch.object(assets.ast, "parse"):109 with patch.object(assets.FetchAssetHandler, "visit"):110 with patch.object(assets.FetchAssetHandler,111 "generic_visit"):112 handler = assets.FetchAssetHandler("fake_file.py")113 handler.visit_ClassDef(node)114 self.assertEqual(handler.current_klass, 'FetchAssets')115 @patch('avocado.plugins.assets.safeloader')116 def test_visit_classdef_invalid_class(self, mocked_safeloader):117 """118 Make sure that current_klass is not assigned with a class name119 """120 mocked_safeloader.find_avocado_tests.return_value = [[]]121 tree = ast.parse(NOT_TEST_CLASS_SOURCE)122 node = tree.body[1]123 with patch("builtins.open", mock_open(read_data=NOT_TEST_CLASS_SOURCE)):124 with patch.object(assets.ast, "parse"):125 with patch.object(assets.FetchAssetHandler, "visit"):126 with patch.object(assets.FetchAssetHandler,127 "generic_visit"):128 handler = assets.FetchAssetHandler("fake_file.py")129 handler.visit_ClassDef(node)130 self.assertTrue((handler.current_klass is None))131 @patch('avocado.plugins.assets.safeloader')132 def test_visit_fuctiondef_valid_class(self, mocked_safeloader):133 """134 Make sure that current_klass is correctly assigned with a class name135 """136 mocked_safeloader.find_avocado_tests.return_value = [137 'FetchAssets'138 ]139 tree = ast.parse(TEST_CLASS_SOURCE)140 node_class = tree.body[1]141 node_function = tree.body[1].body[0]142 expected_method = "test_fetch_assets"143 with patch("builtins.open", mock_open(read_data=TEST_CLASS_SOURCE)):144 with patch.object(assets.ast, "parse"):145 with patch.object(assets.FetchAssetHandler, "visit"):146 with patch.object(assets.FetchAssetHandler,147 "generic_visit"):148 handler = assets.FetchAssetHandler("fake_file.py")149 handler.visit_ClassDef(node_class)150 handler.visit_FunctionDef(node_function)151 self.assertEqual(handler.current_method, expected_method)152 @patch('avocado.plugins.assets.safeloader')153 def test_visit_fuctiondef_invalid_class(self, mocked_safeloader):154 """155 Make sure that current_klass is not assigned with a class name156 """157 mocked_safeloader.find_avocado_tests.return_value = [[]]158 tree = ast.parse(NOT_TEST_CLASS_SOURCE)159 node = tree.body[1].body[0]160 with patch("builtins.open", mock_open(read_data=NOT_TEST_CLASS_SOURCE)):161 with patch.object(assets.ast, "parse"):162 with patch.object(assets.FetchAssetHandler, "visit"):163 with patch.object(assets.FetchAssetHandler,164 "generic_visit"):165 handler = assets.FetchAssetHandler("fake_file.py")166 handler.visit_FunctionDef(node)167 self.assertTrue((handler.current_method is None))168 @patch('avocado.plugins.assets.safeloader')169 def test_visit_assign_valid_class_method(self, mocked_safeloader):170 """171 Make sure that current_klass is correctly assigned with a class name172 """173 mocked_safeloader.find_avocado_tests.return_value = [174 'FetchAssets'175 ]176 tree = ast.parse(TEST_CLASS_SOURCE)177 node_class = tree.body[1]178 node_function = tree.body[1].body[0]179 node_assign = tree.body[1].body[0].body[0]180 expected_assign = "bar"181 with patch("builtins.open", mock_open(read_data=TEST_CLASS_SOURCE)):182 with patch.object(assets.ast, "parse"):183 with patch.object(assets.FetchAssetHandler, "visit"):184 with patch.object(assets.FetchAssetHandler,185 "generic_visit"):186 handler = assets.FetchAssetHandler("fake_file.py")187 handler.visit_ClassDef(node_class)188 handler.visit_FunctionDef(node_function)189 handler.visit_Assign(node_assign)190 self.assertEqual(191 (handler.asgmts[handler.current_klass]192 [handler.current_method]['foo']),193 expected_assign)194 @patch('avocado.plugins.assets.safeloader')195 def test_visit_assign_invalid_class_method(self, mocked_safeloader):196 """197 Make sure that current_klass is not assigned with a class name198 """199 mocked_safeloader.find_avocado_tests.return_value = [[]]200 tree = ast.parse(NOT_TEST_CLASS_SOURCE)201 node = tree.body[1].body[0].body[0]202 with patch("builtins.open", mock_open(read_data=NOT_TEST_CLASS_SOURCE)):203 with patch.object(assets.ast, "parse"):204 with patch.object(assets.FetchAssetHandler, "visit"):205 with patch.object(assets.FetchAssetHandler,206 "generic_visit"):207 handler = assets.FetchAssetHandler("fake_file.py")208 handler.visit_Assign(node)209 self.assertTrue((handler.current_method is None))210if __name__ == '__main__':...

Full Screen

Full Screen

test_safeloader_caveats.py

Source:test_safeloader_caveats.py Github

copy

Full Screen

...12 failure = os.path.join(caveat_dir, "failure.py")13 sys_path = sys.path + [caveat_dir]14 with unittest.mock.patch("sys.path", sys_path):15 self.assertEqual(16 find_avocado_tests(success)[0], {"Test": [("test", {}, [])]}17 )...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful