Python 测试未初始化应用程序

Python 测试未初始化应用程序,python,flask,Python,Flask,我有一个Flask服务器,它的server.py设置如下 server.py app = Flask(__name__) app.register_blueprint(mybp) if __name__ == '__main__': app.config.foo = "bar" @mybp.route('/', methods=['POST']) def root(): return app.config.foo @pytest.fixture def client():

我有一个Flask服务器,它的
server.py
设置如下

server.py

app = Flask(__name__)
app.register_blueprint(mybp)
if __name__ == '__main__':
    app.config.foo = "bar"
@mybp.route('/', methods=['POST'])
def root():
    return app.config.foo
@pytest.fixture
def client():
    server.app.config['TESTING'] = True
    client = server.app.test_client()
    client
    yield client

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

app = Flask(__name__)
app.register_blueprint(mybp)
if __name__ == '__main__':
    app.config.foo = "bar"
@mybp.route('/', methods=['POST'])
def root():
    return app.config.foo
@pytest.fixture
def client():
    server.app.config['TESTING'] = True
    client = server.app.test_client()
    client
    yield client

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

app = Flask(__name__)
app.register_blueprint(mybp)
if __name__ == '__main__':
    app.config.foo = "bar"
@mybp.route('/', methods=['POST'])
def root():
    return app.config.foo
@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'

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

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