croach / Flask-Fixtures

A simple library for adding database fixtures for unit tests using nothing but JSON or YAML.
MIT License
63 stars 30 forks source link

py.test seems not to be really supported #29

Open mbegoc opened 7 years ago

mbegoc commented 7 years ago

I tryed to use flask-fixtures for testing my project with py.test. It turned out that the only way to make fixtures load is to make the test class inherite from unittest.TestCase. Doing so make py.test run tests as unittest tests and act as a test container rather than the actual underlying test lib (it's at least how I understand what's going on here), and thus all the py.test fixture feature is deactivated. It's a huge issue, since I obviously need several fixture from pytest and pytest-flask to test my app (namely the client fixture). The claim that flask-fixtures support py.test seems to rely only on the fact that py.test can run many test lib like unittest. I think the doc should at least be clearer and warn about that, and maybe it would be even better not to claim to support py.test but just indicate that flask-fixtures tests can be run through py.test.

Anyway, I started to look into a way to make flask-fixtures fully support py.test, and if the project maintainer thinks it can be a good addition to the lib, I will go forward on this feature.

javierseixas commented 5 years ago

Hi! Just wondering if there has been any advance on this, since I'm looking for this functionality.

retr0h commented 4 years ago

Any news @mbegoc ?

Reimirno commented 9 months ago

For any future visitors this is my workaround.

from server.models import db as sqlalchemy_db

@pytest.fixture()
def seeded_db(app):
    from flask_fixtures import load_fixtures_from_file

    with app.app_context():
        sqlalchemy_db.drop_all()
        sqlalchemy_db.create_all()
        sqlalchemy_db.session.rollback()

        ... # prepares seed_files_names and seed_dir_paths

        for filename in seed_files_names:
            load_fixtures_from_file(sqlalchemy_db, filename, seed_dir_paths)

        yield sqlalchemy_db

        sqlalchemy_db.session.expunge_all()
        sqlalchemy_db.drop_all()

I didn't use the flask_fixtures.setup or flask_fixtures.teardown becuase I want to manage context myself. I also disliked how it finds seeding fixture files. But those are not really important.

And here is how you use that pytest fixture in a test case:

def test_seeded_db(seeded_db):
    """
    Test that seeded database works
    We have a User table and a user.json seeding fixture file. So there should be some users in table.
    """
    assert User.query.count() > 0
    # now remove all user
    User.query.delete()
    seeded_db.session.commit()
    assert User.query.count() == 0

def test_seeded_db_rollback(seeded_db):
    """
    Test that the previous test did not affect the seeded database
    """
    assert User.query.count() > 0

Seems to work for me as of now.

If you have question you could reply to this thread.