我正在使用GET API在Django上编写一个测试用例。我只想做第一次测试。这是我的代码。
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来发出请求。但它给了我错误
为别名'default'创建测试数据库......
˚F
FAIL: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.135s中进行1次测试
失败(失败= 1)
我尝试使用pdb.set_trace()
来确定即将发生的错误,并且在运行时我想到了
self.client.get('/api/v2/inventory/inventory_item_detail/1233/')
我收到此错误
*** KeyError:'content-type'
我尝试提供额外的参数名称content_type
,如
self.client.get('/api/v2/inventory/inventory_item_detail/1233/', content_type='application/json')
但我仍然得到同样的错误。我可以单独运行API,我的API正在获取正确的响应。如果有人经历过这个,请告诉我。
答案 0 :(得分:1)
传递给client.get
的标头名称应遵循CGI规范。来自docs:
通过 ** extra 发送的标头应遵循CGI规范。例如,模拟从浏览器到服务器的HTTP请求中发送的不同“主机”标头应作为 HTTP_HOST 传递。
这意味着,您应指定HTTP_CONTENT_TYPE
而不是content_type
。
答案 1 :(得分:0)
我通过测试解决了这个问题,我没有在测试用例中使用所需模型的实例,所以我只需更新setUp()
方法,现在我的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中的实例可用于下面定义的测试用例。