Django断言错误-302不是302

Django断言错误-302不是302,django,django-testing,Django,Django Testing,我为一个端点设置了一个超级简单的单元测试,该端点通过一个文件接受POST请求,并在成功上传后将用户重定向到一个新页面。本单元的目标是测试它以确保文件上传正常工作 测试.py c = Client() with open('replays/static/test.txt', 'r', ) as f: response = c.post( '/upload/', { 'summoner': 'test user',

我为一个端点设置了一个超级简单的单元测试,该端点通过一个文件接受POST请求,并在成功上传后将用户重定向到一个新页面。本单元的目标是测试它以确保文件上传正常工作

测试.py

c = Client()
with open('replays/static/test.txt', 'r', ) as f:
    response = c.post(
        '/upload/',
        {
            'summoner': 'test user',
            'title': 'Testing title',
            'replay': f
        },
        follow=False
    )

    print(response.status_code)
    print(response.status_code == 302)
    self.assertIs(response.status_code, 302)
输出

$ python manage.py test replays
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
302
True
======================================================================
FAIL: test_create_replay (replays.tests.ReplayCreationTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/project/tests.py", line 52, in test_create_replay
    self.assertIs(response.status_code, 302)
AssertionError: 302 is not 302

----------------------------------------------------------------------
Ran 1 test in 0.173s

FAILED (failures=1)
Destroying test database for alias 'default'...
c = Client()
    with open('replays/static/test.txt', 'r', ) as f:
        response = c.post(
            '/upload/',
            {
                'summoner': 'test user',
                'title': 'Testing title',
                'replay': f
            },
            follow=True
        )

    print(response.status_code)
    print(response.status_code == 200)
    self.assertIs(response.status_code, 200)
$ python manage.py test replays
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
200
True
.
----------------------------------------------------------------------
Ran 1 test in 0.196s

OK
Destroying test database for alias 'default'...
如果我在调用测试客户机的post方法时更改了以下重定向的参数,则响应状态为200时,一切正常

tests.py-跟随重定向

$ python manage.py test replays
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
302
True
======================================================================
FAIL: test_create_replay (replays.tests.ReplayCreationTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/project/tests.py", line 52, in test_create_replay
    self.assertIs(response.status_code, 302)
AssertionError: 302 is not 302

----------------------------------------------------------------------
Ran 1 test in 0.173s

FAILED (failures=1)
Destroying test database for alias 'default'...
c = Client()
    with open('replays/static/test.txt', 'r', ) as f:
        response = c.post(
            '/upload/',
            {
                'summoner': 'test user',
                'title': 'Testing title',
                'replay': f
            },
            follow=True
        )

    print(response.status_code)
    print(response.status_code == 200)
    self.assertIs(response.status_code, 200)
$ python manage.py test replays
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
200
True
.
----------------------------------------------------------------------
Ran 1 test in 0.196s

OK
Destroying test database for alias 'default'...
输出

$ python manage.py test replays
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
302
True
======================================================================
FAIL: test_create_replay (replays.tests.ReplayCreationTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/path/to/project/tests.py", line 52, in test_create_replay
    self.assertIs(response.status_code, 302)
AssertionError: 302 is not 302

----------------------------------------------------------------------
Ran 1 test in 0.173s

FAILED (failures=1)
Destroying test database for alias 'default'...
c = Client()
    with open('replays/static/test.txt', 'r', ) as f:
        response = c.post(
            '/upload/',
            {
                'summoner': 'test user',
                'title': 'Testing title',
                'replay': f
            },
            follow=True
        )

    print(response.status_code)
    print(response.status_code == 200)
    self.assertIs(response.status_code, 200)
$ python manage.py test replays
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
200
True
.
----------------------------------------------------------------------
Ran 1 test in 0.196s

OK
Destroying test database for alias 'default'...

我错过了什么?这似乎不是断言语句的预期行为。我使用的是Django 3.1。

AssertIs
检查
x是否为y
,换句话说,
x
y
指的是同一个对象。但是您可以有两个
int
对象,它们都是
302
,但不是同一个对象

你应使用:

self.assertEqual(302,response.status\u code)
对于小整数,CPython解释器将构造-5到256的int对象,因此使用flyweight模式:

当前的实现为
-5
256
之间的所有整数保留一个整数对象数组,当您在该范围内创建一个int时,实际上只返回对现有对象的引用


这意味着对于-5到256之间的
int
,它将引用同一个对象,对于该范围之外的值,它通常会构造一个新对象。

@ihunter2839-如果我错了,请纠正我,在文件上载测试中,实际文件的上载不应该被模拟吗?在这种情况下,我希望文件完成上传,因为我使用的外部存储需要访问密钥才能完成,测试的一部分是确保密钥是好的。除非有更有效的方法来实现这一点?@ihunter2839-似乎你在测试两件事。身份验证和文件上传。据我所知,I/O绑定调用应该是模拟的。Hrm,只是要明确一点——没有用户身份验证,因为文件上传是公开的,密钥是用于S3存储桶的。如果您指的是应用程序身份验证,那么我想您是正确的,但我不熟悉唯一地测试这一点的技术。@ihunter2839-是的,我指的是应用程序身份验证。我确认self.assertEqual按预期工作,谢谢。但是我不能说我理解self.assertIs为什么在状态为200的情况下有效,但在状态为302的情况下失败。似乎这两种情况的行为应该是相同的?@ihunter2839:因为小整数(通常)与flyweight模式一起工作:对于最大256的值,它将使用
int
对象创建一个小缓存。@ihunter2839:请参见编辑。然而,flyweight模式是一个实现细节,所以您不应该真的认为这(总是)有效,未来的版本可以改变这一点。