aboutsummaryrefslogtreecommitdiffstats
path: root/tests/base.py
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2019-10-02 16:59:03 +0200
committerGravatar Sebastiaan Zeeff <[email protected]>2019-10-11 17:42:21 +0200
commitc4213744c18be23e3e4484f126ae0b2d0eba4437 (patch)
treefa26b8d115eac7b9d46fd2abae966c3030f32e78 /tests/base.py
parentMerge pull request #505 from python-discord/user-log-display-name-changes (diff)
Migrate pytest to unittest
After a discussion in the core developers channel, we have decided to migrate from `pytest` to `unittest` as the testing framework. This commit sets up the repository to use `unittest` and migrates the first couple of tests files to the new framework. What I have done to migrate to `unitest`: - Removed all `pytest` test files, since they are incompatible. - Removed `pytest`-related dependencies from the Pipfile. - Added `coverage.py` to the Pipfile dev-packages and relocked. - Added convenience scripts to Pipfile for running the test suite. - Adjust to `azure-pipelines.yml` to use `coverage.py` and `unittest`. - Migrated four test files from `pytest` to `unittest` format. In addition, I've added five helper Mock subclasses in `helpers.py` and created a `TestCase` subclass in `base.py` to add an assertion that asserts that no log records were logged within the context of the context manager. Obviously, these new utility functions and classes are fully tested in their respective `test_` files. Finally, I've started with an introductory guide for writing tests for our bot in `README.md`.
Diffstat (limited to 'tests/base.py')
-rw-r--r--tests/base.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/base.py b/tests/base.py
new file mode 100644
index 000000000..625dcc0a8
--- /dev/null
+++ b/tests/base.py
@@ -0,0 +1,70 @@
+import logging
+import unittest
+from contextlib import contextmanager
+
+
+class _CaptureLogHandler(logging.Handler):
+ """
+ A logging handler capturing all (raw and formatted) logging output.
+ """
+
+ def __init__(self):
+ super().__init__()
+ self.records = []
+
+ def flush(self):
+ pass
+
+ def emit(self, record):
+ self.records.append(record)
+
+
+class LoggingTestCase(unittest.TestCase):
+ """TestCase subclass that adds more logging assertion tools."""
+
+ @contextmanager
+ def assertNotLogs(self, logger=None, level=None, msg=None):
+ """
+ Asserts that no logs of `level` and higher were emitted by `logger`.
+
+ You can specify a specific `logger`, the minimum `logging` level we want to watch and a
+ custom `msg` to be added to the `AssertionError` if thrown. If the assertion fails, the
+ recorded log records will be outputted with the `AssertionError` message. The context
+ manager does not yield a live `look` into the logging records, since we use this context
+ manager when we're testing under the assumption that no log records will be emitted.
+ """
+ if not isinstance(logger, logging.Logger):
+ logger = logging.getLogger(logger)
+
+ if level:
+ level = logging._nameToLevel.get(level, level)
+ else:
+ level = logging.INFO
+
+ handler = _CaptureLogHandler()
+ old_handlers = logger.handlers[:]
+ old_level = logger.level
+ old_propagate = logger.propagate
+
+ logger.handlers = [handler]
+ logger.setLevel(level)
+ logger.propagate = False
+
+ try:
+ yield
+ except Exception as exc:
+ raise exc
+ finally:
+ logger.handlers = old_handlers
+ logger.propagate = old_propagate
+ logger.setLevel(old_level)
+
+ if handler.records:
+ level_name = logging.getLevelName(level)
+ n_logs = len(handler.records)
+ base_message = f"{n_logs} logs of {level_name} or higher were triggered on {logger.name}:\n"
+ records = [str(record) for record in handler.records]
+ record_message = "\n".join(records)
+ standard_message = self._truncateMessage(base_message, record_message)
+ msg = self._formatMessage(msg, standard_message)
+ self.fail(msg)