Best Python code snippet using avocado_python
test_safeloader.py
Source:test_safeloader.py  
...6else:7    import unittest8from avocado.core import safeloader9from avocado.utils import script10def get_this_file():11    this_file = __file__12    if this_file.endswith('.py'):13        return this_file14    elif (this_file.endswith('.pyc') or this_file.endswith('.pyo')):15        return this_file[:-1]16    else:17        raise ValueError("Could not find the Python file associated with this "18                         "module")19class ModuleImportedAs(unittest.TestCase):20    def _test(self, content, result):21        temp_script = script.TemporaryScript('temp.py', content,22                                             'avocado_loader_unittest')23        temp_script.save()24        module = ast.parse(content, temp_script.path)25        temp_script.remove()26        self.assertEqual(result, safeloader.modules_imported_as(module))27    def test_foo(self):28        self._test('import foo', {'foo': 'foo'})29    def test_foo_as_bar(self):30        self._test('import foo as bar', {'foo': 'bar'})31    def test_foo_as_foo(self):32        self._test('import foo as foo', {'foo': 'foo'})33    def test_import_inside_class(self):34        self._test("class Foo(object): import foo as foo", {})35class DocstringTag(unittest.TestCase):36    def test_longline(self):37        docstring = ("This is a very long docstring in a single line. "38                     "Since we have nothing useful to put in here let's just "39                     "mention avocado: it's awesome, but that was not a tag. "40                     "a tag would be something line this: :avocado: enable")41        self.assertIsNotNone(safeloader.get_docstring_tag(docstring))42    def test_newlines(self):43        docstring = ("\n\n\nThis is a docstring with many new\n\nlines "44                     "followed by an avocado tag\n"45                     "\n\n:avocado: enable\n\n")46        self.assertIsNotNone(safeloader.get_docstring_tag(docstring))47    def test_enabled(self):48        self.assertTrue(safeloader.is_docstring_tag_enable(":avocado: enable"))49        self.assertTrue(safeloader.is_docstring_tag_enable(":avocado:\tenable"))50        self.assertFalse(safeloader.is_docstring_tag_enable(":AVOCADO: ENABLE"))51        self.assertFalse(safeloader.is_docstring_tag_enable(":avocado: enabled"))52    def test_disabled(self):53        self.assertTrue(safeloader.is_docstring_tag_disable(":avocado: disable"))54        self.assertTrue(safeloader.is_docstring_tag_disable(":avocado:\tdisable"))55        self.assertFalse(safeloader.is_docstring_tag_disable(":AVOCADO: DISABLE"))56        self.assertFalse(safeloader.is_docstring_tag_disable(":avocado: disabled"))57class UnlimitedDiff(unittest.TestCase):58    """59    Serves two purposes: as a base class to test safeloader.find_class_and_methods60    and, while at it, to set unlimited diff on failure results.61    """62    def setUp(self):63        self.maxDiff = None64class FindClassAndMethods(UnlimitedDiff):65    def test_self(self):66        reference = {67            'ModuleImportedAs': ['_test',68                                 'test_foo',69                                 'test_foo_as_bar',70                                 'test_foo_as_foo',71                                 'test_import_inside_class'],72            'DocstringTag': ['test_longline',73                             'test_newlines',74                             'test_enabled',75                             'test_disabled'],76            'FindClassAndMethods': ['test_self',77                                    'test_with_pattern',78                                    'test_with_base_class',79                                    'test_with_pattern_and_base_class'],80            'UnlimitedDiff': ['setUp']81        }82        found = safeloader.find_class_and_methods(get_this_file())83        self.assertEqual(reference, found)84    def test_with_pattern(self):85        reference = {86            'ModuleImportedAs': ['test_foo',87                                 'test_foo_as_bar',88                                 'test_foo_as_foo',89                                 'test_import_inside_class'],90            'DocstringTag': ['test_longline',91                             'test_newlines',92                             'test_enabled',93                             'test_disabled'],94            'FindClassAndMethods': ['test_self',95                                    'test_with_pattern',96                                    'test_with_base_class',97                                    'test_with_pattern_and_base_class'],98            'UnlimitedDiff': []99        }100        found = safeloader.find_class_and_methods(get_this_file(),101                                                  re.compile(r'test.*'))102        self.assertEqual(reference, found)103    def test_with_base_class(self):104        reference = {105            'FindClassAndMethods': ['test_self',106                                    'test_with_pattern',107                                    'test_with_base_class',108                                    'test_with_pattern_and_base_class'],109        }110        found = safeloader.find_class_and_methods(get_this_file(),111                                                  base_class='UnlimitedDiff')112        self.assertEqual(reference, found)113    def test_with_pattern_and_base_class(self):114        reference = {115            'FindClassAndMethods': ['test_with_pattern',116                                    'test_with_base_class',117                                    'test_with_pattern_and_base_class'],118        }119        found = safeloader.find_class_and_methods(get_this_file(),120                                                  re.compile(r'test_with.*'),121                                                  'UnlimitedDiff')122        self.assertEqual(reference, found)123if __name__ == '__main__':...this_file.py
Source:this_file.py  
1import re,os2def get_this_file():3    g = globals()4    file = None5    if __name__ == "__main__" and \6       len(sys.argv) > 0 and len(sys.argv[0]) > 0:7        # run from command line (python .../gxpd.py)8        file = sys.argv[0]9    elif g.has_key("__file__"):10        # appears this has been loaded as a module11        file = g["__file__"]12        # if it is .pyc file, get .py instead13        m = re.match("(.+)\.pyc$", file)14        if m: file = m.group(1) + ".py"15    if file is None:16        return None,"cannot find the location of this_file.py"17    #file = os.path.abspath(file)18    file = os.path.realpath(file)19    if os.access(file, os.F_OK) == 0:20        return None,("source file %s is missing!\n" % file)21    elif os.access(file, os.R_OK) == 0:22        return None,("cannot read source file %s!\n" % file)23    else:24        return file,None25def get_this_dir():26    f,err = get_this_file()27    if f is None:28        return None,err29    else:30        a,b = os.path.split(f)31        if b != "this_file.py":32            return None,("filename (%s) != this_file.py" % b)...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!!
