LiveServerTestCase在Django视图中挂在python-requests调用后 [英] LiveServerTestCase hangs at python-requests post call in django view

查看:86
本文介绍了LiveServerTestCase在Django视图中挂在python-requests调用后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用我创建的REST API的Django应用。目的是通过Web应用程序证明api用例。在我看来,因此我使用python-requests库调用api,如下所示:

I'm writing a Django app that uses a REST api I created. The purpose is to prove api use cases using the web app. In my view, I therefore call the api using the python-requests library like so:

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 })

我已使用单元测试验证了POST到/ entry /的有效数据结果在正确的数据库更新中。

I have verified using Unit Tests that POSTing to /entries/ with valid data results in the correct database updates.

url = '/entries/'
#verify initial db state
data = { 'fieldA': value1, 'fieldB': value2 }
response = self.client.post(url, data, format='json')
# verify db was updated

在我的功能测试中,我使用LiveServerTestCase并与Form交互。测试提交表单后,浏览器选项卡的标题中显示 Connecting ...,并且测试用例挂起。当我直接与数据库进行交互而不是使用请求调用api时并非如此,因此这一定是造成延迟的原因。

In my Functional Tests, I use LiveServerTestCase and interact with the Form. When the test submits the form, the browser tab shows "Connecting..." in the title and the test case hangs. It wasn't so when I was directly interacting with the database instead of calling the api using requests, so that must be the source of the delay.

关于LiveServerTestCase的工作方式,我在这里不了解吗?

Is there something about how LiveServerTestCase works that I'm not understanding here?

推荐答案

测试完成后,请求被挂起,因为您是通过 requests 库发出请求的,而不是官方测试客户端描述的此处

The request is hanging after the test is complete because you're making the request via the requests library and not the official test client described here.

尽管您甚至不需要执行此操作,但直接测试API而不通过Django启动Web服务器更有意义。

You don't even need to do this though, it makes more sense to test the API directly and not spin up a web server through Django.

这是文档LiveServerTestCase 的描述。 #liveservertestcase rel = nofollow>此处

This is a decription of LiveServerTestCase from the documentation here


LiveServerTestCase
设置中在后台启动实时Django服务器,并在拆卸时将其关闭。这允许使用自动化的
测试客户端(例如Selenium客户端)在浏览器中执行
系列功能测试,并模拟真实用户的
操作。

LiveServerTestCase launches a live Django server in the background on setup, and shuts it down on teardown. This allows the use of automated test clients such as, for example, the Selenium client, to execute a series of functional tests inside a browser and simulate a real user’s actions.

如果您想测试API本身,甚至不需要初始化Django应用程序。

There's no need to even initalise the Django application if you want to test the API itself.

一种我喜欢遵循的用于解决此类问题的测试方法是组件集成方法,该方法在Wikipedia上进行了描述此处

One method for testing I like to follow for problems like this is the component integration methodology, sort of described on Wikipedia here.

在此示例中,我们将与服务层(API)分开测试Django应用程序(前端)。在测试前端时,您仍然可以使用API​​,但是存在分隔来定义如何编写测试。

In this example we would test the Django application (the front end) separately from the service layer (the API). You may still use the API's when testing the front end, but the separation exists to define how you write the tests.

由于API是 Flask 应用程序中,我会使用此处

Since the API is a Flask application, I would use Flasks built in testing tools described here.

该API与Django无关,是的,它已被Django应用程序使用,并且该应用程序对该API有着严格的依赖性,但是您重新想专门测试API本身。

The API has nothing to do with Django, yes it is consumed by a Django application, and that application has a hard dependency on that API, but you're wanting to specifically test the API itself.

为帮助进行UI测试,您可以将API用作测试装置/ setUp 例程,以免于使用UI添加执行所需的任何测试数据。但是,如果您想测试API本身,那么由于上述问题,通过Django客户端执行该操作将无法正常工作。

To aid in UI testing, you could use the API as part of your test fixture/setUp routine to save yourself from using the UI to add any test data needed for execution. However, if you're wanting to test the API itself then doing it through the Django client is not going to work due to the issue mentioned above.

这篇关于LiveServerTestCase在Django视图中挂在python-requests调用后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆