granite.testcase module

Extends unittest.

exception AssetDirectoryNotSet[source]

Bases: granite.exceptions.GraniteException

Raised when the ASSETS_DIR attribute is not set.

class AssetMixin(*args, **kwargs)[source]

Bases: granite.testcase.TestCaseMixin

Provides support for accessing assets needed by tests.

ASSET_DIR = None
get_asset_filename(*parts)[source]

Gets the absolute filename of the asset file given by filename and *parts relative to the asset directory.

Treat this input like that of os.path.join.

Assume the absolute path to the ASSETS_DIR is /path/to/assets/ and the assets directory contains some_file.txt, then:

>>> self.get_asset_filename('some_file.txt')
'path/to/assets/some_file.txt'
Raises:AssetNotFound – when the filename to search for is not found on disk.
Returns:the absolute path to the asset file.
Return type:str
read_asset_file(filename, *parts, **kwargs)[source]

Gets the contents of the given asset filename.

Internally this calls get_asset_filename()

Pass the optional keyword argument mode in oder to set the file mode. For example use mode='rb' to read in bytes.

Parameters:
  • filename
  • *parts
  • **kwargs
Returns:

The contents of the file using the given read mode.

Raises:

AssetNotFound – when the filename to search for is not found on disk.

exception AssetNotFound[source]

Bases: granite.exceptions.GraniteException

Raised when an asset file requested is not found.

class AutoMockMixin[source]

Bases: granite.testcase.TestCaseMixin

Helps prevent the boilerplate of having mock patcher and object setup and teardown logic for every function needing to be mocked.

setUp()[source]

Attaches all mock patchers to this instance as mocked attributes

class TemporaryProjectMixin(*args, **kwargs)[source]

Bases: granite.testcase.TestCaseMixin

Provides support for temporary project (directory) creation on a per-test basis.

In order to use this mixin, the base TestCase class should inherit this mixin. This provides a new attribute named temp_project which is an instance of TemporaryProject.

See the TemporaryProject class for all of its methods for how to manipulate the created temp project.

Example:

import os

from granite.testcase import TestCase, TemporaryProjectMixin

class TestSomeThing(TemporaryProjectMixin, TestCase):
    def test_some_thing(self):
        # a new temporary directory has already been created by
        # this point. let's create a new file and add some contents:
        self.temp_project.write('some_file', 'Ohai :)')
        # get the temp_project's path by its .path attribute.
        # This proves that the file was created and exists on disk:
        self.assertTrue(os.path.exists(
            os.path.join(self.temp_project.path, 'some_file')))
        # read the contents of a file relative to the temp project's
        # directory:
        contents = self.temp_project.read('some_file')
        self.assertEqual(contents, 'Ohai :)')
ENABLE_PRESERVE = False

A flag indicating whether the temp project should be preserved after the temp project object is destroyed. If True, the directory will still exist allowing a user to view the state of the directory after a test has run. This works in tandem with the PRESERVE_DIR class attribute.

PRESERVE_DIR = None

Sets where the preserved path should be dumped too. This overrides the TMP_DIR when ENABLE_PRESERVE is set to True.

TMP_DIR = None

Allows for setting the temp directory. Defaults to None which will use Python’s tempfile.mkdtemp to make the temp directory.

TemporaryProjectClass

alias of granite.environment.TemporaryProject

assert_in_temp_file(substring, filename, msg='', mode='r', not_in=False)[source]

Asserts that the given contents are found in the file in the temp project.

Parameters:
  • substring (str) – the substring to look for in the file’s contents
  • filename (str) – the name of the file relative to the temp project
  • msg (str) – the message to output in the event of a failure.
  • mode (str) – the mode to open the file with. defaults to ‘r’
  • not_in (bool) – asserts that the contents are not in the file
assert_not_in_temp_file(substring, filename, msg='', mode='r')[source]

Asserts that the given contents are not found in the file in the temp project.

Parameters:
  • substring (str) – the substring to look for in the file’s contents
  • filename (str) – the name of the file relative to the temp project
  • msg (str) – the message to output in the event of a failure.
  • mode (str) – the mode to open the file with. defaults to ‘r’
assert_temp_path_exists(path='.', msg='')[source]

Asserts that the path given exists relative to the root of the temp project.

Parameters:
  • path (str) – the string of the path relative to the root of the temp directory.
  • msg (str) – a custom string to show in the event that this assertion fails.
setUp()[source]

Sets up the temporary project on test startup.

tearDown()[source]

Deletes the temp project.

class TestCase(methodName='runTest')[source]

Bases: unittest.case.TestCase

Extends the Standard Library’s TestCase class.

assert_exists(path, msg='')[source]

Asserts that the given path exists on disk.

This function acts like os.path.join() in that it can accept multiple arguments all of which will be joined together before checking for existence.

Parameters:
  • path (str) – the root path to check for
  • msg (str) – the message to show if the assertion fails.
assert_iterable_of_type(iterable, types, msg='')[source]

Assert that all items in the given iterable are of the given type(s).

Parameters:
  • iterable (Iterable) – the items to check
  • types (Union[object, tuple]) – valid type input for isinstance.
  • msg (str) – optional message if the assertion fails
assert_length(sized, length, msg='')[source]

Asserts that the sized object has length number of items.

Parameters:
  • sized (Sized) – any object that implements the __len__() method.
  • length (int) – the number of items that sized should contain
  • msg (Optional[str]) – a message to display in the event that this
  • fails. (assert) –

Example:

from granite.testcase import TestCase

class MyTestCase(TestCase):
    def test_that_contents_are_correct_length(self):
        contents = [1, 2, 3]
        self.assert_length(
            contents, 3, msg='Some how, the length is not 3???')
class TestCaseMixin[source]

Bases: object

Base TestCase Mixin class. All Mixins should inherit this class.