Best Python code snippet using avocado_python
__init__.py
Source:__init__.py
...9BASEDIR = os.path.abspath(os.path.join(BASEDIR, os.path.pardir))10#: The name of the avocado test runner entry point11AVOCADO = os.environ.get("UNITTEST_AVOCADO_CMD",12 "%s -m avocado" % sys.executable)13def python_module_available(module_name):14 '''15 Checks if a given Python module is available16 :param module_name: the name of the module17 :type module_name: str18 :returns: if the Python module is available in the system19 :rtype: bool20 '''21 try:22 pkg_resources.require(module_name)23 return True24 except pkg_resources.DistributionNotFound:25 return False26def setup_avocado_loggers():27 """28 Setup avocado loggers to contain at least one logger29 This is required for tests that directly utilize avocado.Test classes30 because they require those loggers to be configured. Without this31 it might (py2) result in infinite recursion while attempting to log32 "No handlers could be found for logger ..." message.33 """34 for name in ('', 'avocado.test', 'avocado.app'):35 logger = logging.getLogger(name)36 if not logger.handlers:37 logger.handlers.append(logging.NullHandler())38def temp_dir_prefix(module_name, klass, method):39 """40 Returns a standard name for the temp dir prefix used by the tests41 """42 fmt = 'avocado__%s__%s__%s__'43 return fmt % (module_name, klass.__class__.__name__, method)44def get_temporary_config(module_name, klass, method):45 """46 Creates a temporary bogus config file47 returns base directory, dictionary containing the temporary data dir48 paths and the configuration file contain those same settings49 """50 prefix = temp_dir_prefix(module_name, klass, method)51 base_dir = tempfile.TemporaryDirectory(prefix=prefix)52 test_dir = os.path.join(base_dir.name, 'tests')53 os.mkdir(test_dir)54 data_directory = os.path.join(base_dir.name, 'data')55 os.mkdir(data_directory)56 cache_dir = os.path.join(data_directory, 'cache')57 os.mkdir(cache_dir)58 mapping = {'base_dir': base_dir.name,59 'test_dir': test_dir,60 'data_dir': data_directory,61 'logs_dir': os.path.join(base_dir.name, 'logs'),62 'cache_dir': cache_dir}63 temp_settings = ('[datadir.paths]\n'64 'base_dir = %(base_dir)s\n'65 'test_dir = %(test_dir)s\n'66 'data_dir = %(data_dir)s\n'67 'logs_dir = %(logs_dir)s\n') % mapping68 config_file = tempfile.NamedTemporaryFile('w', delete=False)69 config_file.write(temp_settings)70 config_file.close()71 return base_dir, mapping, config_file72#: The plugin module names and directories under optional_plugins73PLUGINS = {'varianter_yaml_to_mux': 'avocado-framework-plugin-varianter-yaml-to-mux',74 'runner_remote': 'avocado-framework-plugin-runner-remote',75 'runner_vm': 'avocado-framework-plugin-runner-vm',76 'varianter_cit': 'avocado-framework-plugin-varianter-cit',77 'html': 'avocado-framework-plugin-result-html'}78def test_suite(base_selftests=True, plugin_selftests=None):79 '''80 Returns a test suite with all selftests found81 This includes tests on available optional plugins directories82 :param base_selftests: if the base selftests directory should be included83 :type base_selftests: bool84 :param plugin_selftests: the list optional plugin directories to include85 or None to include all86 :type plugin_selftests: list or None87 :rtype: unittest.TestSuite88 '''89 suite = unittest.TestSuite()90 loader = unittest.TestLoader()91 selftests_dir = os.path.dirname(os.path.abspath(__file__))92 basedir = os.path.dirname(selftests_dir)93 if base_selftests:94 for section in ('unit', 'functional'):95 start_dir = os.path.join(selftests_dir, section)96 suite.addTests(loader.discover(start_dir=start_dir,97 top_level_dir=basedir))98 if plugin_selftests is None:99 plugin_selftests = PLUGINS.keys()100 for plugin_dir in plugin_selftests:101 plugin_name = PLUGINS.get(plugin_dir, None)102 if python_module_available(plugin_name):103 path = os.path.join(basedir, 'optional_plugins',104 plugin_dir, 'tests')105 suite.addTests(loader.discover(start_dir=path, top_level_dir=path))106 return suite107def skipOnLevelsInferiorThan(level):108 return unittest.skipIf(int(os.environ.get("AVOCADO_CHECK_LEVEL", 0)) < level,109 "Skipping test that take a long time to run, are "110 "resource intensive or time sensitve")111def skipUnlessPathExists(path):112 return unittest.skipUnless(os.path.exists(path),113 ('File or directory at path "%s" used in test is'114 ' not available in the system' % path))115class TestCaseTmpDir(unittest.TestCase):116 def setUp(self):...
test_robot.py
Source:test_robot.py
1import os2import unittest3import pkg_resources4import avocado_robot5def python_module_available(module_name):6 '''7 Checks if a given Python module is available8 :param module_name: the name of the module9 :type module_name: str10 :returns: if the Python module is available in the system11 :rtype: bool12 '''13 try:14 pkg_resources.require(module_name)15 return True16 except pkg_resources.DistributionNotFound:17 return False18THIS_DIR = os.path.dirname(os.path.abspath(__file__))19ROBOT_AVOCADO = os.path.join(THIS_DIR, 'avocado.robot')20class Loader(unittest.TestCase):21 @unittest.skipUnless(python_module_available('robotframework'),22 'robotframework python module missing')23 @unittest.skipUnless(python_module_available('avocado-framework-plugin-robot'),24 'avocado-framework-plugin-robot python module missing')25 @unittest.skipUnless(os.path.isfile(ROBOT_AVOCADO),26 'Robot test file not found at "%s"' % ROBOT_AVOCADO)27 def test_discover(self):28 loader = avocado_robot.RobotLoader(None, {})29 results = loader.discover(ROBOT_AVOCADO)30 self.assertEqual(len(results), 2)31 nosleep_klass, nosleep_params = results[0]32 self.assertIs(nosleep_klass, avocado_robot.RobotTest)33 self.assertEqual(nosleep_params['name'],34 "%s:Avocado.NoSleep" % ROBOT_AVOCADO)35 sleep_klass, sleep_params = results[1]36 self.assertIs(sleep_klass, avocado_robot.RobotTest)37 self.assertEqual(sleep_params['name'],...
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!!