【发布时间】:2014-02-11 00:26:16
【问题描述】:
我正在编写一个使用我创建的 REST api 的 Django 应用程序。目的是证明使用 web 应用程序的 api 用例。在我看来,我因此使用 python-requests 库调用 api,如下所示:
def my_view_method(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
data = form.cleaned_data
data_to_post = {
'fieldA': data.get('fieldA_in_form'),
'fieldB': data.get('fieldB_in_form'),
}
post_url = "http://%s/%s/" % (request.get_host(), 'entries')
logger.info("request api url: "+ post_url)
r = requests.post(post_url, data=data_to_post)
return HttpResponseRedirect('/')
else:
form = MyForm()
return render(request, 'myview.html', { 'form': form })
我已经使用单元测试验证了使用有效数据发布到 /entries/ 会导致正确的数据库更新。
url = '/entries/'
#verify initial db state
data = { 'fieldA': value1, 'fieldB': value2 }
response = self.client.post(url, data, format='json')
# verify db was updated
在我的功能测试中,我使用 LiveServerTestCase 并与表单交互。当测试提交表单时,浏览器选项卡在标题中显示“正在连接...”,测试用例挂起。当我直接与数据库交互而不是使用请求调用api时,情况并非如此,所以这一定是延迟的来源。
关于 LiveServerTestCase 的工作原理是否有一些我在这里不理解的地方?
【问题讨论】:
标签: python django python-requests functional-testing