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

Add an example on how to share session fixture data to README #483

Merged
merged 2 commits into from Nov 4, 2019

Conversation

nicoddemus
Copy link
Member

As discussed in #385

@nicoddemus nicoddemus merged commit 2c6bc13 into pytest-dev:master Nov 4, 2019
@nicoddemus nicoddemus deleted the share-fixture branch November 4, 2019 19:38
@larsblumberg
Copy link

larsblumberg commented Nov 29, 2019

Great to have this MR! I want to throw in code that we're using in your product that makes sure that only one worker sets up the database (we're using SQLAlchemy in the below example). Maybe this MR can profit from our solution which is slightly less complex but comes at the expense to use the external library posix_ipc for creating a shared Semaphore:

from posix_ipc import O_CREAT, Semaphore

def pytest_configure(config):
    from fahrtwind.models.meta import Base
    # As we run tests in parallel, only let the first test runner create the schema to avoid collision
    # TODO: Write a blog post about this technique
    with Semaphore('/fahrtwind-testing', flags=O_CREAT, initial_value=1):
        engine = create_engine(environ['TESTING_DB_URL'])
        if not engine.table_names():
            Base.metadata.create_all(engine)

This method is contained in the root conftest.py and is run at the start-up of eacher worker. This approach might be interesting to you if pytest accepts to add the external dependency.

Edit: Just noting that this MR is already merged anyway.

@nicoddemus
Copy link
Member Author

nicoddemus commented Dec 3, 2019

Hi @larsblumberg,

Thanks for sharing! I'm not sure this would make sense in pytest-xdist though as posix_ipc is not Windows compatible.

But I think it would be worthwhile to add it as an example next to this one in the README (with the caveat that it only works on Linux), would you like to open a PR?

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

Successfully merging this pull request may close these issues.

None yet

2 participants