使用 requests-html 在不启动服务器的情况下测试 Flask 应用程序

Use requests-html to test Flask app without starting server

我一直在使用 Flask test_client 对象来测试我的 Web 应用程序。我使用 BeautifulSoup 来解析其中一些调用的 HTML 输出。

现在我想尝试 requests-html instead, but I cannot figure out how to make it work with the Flask test client. The examples all use the request package 获取响应,但 Werkzeug 测试客户端并未进行实际的 HTTP 调用。据我所知,它设置了环境并调用了处理程序方法。

有没有办法在不需要实际服务的情况下完成这项工作运行?

requests-wsgi-adapter provides an adapter to mount a WSGI callable at a URL. You use session.mount() to mount adapters, so for requests-html 你会使用 HTMLSession 并挂载到那个。

$ pip install flask requests-wsgi-adapter requests-html
from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "<p>Hello, World!</p>"
from requests_html import HTMLSession
from wsgiadapter import WSGIAdapter

s = HTMLSession()
s.mount("http://test", WSGIAdapter(app))
r = s.get("http://test/")
assert r.html.find("p")[0].text == "Hello, World!"

使用请求的缺点是您必须在每个要向其发出请求的 URL 之前添加 "http://test/"。 Flask 测试客户端不需要这个。


除了使用请求和请求-html,您还可以告诉 Flask 测试客户端 return 一个为您执行 BeautifulSoup 解析的响应。快速浏览了一下requests-html,我还是更喜欢直接Flask测试客户端和BeautifulSoup API.

$ pip install flask beautifulsoup4 lxml
from flask.wrappers import Response
from werkzeug.utils import cached_property

class HTMLResponse(Response):
    @cached_property
    def html(self):
        return BeautifulSoup(self.get_data(), "lxml")

app.response_class = HTMLResponse
c = app.test_client()
r = c.get("/")
assert r.html.p.text == "Hello, World!"

您还应该考虑使用 HTTPX instead of requests. It's a modern, well maintained HTTP client library that shares many API similarities with requests. It also has great features like async, HTTP/2, and built-in ability to call WSGI applications directly

$ pip install flask httpx
c = httpx.Client(app=app, base_url="http://test")
with c:
    r = c.get("/")
    html = BeautifulSoup(r.text)
    assert html.p.text == "Hello, World!"