当我测试API时,有没有办法指定url的完整路径?现在我是这样做的:
def test_product_types_retrieve(self):
self.relative_path = '/api/api_product/'
response = self.client.get(self.relative_path + 'product_types/')
我应该将relative_path部件添加到每个请求中,但我想设置它,例如在setUp函数中。如果没有self.relative_path,我将得到http://localhost:8000/product_types/而不是http://localhost:8000/api/api_product/product_types/
我的项目结构如下,每个应用程序接口都有自己的带有own模式设置的urls.py。
发布于 2021-11-10 15:11:47
您可以这样做,然后在setUp中设置相对路径,并从call_api调用可以传递args和kwargs的api。
然后,如果在测试中需要一个不同的relative_path,您可以在该测试上设置它,然后仍然调用call_api。
class ExampleTestCase(TestCase):
def setUp(self):
self.relative_path = '/api/api_product/'
def call_api(self, endpoint):
return self.client.get(self.relative_path + endpoint)
def test_product_types_retrieve(self):
response = self.call_api('product_types/')
def test_requires_different_path(self):
self.relative_path = '/api/api_product/v1/'
response = self.call_api('product_types/')
https://stackoverflow.com/questions/69915429
复制相似问题