aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_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/test_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/test_base.py')
-rw-r--r--tests/test_base.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/test_base.py b/tests/test_base.py
new file mode 100644
index 000000000..b7c1e0037
--- /dev/null
+++ b/tests/test_base.py
@@ -0,0 +1,61 @@
+import logging
+import unittest
+import unittest.mock
+
+
+from tests.base import LoggingTestCase
+
+
+class LoggingTestCaseTests(unittest.TestCase):
+ """Tests for the LoggingTestCase."""
+
+ @classmethod
+ def setUpClass(cls):
+ cls.log = logging.getLogger(__name__)
+
+ def test_assert_not_logs_does_not_raise_with_no_logs(self):
+ """Test if LoggingTestCase.assertNotLogs does not raise when no logs were emitted."""
+ try:
+ with LoggingTestCase.assertNotLogs(self, level=logging.DEBUG):
+ pass
+ except AssertionError:
+ self.fail("`self.assertNotLogs` raised an AssertionError when it should not!")
+
+ @unittest.mock.patch("tests.base.LoggingTestCase.assertNotLogs")
+ def test_the_test_function_assert_not_logs_does_not_raise_with_no_logs(self, assertNotLogs):
+ """Test if test_assert_not_logs_does_not_raise_with_no_logs captures exception correctly."""
+ assertNotLogs.return_value = iter([None])
+ assertNotLogs.side_effect = AssertionError
+
+ message = "`self.assertNotLogs` raised an AssertionError when it should not!"
+ with self.assertRaises(AssertionError, msg=message):
+ self.test_assert_not_logs_does_not_raise_with_no_logs()
+
+ def test_assert_not_logs_raises_correct_assertion_error_when_logs_are_emitted(self):
+ """Test if LoggingTestCase.assertNotLogs raises AssertionError when logs were emitted."""
+ msg_regex = (
+ r"1 logs of DEBUG or higher were triggered on root:\n"
+ r'<LogRecord: tests\.test_base, [\d]+, .+/tests/test_base\.py, [\d]+, "Log!">'
+ )
+ with self.assertRaisesRegex(AssertionError, msg_regex):
+ with LoggingTestCase.assertNotLogs(self, level=logging.DEBUG):
+ self.log.debug("Log!")
+
+ def test_assert_not_logs_reraises_unexpected_exception_in_managed_context(self):
+ """Test if LoggingTestCase.assertNotLogs reraises an unexpected exception."""
+ with self.assertRaises(ValueError, msg="test exception"):
+ with LoggingTestCase.assertNotLogs(self, level=logging.DEBUG):
+ raise ValueError("test exception")
+
+ def test_assert_not_logs_restores_old_logging_settings(self):
+ """Test if LoggingTestCase.assertNotLogs reraises an unexpected exception."""
+ old_handlers = self.log.handlers[:]
+ old_level = self.log.level
+ old_propagate = self.log.propagate
+
+ with LoggingTestCase.assertNotLogs(self, level=logging.DEBUG):
+ pass
+
+ self.assertEqual(self.log.handlers, old_handlers)
+ self.assertEqual(self.log.level, old_level)
+ self.assertEqual(self.log.propagate, old_propagate)