diff options
author | 2020-02-23 12:39:25 +0100 | |
---|---|---|
committer | 2020-02-23 21:00:48 +0100 | |
commit | d3f4673c1a1c3f5213840e756c5f35f7c70d46f6 (patch) | |
tree | 89687178d0bba65a786c4fba2d9defe96b59cdb2 /tests/base.py | |
parent | Update to Python 3.8 and discord.py 1.3.2 (diff) |
Use mixin-composition not inheritance for LoggingTestCase
We used inheritence to add additional logging assertion methods to unittest's TestCase class. However, with the introduction of the new IsolatedAsyncioTestCase this extension strategy means we'd have to create multiple child classes to be able to use the extended functionality in all of the TestCase variants.
Since that leads to undesirable code reuse and an inheritance relationship is not at all needed, I've switched to a mixin-composition based approach that allows the user to extend the functionality of any TestCase variant with a mixin where needed.
Diffstat (limited to 'tests/base.py')
-rw-r--r-- | tests/base.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/tests/base.py b/tests/base.py index 029a249ed..21a57716a 100644 --- a/tests/base.py +++ b/tests/base.py @@ -1,5 +1,4 @@ import logging -import unittest from contextlib import contextmanager @@ -16,8 +15,13 @@ class _CaptureLogHandler(logging.Handler): self.records.append(record) -class LoggingTestCase(unittest.TestCase): - """TestCase subclass that adds more logging assertion tools.""" +class LoggingTestsMixin: + """ + A mixin that defines additional test methods for logging behavior. + + This mixin relies on the availability of the `fail` attribute defined by the + test classes included in Python's unittest method to signal test failure. + """ @contextmanager def assertNotLogs(self, logger=None, level=None, msg=None): |