python - 将 PyTest 与 hug 和 base_url 结合使用

标签 python pytest hug

我有一个用

设置的 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

我正在尝试使用 PyTest 对其进行测试。

我正在尝试用

测试路线
import pytest
import hug
from myapi import api

...

def test_hello_world_route(self):
    result = hug.test.get(myapp, 'v1/hello-world')
    assert result.status == hug.HTTP_200

如何测试配置了 http.base_url 的 hug 路由?

无论路由路径如何,我都会收到 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 存储库和各种在线资源(如 ProgramTalk)上的文档,但我没有取得太大成功。

有什么建议吗?

最佳答案

您应该将您的模块 (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

.

# tests.py

import hug
import myapp


def test_hello_world_route():
    result = hug.test.get(myapp, '/api/v1/hello-world')
    assert result.status == hug.HTTP_200

.

# run in shell
pytest tests.py

关于python - 将 PyTest 与 hug 和 base_url 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49166424/

相关文章:

python - 为什么点积在 dask 中比在 numpy 中慢

python - 用拥抱和女服务员记录

错误识别的 Python 版本

python - 当我看到特殊的 eol 字符时,如何使用 Python 识别它?

python - 如何让 pytest 运行 doctests 以及正常的测试目录?

python-3.x - 如何对结果基于 while 循环中包含的条件语句的函数进行单元测试

python - 如果特定测试失败,则剩余的 pytest 测试失败

python - 如何使用 POST 方法发送 pandas 数据帧并在 Hug/其他 REST API 框架中接收它? pickle.loads 发送后无法取消pickle

python - 处理 Python 中可以 str 或 list 的函数参数

python - scikit 特征重要性选择经验