gpt4 book ai didi

python - 在Django的unittest中更改Client()的默认域

转载 作者:太空狗 更新时间:2023-10-29 16:53:34 25 4
gpt4 key购买 nike

我正在为 Django View 编写单元测试。

class TestLog(unittest.TestCase):
"""Test for Contact"""
def setUp(self):
self.c = Client()
try:
self.bob = User.objects.create_user("mojo","b@example.com", "bmojo")
except :
print ''

def test_get_emails(self):
response = self.c.get('/text/')
self.assertEqual(response.status_code, 200)


def test_htmlemils(self):
response = self.c.get('/emails/html/upload')
self.assertEqual(response.status_code, 200)

c = Client() 将“http://testserver”作为我要覆盖的域,我想在该测试客户端中添加我的真实域,这是他们自定义测试客户端的方法吗?

最佳答案

Django 的客户端 extends RequestFactory所以你应该能够传入额外的参数作为关键字参数。

尝试:

response = self.c.get('/emails/html/upload', SERVER_NAME="mydomain.com")

关于python - 在Django的unittest中更改Client()的默认域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6291120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com