aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2019-11-13 15:32:13 +0100
committerGravatar Sebastiaan Zeeff <[email protected]>2019-11-13 16:13:53 +0100
commitdceafb83e829548638e8589c88f80364e8009821 (patch)
tree985a0610a629580d1780c1928f3031315f4d2217 /tests
parentMerge pull request #618 from python-discord/schedule-superstarify (diff)
Prevent unwanted logging while running tests
Previously, logging messages would output to std.out. when running individual test files (instead of running the entire suite). To prevent this, I've added a `for`-loop to `tests.helpers` that sets the level of all registered loggers to `CRITICAL`. The reason for adding this to `tests.helpers` is simple: It's the most common file to be imported in individual tests, increasing the chance of the code being run for individual test files. A small downside of this way of handling logging is that when we are trying to assert logging messages are being emitted, we need to set the logger explicitly in the `self.assertLogs` context manager. This is a small downside, though, and probably good practice anyway. There was one test in `tests.bot.test_api` that did not do this, so I have changed this to make the test compatible with the new set-up.
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/test_api.py4
-rw-r--r--tests/helpers.py11
2 files changed, 14 insertions, 1 deletions
diff --git a/tests/bot/test_api.py b/tests/bot/test_api.py
index e0ede0eb1..5a88adc5c 100644
--- a/tests/bot/test_api.py
+++ b/tests/bot/test_api.py
@@ -121,7 +121,9 @@ class LoggingHandlerTests(LoggingTestCase):
def test_schedule_queued_tasks_for_nonempty_queue(self):
"""`APILoggingHandler` should schedule logs when the queue is not empty."""
- with self.assertLogs(level=logging.DEBUG) as logs, patch('asyncio.create_task') as create_task:
+ log = logging.getLogger("bot.api")
+
+ with self.assertLogs(logger=log, level=logging.DEBUG) as logs, patch('asyncio.create_task') as create_task:
self.log_handler.queue = [555]
self.log_handler.schedule_queued_tasks()
self.assertListEqual(self.log_handler.queue, [])
diff --git a/tests/helpers.py b/tests/helpers.py
index 8496ba031..8d661513d 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import functools
import inspect
+import logging
import unittest.mock
from typing import Any, Iterable, Optional
@@ -10,6 +11,16 @@ import discord
from discord.ext.commands import Bot, Context
+for logger in logging.Logger.manager.loggerDict.values():
+ # Set all loggers to CRITICAL by default to prevent screen clutter during testing
+
+ if not isinstance(logger, logging.Logger):
+ # There might be some logging.PlaceHolder objects in there
+ continue
+
+ logger.setLevel(logging.CRITICAL)
+
+
def async_test(wrapped):
"""
Run a test case via asyncio.