gpt4 book ai didi

How can I test some Python code which makes use of the requests package?(我如何测试一些使用请求包的Python代码?)

转载 作者:bug小助手 更新时间:2023-10-25 13:05:18 26 4
gpt4 key购买 nike



I have a Python script which uses the requests package to make a get request to a url to download a (large) file.

我有一个Python脚本,它使用Request包向URL发出GET请求,以下载(大)文件。


Becuase the file is large, I cannot use this in the current state for testing. (It takes about 20 minutes to download.)

因为文件很大,所以我不能在当前状态下使用它进行测试。(下载大约需要20分钟。)


Is there a way I can use another Python package to setup a local process/server which can provide a "fake" (or just shorter) version of this file for testing purposes? It doesn't matter if I have to change the url for testing, and indeed I would expect to do so.

有没有一种方法可以使用另一个Python包来设置一个本地进程/服务器,该进程/服务器可以提供该文件的“假”(或更短)版本用于测试目的?如果我必须更改URL以进行测试,这并不重要,事实上,我也希望这样做。


Under test conditions, the application can just ask for a localhost address. localhost:/test_endpoint, or something.

在测试条件下,应用程序只需请求本地主机地址。LOCALHOST:/TEST_ENDPOINT等。


The code I use to make the request to download the file is just two lines.

我用来请求下载文件的代码只有两行。


url = 'http://blaa_blaa.txt'
request = requests.get(url, allow_redirects=True)

更多回答

Are you wanting to test the actual process of downloading the file?

您想测试下载文件的实际过程吗?

@JohnGordon Yes - what I imagine is using some kind of Python package which can act as a server on localhost, which can then serve a cut-down version of the actual file. Then the application under test can just point to that localhost address and pull the file from there?

@JohnGordon是的-我想象的是使用某种类型的Python包,它可以在本地主机上充当服务器,然后可以提供实际文件的精简版本。那么被测试的应用程序可以只指向该本地主机地址并从那里拉出文件吗?

优秀答案推荐

I solved this by writing a short application using Flask to act as a fake versin of the actual server the production application would query.

我用Flask语言编写了一个简短的应用程序,作为生产应用程序要查询的实际服务器的假版本,从而解决了这个问题。


Flask uses port 5000 by default, so the URL needed to be changed to point at the correct host and this port number.

默认情况下,FlASK使用端口5000,因此需要更改URL以指向正确的主机和此端口号。


Example code:

示例代码:


#!/usr/bin/env python3

import flask

app = flask.Flask(__name__)

@app.route('/')
def index():
return 'go to /filename.txt to download file'

@app.route('/filename.txt')
def filename_txt():

with open('filename-test.txt', 'r') as input_file:

data = input_file.read()
return data

if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port=5000)

更多回答

That's not the best solution. You should remove hard dependency on requests from your code instead and abstract that by introducing "middleman", so i.e your code will expect class implementing Downloader interface (yes, I know we talk python but that's general principle). So at runtime you will give it Downloader which uses requests under the hood, but for testing you can give it Downloader that does whateevr you want (i.e. load from file, generate data etc, etc)

这不是最好的解决方案。相反,你应该从你的代码中去除对请求的硬依赖,并通过引入“中间人”来抽象它,这样你的代码将期望类实现DownLoader接口(是的,我知道我们说的是Python,但这是一般原则)。所以在运行时,你可以给它下载器,它在幕后使用请求,但为了测试,你可以给它下载器,它可以做任何你想做的事情(例如,从文件加载,生成数据等)

@MarcinOrlowski I understand your point - but this isn't the appropriate solution. I am not trying to test the downstream logic. I am trying to test, quite literally, does the application connect to a remote endpoint every hour and download some data. Trust me when I say I would be the first person to weild the Design Patterns hammer should the situation have given any excuse to use it.

@MarcinOrlowski我理解你的观点--但这不是合适的解决方案。我不是想测试下游的逻辑。我正在尝试测试应用程序是否每小时连接到远程终结点并下载一些数据。相信我,当我说我会是第一个使用设计模式锤子的人,如果情况给了任何借口使用它的话。

@MarcinOrlowski Had I been writing a unit test for some logic block used in the application this would be the correct way to go about it, but that isn't what's happening here.

@MarcinOrlowski如果我正在为应用程序中使用的某个逻辑块编写单元测试,这将是正确的方法,但这里发生的情况并非如此。

Still, technicality aside it still makes little sense for me. I am trying to test, quite literally, does the application connect to a remote endpoint every hour and download some data Then you are not testing, but you are monitoring, no? What is your test proven if you do not control the endpoint? That you can deal with the file you serve. That's it. If the real ednpoint hand you garbage you then what? if connection is down then what? For me it looks you should rethink what you want to achieve and you simply asked wrong question.

尽管如此,撇开技术性不谈,它对我来说仍然没有什么意义。我试图测试,相当字面上,应用程序是否每小时连接到一个远程端点并下载一些数据然后你不是在测试,但你是在监视,不是吗?如果你不控制端点,你的测试证明了什么?你可以处理你提供的文件。就这样。如果真的把你当垃圾扔了怎么办?如果连接中断了怎么办?在我看来,你应该重新考虑你想要实现什么,你只是问了错误的问题。

I don't understand why people on this site insist on arguing and insisting that they know more about what I'm currently working on than I do, given that they have no idea what I am doing. I've been doing this job for 10 years. Weirdly enough, I know what I am trying to achieve here. @MarcinOrlowski If you check the question you will see that this is exactly what I was asking for.

我不明白为什么这个网站上的人坚持争论并坚持他们比我更了解我目前正在做的事情,因为他们根本不知道我在做什么。我做这份工作已经10年了。奇怪的是,我知道我在这里试图实现什么。@MarcinOrlowski如果你检查问题,你会发现这正是我所要求的。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com