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 を使用してリクエストを行っています。しかし、それは私にエラーを与えます

エイリアス 'default' のテスト データベースを作成しています...

不合格: 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 つのテストを実行

FAILED (失敗 = 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

に渡されるヘッダーの名前は、CGI仕様client.getに従う必要があります。ドキュメントから:

**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 に答える