如何在Flask应用程序工厂中测试非视图功能?

如何在Flask应用程序工厂中测试非视图功能?,flask,pytest,Flask,Pytest,我在烧瓶中使用工厂模式。这是我的代码的简化版本: def create_app(): app = Flask(__name__) @app.before_request def update_last_seen(): if current_user.is_authenticated: current_user.update(last_seen=arrow.utcnow().datetime) @app.route('/'

我在烧瓶中使用工厂模式。这是我的代码的简化版本:

def create_app():
    app = Flask(__name__)

    @app.before_request
    def update_last_seen():
        if current_user.is_authenticated:
            current_user.update(last_seen=arrow.utcnow().datetime)

    @app.route('/', methods=['GET'])
    def home():
         return render_template("home.html")

    return app
我正在使用
pytest flask
,我希望能够为上次看到的
update\u
函数编写一个测试

我如何访问该功能?我在client.application(
client
是通过pytest烧瓶自动使用的夹具)中找不到它,也在我通过
conftest.py设置的
app
夹具中找不到它,就像这样:

@pytest.fixture
def app():
    os.environ["FLASK_ENV"] = "test"
    os.environ["MONGO_DB"] = "test"
    os.environ["MONGO_URI"] = 'mongomock://localhost'

    app = create_app()
    app.config['ENV'] = 'test'
    app.config['DEBUG'] = True
    app.config['TESTING'] = True

    app.config['WTF_CSRF_ENABLED'] = False   

    return app
因此,当我运行此测试时:

def test_previous_visit_is_stored_in_session(app, client):
    app.update_last_seen()
我得到的错误是:

    def test_previous_visit_is_stored_in_session(app, client):
>       app.update_last_seen()
E       AttributeError: 'Flask' object has no attribute 'update_last_seen' 
我一直在查看
应用程序。在请求之前也会执行
功能,但遗憾的是没有任何效果。

参考,您可以手动运行请求的预处理

initial_last_seen = current_user.last_seen

with app.test_request_context('/'):
    app.preprocess_request()

    assert current_user.last_seen != initial_last_seen # ...for example