将PyTest与hug和base-u一起使用

2024-05-12 21:33:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个api,是用

import hug
API = hug.API(__name__).http.base_url='/api'

@hug.get('/hello-world', versions=1)
def hello_world(response):
    return hug.HTTP_200

我试着测试一下。在

我试着用

^{pr2}$

如何测试配置了http.base_url的拥抱路由?在

不管路由路径如何,我都会得到一个404错误。我试过了

  • /api/v1/hello-world
  • api/v1/hello-world
  • v1/hello-world
  • /v1/hello-world

如果我删除hug.API().http.base_url设置,那么v1/hello-world可以正常工作,但是我的要求是有一个base_url设置。在

我已经看过了官方的hug github repo的文档和各种在线资源,比如programmtalk,但是我没有取得太大的成功。在

有什么建议吗?在


Tags: nameimportapihttpurl路由helloworld
1条回答
网友
1楼 · 发布于 2024-05-12 21:33:52

您应该将模块(myapp)作为hug.test.get()的第一个参数。在

然后可以使用完整路径/api/v1/hello-world作为第二个参数。在

下面是一个最小的工作示例:

# myapp.py

import hug

api = hug.API(__name__).http.base_url='/api'

@hug.get('/hello-world', versions=1)
def hello_world(response):
    return hug.HTTP_200

一。在

^{pr2}$

一。在

# run in shell
pytest tests.py

相关问题 更多 >