我在Django应用中进行了一个测试,运行良好,但我想禁用显示控制台日志,如.Bad Request: /api/v1/users/register/
我的一个测试代码
def test_user_register_username_error(self):
data = {
'username': 'us',
'email': 'mail@mail.mail',
'password': 'pass123123',
'password_again': 'pass123123'
}
url = self.register_url
response = client.post(url, data=data)
self.assertEqual(response.status_code, 400)
self.assertFalse(User.objects.filter(username='us').first())
控制台输出
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.Bad Request: /api/v1/users/register/
----------------------------------------------------------------------
Ran 1 tests in 0.414s
OK
一切工作正常,但我想禁用Bad Request: /api/v1/users/register/
输出到控制台。我仔细检查,没有print
或logging
函数,可能会记录到控制台。
如何在测试时禁用Bad Request: /api/v1/users/register/
日志记录之类的消息
编辑
为了使问题更容易理解,下面是当前的控制台输出:
Found 22 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...........Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/register/
..Bad Request: /api/v1/users/register/
.Bad Request: /api/v1/users/activate/
.Bad Request: /api/v1/users/login/
.Bad Request: /api/v1/users/recovery/
Bad Request: /api/v1/users/recovery/
.Bad Request: /api/v1/users/register/
.
----------------------------------------------------------------------
Ran 22 tests in 6.350s
OK
而我所期望的是:
Found 22 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
......................
----------------------------------------------------------------------
Ran 22 tests in 6.350s
OK
1条答案
按热度按时间zzlelutf1#
您可以只记录测试期间的错误,并在测试完成后返回正常的日志记录级别。