Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

falcon, AttributeError: 'API' object has no attribute 'create'

I'm trying test my falcon routes, but tests always failed, and looks like I make all things right.

my app.py

import falcon
from resources.static import StaticResource


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

and my test directory 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

Help please, why I got AttributeError: 'API' object has no attribute 'create' error? Thanks.


1 Answers

You are missing the hypothetical create() function in your app.py.

Your app.py should look like the following:

import falcon
from resources.static import StaticResource

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

api = create()

Then in your tests/static.py should look like:

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
like image 175
joarleymoraes Avatar answered Oct 24 '25 21:10

joarleymoraes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!