Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py.test seems not to be really supported #29

Open
mbegoc opened this issue Jan 28, 2017 · 3 comments
Open

py.test seems not to be really supported #29

mbegoc opened this issue Jan 28, 2017 · 3 comments

Comments

@mbegoc
Copy link

mbegoc commented Jan 28, 2017

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
Copy link

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

@retr0h
Copy link

retr0h commented Jul 10, 2020

Any news @mbegoc ?

@Reimirno
Copy link

Reimirno commented Nov 22, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants