Suppress HTTP response warning messages from WSGI during test execution #3171

Closed
opened 2025-12-29 18:26:18 +01:00 by adam · 1 comment
Owner

Originally created by @jeremystretch on GitHub (Jan 16, 2020).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

Prevent the WSGI server from logging warnings regarding HTTP response codes 400 or higher during execution of the test suite. Example test output is below:

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................14:55:50: Conflict: /api/ipam/vlans/28/
..........................................................................................................................................................................14:56:17: Bad Request: /api/ipam/vlans/
.14:56:17: Bad Request: /api/ipam/vlans/
.14:56:17: Bad Request: /api/ipam/vlans/
..14:56:17: Bad Request: /api/ipam/vlans/
...........................................................................14:56:19: Bad Request: /api/virtualization/virtual-machines/
...................................................
----------------------------------------------------------------------
Ran 1017 tests in 77.226s

Justification

These warnings disrupt the test suite output and suggest that something may be wrong even though these response codes are being generated intentionally for testing purposes.

The warnings are being logged by the WSGI server itself rather than the test suite itself, so working around this may take some creativity.

Originally created by @jeremystretch on GitHub (Jan 16, 2020). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes Prevent the WSGI server from logging warnings regarding HTTP response codes 400 or higher during execution of the test suite. Example test output is below: ``` Creating test database for alias 'default'... System check identified no issues (0 silenced). .............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................14:55:50: Conflict: /api/ipam/vlans/28/ ..........................................................................................................................................................................14:56:17: Bad Request: /api/ipam/vlans/ .14:56:17: Bad Request: /api/ipam/vlans/ .14:56:17: Bad Request: /api/ipam/vlans/ ..14:56:17: Bad Request: /api/ipam/vlans/ ...........................................................................14:56:19: Bad Request: /api/virtualization/virtual-machines/ ................................................... ---------------------------------------------------------------------- Ran 1017 tests in 77.226s ``` ### Justification These warnings disrupt the test suite output and suggest that something may be wrong even though these response codes are being generated intentionally for testing purposes. The warnings are being logged by the [WSGI server itself](https://github.com/django/django/blob/master/django/core/handlers/base.py#L77-L82) rather than the test suite itself, so working around this may take some creativity.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:26:18 +01:00
adam closed this issue 2025-12-29 18:26:18 +01:00
Author
Owner

@hSaria commented on GitHub (Jan 16, 2020):

Something like this would do it:

import logging
from contextlib import contextmanager

@contextmanager
def logger_disabled(logger_name):
    logger = logging.getLogger(logger_name)
    current_state = logger.disabled
    logger.disabled = True
    yield
    logger.disabled = current_state


with logger_disabled('django.request'):
    # do the dirty work

You could also disable the individual handler console temporarily

@hSaria commented on GitHub (Jan 16, 2020): Something like this would do it: ``` import logging from contextlib import contextmanager @contextmanager def logger_disabled(logger_name): logger = logging.getLogger(logger_name) current_state = logger.disabled logger.disabled = True yield logger.disabled = current_state with logger_disabled('django.request'): # do the dirty work ``` You could also disable the individual handler `console` temporarily
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3171