我试图用pytest
测试我的Flask
API,但我遇到了以下错误:AttributeError: 'PosixPath' object has no attribute 'lstrip'
。当我向/authentication/users
发布请求时,它被提出了。当我在用户模型中使用Postman
和unit tests
时,端点工作得很好(它们不使用client
(。这是我的代码:
# conftest.py
# ... imports here ...
@pytest.fixture
def client():
app.config.from_object(TestSettings)
with app.test_client() as client:
with app.app_context():
# starts connection with tests db
db = MongoEngine(app)
yield client # Do the tests
with app.app_context():
# drop db after tests
db.connection.drop_database('test')
我的测试使用client
# integrations/test_auth_endpoints.py
def test_create_user(client):
"""
GIVEN the /authentication/users endpoint
WHEN a POST request is made
THEN check the status code, message and data of response
"""
usr_data = {
"nome": "ana",
"cpf": "12345678913",
"email": "ana@gmail.com",
"password": "my-secret.@"
}
req = client.post('/authentication/users', data=usr_data) # <-- AttributeError here
assert req.status == 201
那么我做错了什么?
经过几个小时的调试。我发现我做错了什么。在我的设置中,我设置了
APPLICATION_ROOT = Path(__file__).parent.parent
我错误地假设APPLICATION_ROOT
是项目根,它返回了一个PositionPath对象。