falcon,AttributeError:'API'对象没有属性'create'

时间:2017-04-07 09:48:05

标签: python python-3.x pytest falconframework

我正在尝试测试我的猎鹰路线,但测试总是失败,看起来我做的都很正确。

我的app.py

import falcon
from resources.static import StaticResource


api = falcon.API()
api.add_route('/', StaticResource())

和我的测试目录tests/static.py

from falcon import testing
import pytest
from app import api


@pytest.fixture(scope='module')
def client():
    # Assume the hypothetical `myapp` package has a
    # function called `create()` to initialize and
    # return a `falcon.API` instance.
    return testing.TestClient(api.create())


def test_get_message(client):
    result = client.simulate_get('/')
    assert result.status_code == 200

请帮忙,为什么我出现AttributeError: 'API' object has no attribute 'create' 错误?感谢。

1 个答案:

答案 0 :(得分:4)

您遗失了create()中的假设 app.py功能。

您的app.py应如下所示:

import falcon
from resources.static import StaticResource

def create():
    api = falcon.API()
    api.add_route('/', StaticResource()) 
    return api

api = create()

然后在您的tests/static.py中应该看起来像:

from falcon import testing
import pytest
from app import create


@pytest.fixture(scope='module')
def client():
    return testing.TestClient(create())

def test_get_message(client):
    result = client.simulate_get('/')
    assert result.status_code == 200