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 containssome_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 usemode='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.
-
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
whenENABLE_PRESERVE
is set to True.
-
TMP_DIR
= None¶ Allows for setting the temp directory. Defaults to
None
which will use Python’stempfile.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:
-
-
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:
-
assert_iterable_of_type
(iterable, types, msg='')[source]¶ Assert that all items in the given iterable are of the given type(s).
Parameters:
-
assert_length
(sized, length, msg='')[source]¶ Asserts that the sized object has length number of items.
Parameters: 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???')
-