Best Python code snippet using avocado_python
test_imports.py
Source:test_imports.py
1import pytest2from easydict import EasyDict3from hbutils.reflection import import_object, quick_import_object, iter_import_objects4from ..testings import linux_mark, windows_mark5OBJ = 16P_1 = 27P_2 = 38P_3 = 49QQ3 = EasyDict(10 P_1=P_1,11 P_2=P_2,12 P_3=P_3,13)14class TestReflectionImports:15 @pytest.mark.unittest16 def test_import_object(self):17 assert import_object('OBJ', 'test.reflection.test_imports') == 118 assert import_object('zip') == zip19 @pytest.mark.unittest20 def test_quick_import_object(self):21 assert quick_import_object('test.reflection.test_imports.OBJ') == (1, 'test.reflection.test_imports', 'OBJ')22 assert quick_import_object('zip') == (zip, '', 'zip')23 assert quick_import_object('zip.__dict__') == (zip.__dict__, '', 'zip.__dict__')24 assert quick_import_object('test.reflection.test_imports.P_?') == (P_1, 'test.reflection.test_imports', 'P_1')25 assert quick_import_object('test.reflection.test_imports.???.*') == (26 P_1, 'test.reflection.test_imports', 'QQ3.P_1')27 with pytest.raises(ImportError):28 quick_import_object('p233')29 with pytest.raises(ImportError):30 quick_import_object('zip.no_such_attr')31 @linux_mark32 def test_quick_import_object_linux(self):33 assert quick_import_object('z*') == (zip, '', 'zip')34 @windows_mark35 def test_quick_import_object_windows(self):36 assert quick_import_object('z*') == (ZeroDivisionError, '', 'ZeroDivisionError')37 @pytest.mark.unittest38 def test_iter_import_objects(self):39 assert list(iter_import_objects('test.reflection.test_imports.P_*')) == [40 (P_1, 'test.reflection.test_imports', 'P_1'),41 (P_2, 'test.reflection.test_imports', 'P_2'),42 (P_3, 'test.reflection.test_imports', 'P_3'),43 ]44 assert list(iter_import_objects('test.reflection.test_imports.???.*')) == [45 (P_1, 'test.reflection.test_imports', 'QQ3.P_1'),46 (P_2, 'test.reflection.test_imports', 'QQ3.P_2'),47 (P_3, 'test.reflection.test_imports', 'QQ3.P_3'),...
imports.py
Source:imports.py
1''' Some basic code for testing whether forest modules can be imported.2'''3def test_imports(subpackage_name, module_names):4 n_success = 05 n_failure = 06 print('Testing imports from %s...' % subpackage_name)7 for m in module_names:8 test_import = 'from %s.%s import *' % (subpackage_name, m)9 try:10 exec(test_import)11 n_success += 112 except Exception:13 print(' Failed to import %s.%s' % (subpackage_name, m))14 n_failure += 115 print('Successful imports: %s' % n_success)16 print('Failed imports: %s \n' % n_failure)17# Test imports for forest.willow18test_imports('forest.willow',19 ['log_stats'])20# Test imports for forest.jasmine21test_imports('forest.jasmine',22 ['data2mobmat', 'mobmat2traj', 'sogp_gps', 'traj2stats',23 'simulate_gps_data'])24# Test imports for forest.poplar25test_imports('forest.poplar.classes',26 ['history', 'registry', 'template', 'trackers'])27test_imports('forest.poplar.constants',28 ['misc', 'time'])29test_imports('forest.poplar.functions',30 ['helpers', 'holidays', 'io', 'log', 'time', 'timezone'])31test_imports('forest.poplar.legacy',32 ['common_funcs'])33test_imports('forest.poplar.raw',...
test_import.py
Source:test_import.py
...4import subprocess5import warnings6logger = logging.getLogger(__name__)7class TestImports(unittest.TestCase):8 def test_imports(self):9 import tests.test_imports.importa10 with warnings.catch_warnings(record=True) as warns:11 tests.test_imports.importa.do_import()12 self.assertGreater(len(warns), 0)13 # this as well checks for the namespace's pollution14 self.assertEqual(set(tests.test_imports.importa.importb.__all__),15 {'logger', 'sub', 'logging', 'add'})16 self.assertEqual(tests.test_imports.importa.importb.add(4, 5), 9)17 self.assertEqual(tests.test_imports.importa.importb.sub(4, 5), -1)18 self.assertRaises(AttributeError, lambda: tests.test_imports.importa.importb.mul(1, 2))19 def test_import_class(self):20 p_open = import_class('subprocess.Popen')21 self.assertIs(p_open, subprocess.Popen)22 self.assertRaises(ImportError, lambda: import_class('subprocess.DoesNotExist'))
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!!