烧瓶pytest无法初始化应用

时间:2018-07-12 21:33:58

标签: python flask

我有一个Flask服务器,它以以下方式设置了server.py

server.py

app = Flask(__name__)
app.register_blueprint(mybp)
if __name__ == '__main__':
    app.config.foo = "bar"

mybp.py

@mybp.route('/', methods=['POST'])
def root():
    return app.config.foo

test.py

@pytest.fixture
def client():
    server.app.config['TESTING'] = True
    client = server.app.test_client()
    client
    yield client

def testbp(client):
    client.post('/mybp')

运行测试时,出现以下错误

E       AttributeError: 'Config' object has no attribute 'foo'

因为运行测试时尚未初始化配置。

1 个答案:

答案 0 :(得分:0)

您已经在测试文件中初始化配置值“ TESTING”。那么为什么不在那里也为测试初始化​​配置

@pytest.fixture
def client():
    server.app.config.foo = "bar"
    server.app.config['TESTING'] = True
    client = server.app.test_client()
    client
    yield client

def testbp(client):
    client.post('/mybp')

您也要发布到端点/mybp,但您在mybp.py中的端点是/