-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): add project TestCase (#69)
Add a project `TestCase` that extends `unittest.TestCase` to provide the following: - Configure the execution environment at the class level, once for the whole `TestCase`. This is similar to calling `app.setup`. - Add a class method, `config_factory` that returns an `app.lib.Config` instance used to configure the execution environment of this `TestCase` instances. All existing unit tests have also been refactored to extend from this TestCase. Included also are minor bug fixes and improvements.
- Loading branch information
1 parent
f2a7a3c
commit c0d249f
Showing
21 changed files
with
109 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from unittest import TestCase as _TestCase | ||
from unittest.mock import patch | ||
|
||
from app.lib import AppRegistry, Config | ||
|
||
from .factories import config_factory as _cf | ||
|
||
|
||
class TestCase(_TestCase): | ||
"""A project-specific ``TestCase``. | ||
Extend the default :class:`unittest.TestCase` to provide the following: | ||
* Configure the execution environment at the class level, once for the | ||
whole ``TestCase``. This is similar to calling :func:`app.setup`. | ||
* Add a class method, :meth:`config_factory` that returns a | ||
:class:`Config <app.lib.Config>` instance used to configure the | ||
execution environment of this ``TestCase`` instances. | ||
""" | ||
|
||
@classmethod | ||
def config_factory(cls) -> Config: | ||
"""Return a ``Config`` instance. | ||
Return a :class:`Config <app.lib.Config>` instance used to configure | ||
the execution environment of this ``TestCase`` instances. | ||
:return: a Config instance. | ||
""" | ||
return Config(settings=_cf()) | ||
|
||
@classmethod | ||
def setUpClass(cls) -> None: | ||
"""Set up class fixture before running the tests in the class. | ||
Extend the default implementation by setting up a patch for the | ||
application settings and registry using the value returned by the | ||
:meth:`config_factory` class method. The patch is accessible through | ||
the class attribute :attr:`config_patch` for interested subclasses. | ||
.. warning:: | ||
It is highly encouraged that subclasses extending this method call | ||
``super()`` to avoid weird, unpredictable behaviour during test | ||
runs caused by misconfigured execution environments. | ||
:return: None. | ||
""" | ||
super().setUpClass() | ||
config_patcher = patch.multiple( | ||
"app", | ||
registry=AppRegistry(), | ||
settings=cls.config_factory(), | ||
) | ||
config_patcher.start() | ||
cls.config_patcher = config_patcher | ||
cls.addClassCleanup(config_patcher.stop) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.