代码之家  ›  专栏  ›  技术社区  ›  Carpetfizz

Flask Pytest未初始化应用程序[重复]

  •  -1
  • Carpetfizz  · 技术社区  · 6 年前

    我有一个烧瓶服务器 server.py 按以下方式设置

    服务器.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
    

    测试.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 回复  |  直到 6 年前
        1
  •  0
  •   Tim Thompson    6 年前

    您已经在测试文件中初始化了配置值“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 是为了 /