avocado.core.safeloader package

Submodules

avocado.core.safeloader.core module

exception avocado.core.safeloader.core.ClassNotSuitable

Bases: Exception

Exception raised when examination of a class should not proceed.

avocado.core.safeloader.core.find_avocado_tests(path)
avocado.core.safeloader.core.find_python_tests(target_module, target_class, determine_match, path)

Attempts to find Python tests from source files

A Python test in this context is a method within a specific type of class (or that inherits from a specific class).

Parameters:
  • target_module (str) – the name of the module from which a class should have come from. When attempting to find a Python unittest, the target_module will most probably be “unittest”, as per the standard library module name. When attempting to find Avocado tests, the target_module will most probably be “avocado”.
  • target_class (str) – the name of the class that is considered to contain test methods. When attempting to find Python unittests, the target_class will most probably be “TestCase”. When attempting to find Avocado tests, the target_class will most probably be “Test”.
  • path (str) – path to a Python source code file
Returns:

tuple where first item is dict with class name and additional info such as method names and tags; the second item is set of class names which look like Python tests but have been forcefully disabled.

Return type:

tuple

avocado.core.safeloader.core.find_python_unittests(path)
avocado.core.safeloader.core.get_methods_info(statement_body, class_tags, class_dependencies)

Returns information on test methods.

Parameters:
  • statement_body – the body of a “class” statement
  • class_tags – the tags at the class level, to be combined with the tags at the method level.
  • class_dependencies – the dependencies at the class level, to be combined with the dependencies at the method level.

avocado.core.safeloader.docstring module

avocado.core.safeloader.docstring.DOCSTRING_DIRECTIVE_RE_RAW = '\\s*:avocado:[ \\t]+(([a-zA-Z0-9]+?[a-zA-Z0-9_:,\\=\\-\\.]*)|(dependency={.*}))\\s*$'

Gets the docstring directive value from a string. Used to tweak test behavior in various ways

avocado.core.safeloader.docstring.check_docstring_directive(docstring, directive)

Checks if there’s a given directive in a given docstring

Return type:bool
avocado.core.safeloader.docstring.get_docstring_directives(docstring)

Returns the values of the avocado docstring directives

Parameters:docstring (str) – the complete text used as documentation
Return type:builtin.list
avocado.core.safeloader.docstring.get_docstring_directives_dependencies(docstring)

Returns the test dependencies from docstring patterns like :avocado: dependencies={}.

Return type:list
avocado.core.safeloader.docstring.get_docstring_directives_tags(docstring)

Returns the test categories based on a :avocado: tags=category docstring

Return type:dict

avocado.core.safeloader.imported module

class avocado.core.safeloader.imported.ImportedSymbol(module_path, symbol='', importer_fs_path=None, module_alias='', symbol_alias='')

Bases: object

A representation of an importable symbol.

Attributes:

module_path : str symbol : str importer_fs_path: str or None

classmethod from_statement(statement, importer_fs_path=None, index=0)
get_importable_spec(symbol_is_module=False)

Returns the specification of an actual importable module.

This is a check based on the limitations that we do not actually perform an import, and assumes a directory structure with modules.

Parameters:symbol_is_module (bool) – if it’s known that the symbol is also a module, include it in the search for an importable spec
static get_module_path_from_statement(statement)
get_parent_fs_path()
get_relative_module_fs_path()

Returns the module base dir, based on its relative path

The base dir for the module is the directory where one is expected to find the first module of the module path. For a module path of “..foo.bar”, and its importer being at “/abs/path/test.py”, the base dir where “foo” is supposed to be found would be “/abs”. And as a consequence, “bar” would be found at “/abs/foo/bar”.

This assumes that the module path is indeed related to the location of its importer. This may not be true if the namespaces match, but are distributed across different filesystem paths.

static get_symbol_from_statement(statement)
static get_symbol_module_path_from_statement(statement, name_index=0)
importer_fs_path = None

The full, absolute filesystem path of the module importing this symbol. This is used for relative path calculations, but it’s limited to relative modules that also share the filesystem location. An example is “/path/to/mytest.py”, that can contain:

from .base import BaseTestClass

And thus will have a symbol of “BaseTestClass” and the module as “.base”. The relative filesystem path of the module (which should contain the symbol) will be “/path/to”.

And if “/path/to/common/test.py” contains:

from ..base import BaseTestClass

The relative filesystem path of the module (which should contain the symbol) will be “/path/to”.

is_importable(symbol_is_module=False)

Checks whether this imported symbol seems to be importable.

This is a check based on the limitations that we do not actually perform an import, and assumes a directory structure with modules.

Parameters:symbol_is_module (bool) – if it’s known that the symbol is also a module, include it in the search for an importable spec
is_relative()

Returns whether the imported symbol is on a relative path.

module_alias = None

An optional alias for the module, such as when a “import os as operating_system” statement is given.

module_name

The final name of the module from its importer perspective.

If a alias exists, it will be the alias name. If not, it will be the original name.

module_path = None

Path from where the symbol was imported. On a statement such as “import os”, module_path is “os” and there’s no symbol. On a statement such as from unittest.mock import mock_open”, the module_path is “unittest.mock”. On a statement such as “from ..foo import bar”, module_path is “..foo” (relative).

symbol = None

The name of the imported symbol. On a statement such as “import os”, there’s no symbol. On a statement such as “from unittest import mock””, the symbol is “mock” (even though it may actually also be a module, but it’s impossible to know for sure). On a statement such as “from unittest.mock import mock_open”, symbol is “mock_open”.

symbol_alias = None

An optional alias the symbol, such as when a “from os import path as os_path” is given

symbol_name

The final name of the symbol from its importer perspective.

If a alias exists, it will be the alias name. If not, it will be the original name.

to_str()

Returns a string representation of the plausible statement used.

avocado.core.safeloader.module module

class avocado.core.safeloader.module.PythonModule(path, module='avocado', klass='Test')

Bases: object

Representation of a Python module that might contain interesting classes

By default, it uses module and class names that matches Avocado instrumented tests, but it’s supposed to be agnostic enough to be used for, say, Python unittests.

Instantiates a new PythonModule representation

Parameters:
  • path (str) – path to a Python source code file
  • module (str) – the original module name from where the possibly interesting class must have been imported from
  • klass (str) – the possibly interesting class original name
add_imported_symbol(statement)

Keeps track of symbol names and importable entities

imported_symbols
interesting_klass_found
is_matching_klass(klass)

Detect whether given class directly defines itself as <module>.<klass>

It can either be a <klass> that inherits from a test “symbol”, like:

`class FooTest(Test)`

Or from an <module>.<klass> symbol, like in:

`class FooTest(avocado.Test)`

Return type:bool
iter_classes(interesting_klass=None)

Iterate through classes and keep track of imported avocado statements

klass
klass_imports
mod
mod_imports
module
path

avocado.core.safeloader.utils module

avocado.core.safeloader.utils.get_statement_import_as(statement)

Returns a mapping of imported module names whether using aliases or not

Parameters:statement (ast.Import) – an AST import statement
Returns:a mapping of names {<realname>: <alias>} of modules imported
Return type:collections.OrderedDict

Module contents

Safe (AST based) test loader module utilities

avocado.core.safeloader.find_avocado_tests(path)
avocado.core.safeloader.find_python_unittests(path)