3

我有一个设置的 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配置的拥抱路由?

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 和各种在线资源(例如 ProgramTalk)上的文档,但我没有取得太大的成功。

有什么建议吗?

4

1 回答 1

3

您应该将您的模块 ( 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
于 2018-03-08T15:23:07.903 回答