知海

工具:Schemathesis(schema 测试)

graphql-github-io-zh-Hans生态项目与库

工具:Schemathesis(schema 测试)

Schemathesis 是一个现代的 API 测试工具,适用于使用 Open API 和 GraphQL 规范构建的 Web 应用。

通过 pip 安装 Schemathesis:

bash 复制代码
pip install schemathesis

然后,创建文件 test_api.py,内容如下,并将 URL 的值替换为你自己的 GraphQL 端点 URL:

python 复制代码
from hypothesis import settings
import schemathesis

URL = "https://your.app.com/graphql"
schema = schemathesis.graphql.from_url(URL)

@schema.parametrize()
@settings(deadline=None)
def test_api(case):
    response = case.call()
    case.validate_response(response)

然后运行 pytest test_api.py。请注意,你可以使用任何编程语言编写你的应用;该工具将通过 HTTP 与它通信。

Schemathesis 会根据 schema 自动生成有效查询,并使失败的用例最小化。

例如,对 https://bahnql.herokuapp.com/graphql 运行上述代码会发现,执行 { search(searchTerm: "") { stations { name } } } 查询会导致服务器错误:

复制代码
{"errors":[{"message":"Cannot read property \'city\' of undefined","locations":[{"line":1,"column":28}],"path":["search","stations"]}],"data":null}

帮助我们改进文档

发现翻译问题或内容错误?请告诉我们。