factory_boy icon indicating copy to clipboard operation
factory_boy copied to clipboard

Setup for flask - unexpected behavior probably due to session management

Open lmtani opened this issue 3 years ago • 0 comments

The problem

I'm trying to use flask, flask-sqlalchemy, factory-boy, and pytest, but I'm having some kind of trouble with sessions (I think). Could anyone help me?

I've prepared a minimal example to show the problem I'm having. Tests are working only because I put the factory-boy class (named ExampleFactory) inside a pytest fixture, but I would like to have it in one module alone like described in the docs.

Here is the minimal code example of my situation:

https://gist.github.com/lmtani/53a5723795b3ee9f3ad4ae52cecbec75#file-test_factory_boy-py

When I use the factory outside the pytest fixture the second test will unexpectedly present "metrics" field.

    def test_should_not_contain_metrics(app, db_):
        # Arrange
        ExampleFactory._meta.model = ExampleDB
        ExampleFactory._meta.sqlalchemy_session = db_.session
        rec = ExampleFactory()
>       assert "metrics" not in rec.outputs
E       AssertionError: assert 'metrics' not in {'metrics': {'a': 1}}
E        +  where {'metrics': {'a': 1}} = <ExampleDB (transient 140452939208496)>.outputs

Proposed solution

It would be nice to have an example of setup when using Flask,

lmtani avatar Feb 24 '22 12:02 lmtani