【问题标题】:Flask pytest not initializing app [duplicate]Flask pytest没有初始化应用程序[重复]
【发布时间】:2018-12-21 05:24:38
【问题描述】:

我有一个 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'

因为运行测试的时候配置还没有初始化。

【问题讨论】:

    标签: python flask


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-10-21
      • 2019-02-08
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多