2

我正在 Django 在 GET API 上编写一个测试用例。我只想通过第一次测试。这是我的代码。

class InventoryItemDetailTestCase(APITestCase):
    """
        This is the API test case for the reserve/express detail api
    """
    def setUp(self):
        self.resource_uri = '/api/v2/inventory/inventory_item_detail/1233/'

    def test_inventory_item_detail_route(self):
        """
            Test to set routing for item_detail
        """
        route = resolve('/api/v2/inventory/inventory_item_detail/1233/')
        self.assertEqual(route.func.__name__, 'InventoryItemDetails')

    def test_inventory_item_detail_data(self):
        """
            Test case to check the response json of the reserve/express API
        """
        response = self.client.get('/api/v2/inventory/inventory_item_detail/1233/')
        self.assertEqual(response.status_code, 200)

在这里,我使用 client.get 提出请求。但它给了我错误

为别名“默认”创建测试数据库...

F

失败:test_inventory_item_detail_data (inventory.tests.functional_tests.InventoryItemDetailTestCase)

回溯(最近一次通话最后):文件“/Users/chitrankdixit/Documents/work/flyrobe/flyrobe-django/project/inventory/tests/functional_tests.py”,第 131 行,在 >test_inventory_item_detail_data self.assertEqual(response.status_code, 200)断言错误:400!= 200


在 0.135 秒内运行 1 次测试

失败(失败=1)

我试图用它pdb.set_trace()来找出即将发生的错误,并且在我运行时我已经知道了

self.client.get('/api/v2/inventory/inventory_item_detail/1233/')

我收到这个错误

*** KeyError:“内容类型”

我尝试提供一个额外的参数名称content_type,例如

self.client.get('/api/v2/inventory/inventory_item_detail/1233/', content_type='application/json')

但我仍然得到同样的错误。我能够单独运行 API,并且我的 API 正在获取正确的响应。如果有人在此之前经历过,请告诉我。

4

2 回答 2

1

传递给的标头名称client.get应遵循CGI规范。来自文档

通过**extra发送的标头应遵循CGI规范。例如,模拟从浏览器到服务器的 HTTP 请求中发送的不同“Host”标头应作为HTTP_HOST传递。

这意味着,您应该指定HTTP_CONTENT_TYPE而不是content_type.

于 2016-03-21T12:25:48.303 回答
0

我解决了测试问题,我没有制作测试用例中使用的所需模型的实例,所以我只需要更新setUp()方法,现在我的设置方法看起来像这样。

def setUp(self):
        self.warehouse = Warehouse.objects.create(
            location_name = 'Mumbai',
            address = 'D1/D2 tex center Chandivali',
        )
        self.supplier = Supplier.objects.create(
            name = "John Doe",
            website = "johndoe@johndoe.com",
            address = "USA, San francisco , CA"
        )
        self.procurement_detail = ProcurementDetail.objects.create(
            invoice_name = "Indore Mill",
            invoice_number = "14235",
            details = "Cloth from Central India",
            total_cost_price = "0.00",
            payment_method = "CASH",
            supplier_id = self.supplier.id,
            is_paid = "True"
        )
        self.inventory_category = InventoryCategory.objects.create(
            name = "party time"
        )
        self.color = Color.objects.create(
            name = "Green",
            hex_val = "#007089"
        )
        self.occasion = Occasion.objects.create(
            name = "BDay"
        )
        self.photo_shoot = PhotoShoot.objects.create(
            name = 'front 1',
            details = 'This is the front facing image',
            model_size = {'name':'abcd'}
        )
        self.brand = Brand.objects.create(
            name = "ZARA1"
        )
        self.parent_item_description = ParentItemDescription.objects.create(
            features = 'Indian clothing at its best',
            style_tip = 'This is style tip',
            fitting_notes = 'This is fitting notes',
            long_description = 'This is the Long description'
        )
        self.parent_item = ParentItem.objects.create(
            parent_id = "1003",
            mrp = "0.00",
            photo_shoot_id = self.photo_shoot.id,
            description_id = self.parent_item_description.id,
            cost_price = "0.00",
            rental_price = "0.00",
            discount_price = "0.00",
            discount_percent = "0.00",
            security_deposit = "0.00",
            material_percentage = '"cotton"=>"20"',
            mode = "Express",
            brand_id = self.brand.id,
            features = "THe best feature the best it is ",
            size = ['S','M','L'],
            online_link_url = "http://chitrank-dixit.github.io/",
            visibility_order_index = 1,
            category = 'Express clothing',
            inventory_category_id = self.inventory_category.id,
            popularity_index = 90,
            item_domain = "CLT",
            gender = "FML"
        )
        self.inventory_item = InventoryItem.objects.create(
            warehouse_id = self.warehouse.id,
            parent_item_id = self.parent_item.id,
            size = ['S','M','L'],
            procurement_detail_id = self.procurement_detail.id,
            size_range_id = 2,
            product_tag = {'name':'abcd'},
            short_description = 'abcd',
            is_virtual = False,
            warehouse_stack_id = "12345",
            reason_for_out_stock = "NA",
            status = "ILV"
        )
        self.inventory_item.color.add(self.color)
        self.inventory_item.occasion.add(self.occasion)
        self.resource_url = '/api/v2/inventory/inventory_item_detail/'+str(self.inventory_item.parent_item.parent_id)+'/'

现在,setUp 中创建的实例可以在下面定义的测试用例中使用。

于 2016-04-07T13:01:50.290 回答