python - 如何在 Django 单元测试中获取请求对象?

标签 python django django-testing

我有一个函数

def getEvents(eid, request):
    ......

现在我想单独为上述函数编写单元测试(不调用 View )。 那么我应该如何在 TestCase 中调用上述内容。是否可以创建请求?

最佳答案

this solution :

from django.utils import unittest
from django.test.client import RequestFactory

class SimpleTest(unittest.TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.factory = RequestFactory()

    def test_details(self):
        # Create an instance of a GET request.
        request = self.factory.get('/customer/details')

        # Test my_view() as if it were deployed at /customer/details
        response = my_view(request)
        self.assertEqual(response.status_code, 200)

关于python - 如何在 Django 单元测试中获取请求对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277748/

相关文章:

python - 计算并绘制分割掩码像素

python - 使用 kwargs 时如何转义 Python format() 中的冒号?

django - 如何使用 ModelForm 替换图像?

django - 当我不访问它时出现错误 serializer.data

Django:有没有办法从单元测试中计算 SQL 查询?

Django 测试 - 如何为特定应用程序使用不同的设置文件运行测试?

python - 打印二维列表,每个数字右对齐

python - 为什么我的 FPS 相机在滚动?在 Python 中使用欧拉角(不是四元数)实现

djangorest框架序列化器返回名称不是整数

Django 测试 - InternalError : current transaction is aborted, 命令被忽略,直到事务 block 结束