bar_factory parameter foo_inst is evaluated at the time of module import. At that time usually databases still point to local dev db, not test db.
You have to rewrite default value so that it's evaluated much later. For example instead of passing instance, pass a callable and call that within your bar_factory.