使用PyTest与拥抱和base_url。

时间:2022-12-25 20:21:46

I have a an api that is setup with

我有一个可以设置的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

and I'm trying to test it with PyTest.

我想用PyTest测试一下。

I'm trying to test the route with

我在试着测试路线

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

How can I test hug routes that have http.base_url configured?

如何测试具有http的拥抱路由。base_url配置?

I get a 404 errors regardless of the route path. I've tried

无论路径如何,我都会得到404错误。我试过了

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

If I remove the hug.API().http.base_url setting then v1/hello-world works fine but my requirement is to have a base_url setup.

如果我删除了hug.API().http。base_url设置然后v1/hello-world运行良好,但是我的要求是设置一个base_url。

I've reviewed the documentation on the official hug github repo and various online sources such as ProgramTalk but I haven't had much success.

我已经看过了官方拥抱github repo的文档以及各种在线资源,比如ProgramTalk,但是我并没有取得太大的成功。

any recommendations?

你有什么推荐吗?

1 个解决方案

#1


2  

You should send your module (myapp) as the first argument to hug.test.get().

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

Then you can use the full path /api/v1/hello-world as the second argument.

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

Here's a minimal working example:

这里有一个最小的工作示例:

# 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

#1


2  

You should send your module (myapp) as the first argument to hug.test.get().

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

Then you can use the full path /api/v1/hello-world as the second argument.

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

Here's a minimal working example:

这里有一个最小的工作示例:

# 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

相关文章