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

Pytest Fixture to do same as TenantTestCase #589

Open
softinio opened this issue Dec 22, 2018 · 12 comments
Open

Pytest Fixture to do same as TenantTestCase #589

softinio opened this issue Dec 22, 2018 · 12 comments

Comments

@softinio
Copy link

I would like to move our Django Project using this project to use Pytest for testing. Unfortunatley the current TenantTestCase and FastTenantTestCase does not work with Pytest as it doesn't create the tenant schema and tries to use public schema for everything.

See: pytest-dev/pytest-django#633

Be great if a pytest fixture could be added to this project as an alternative to TenantTestCase so we can use pytest easily with this project.

I am happy to help with this issue and contribute this to the project but would like so guidance from the project owner if possible.

Many thanks.

@rj76
Copy link

rj76 commented Dec 24, 2018

I'm using this project with pytest. It took some work to figure out how to set it up, but when I did it worked like a charm.

@softinio
Copy link
Author

@rj76 any pointers or help appreciated on how you set it up appreciated. Thanks!

@rj76
Copy link

rj76 commented Dec 24, 2018

I've created this gist, that should help you get setup:

https://gist.github.com/rj76/c33ee201428be26b9c78a78473b5b169

One thing to keep in mind, is that to use fixtures you must be sure to create them in the right tenant context. If you only use one tenant in your tests, it's easy to reuse them, but in my case I ended up not having many shared fixtures across app tests.

If you have any questions, let me know ;)

@softinio
Copy link
Author

softinio commented Jan 2, 2019

@rj76 thanks so much you are awesome will take a look. Happy New Year!

@JohnnyFang
Copy link

I've created this gist, that should help you get setup:

https://gist.github.com/rj76/c33ee201428be26b9c78a78473b5b169

One thing to keep in mind, is that to use fixtures you must be sure to create them in the right tenant context. If you only use one tenant in your tests, it's easy to reuse them, but in my case I ended up not having many shared fixtures across app tests.

If you have any questions, let me know ;)

Richard, I've been struggling while trying to run pytest on my test_views.py. I was hoping you could point me in the right direction (examples, articles, etc) thanks in advance!

@rj76
Copy link

rj76 commented Jan 29, 2019

Hi, I couldn't find much information on the interwebs, so I figured it out myself. Can you point out the problem you're facing?

@GeeWee
Copy link

GeeWee commented Feb 4, 2019

I spent a little time tinkering with the excellent example (thanks @rj76 ! )
The big difference is that it's a little more annotated (there were a few intricacies I didn't get in the original example) , and that it by default runs all tests in the context of a tenant, so that regular tests "just work" - I'm migrating a couple of hundred tests to this
library, and it was important for me to not have to change all my tests:

https://gist.github.com/GeeWee/54b6fd7ad87bcc876781ae02d1e0993d

@dwcaraway
Copy link

+1 I'm also looking to get this working with pytest. The TenantTestCase is great, but precludes pytest's fixtures and parameterization features.

@softinio
Copy link
Author

softinio commented Sep 5, 2019

Did anyone find a solution to this?

@GeeWee
Copy link

GeeWee commented Sep 6, 2019

I've just updated my gist reference above to the version I am currently using day-to-day, and I'm happy to report it works without a hitch.

@akifd
Copy link

akifd commented Sep 26, 2019

I created another gist how I have done this using fixtures: https://gist.github.com/user37596/97e612f5c67adfad79105613af6204a6

Notice though, it is made for django-tenants package, not django-tenant-schemas, but it's not very hard to adapt the code to django-tenant-schemas.

With those fixtures in your conftest.py, you can create tests like:

# This test will happen with connection.tenant being non-public tenant.
def test_login(test_client):
    user = UserFactory()
    response = test_client.post("/login/", {"username": user.username, "password": user.password})
    assert response.status_code == 302

The test_client fixture makes sure non-public test tenant exists and is activated. If you want to test in the public tenant domain, use:

# This test will happen with connection.tenant being public tenant.
def test_login(public_test_client):
    user = UserFactory()
    response = public_test_client.post("/login/", {"username": user.username, "password": user.password})
    assert response.status_code == 302

@jeroenbrouwer
Copy link

I've fixed it by setting up a public and pytest tenant before all tests run in the django_db_setup hook for pytest and setting the connection to the pytest tenant. See https://gist.github.com/jeroenbrouwer/61b419e7232710e95c3e9454c7c78863

The downside to this implementation is that everything really does happen on a DB (I use a separate test DB for this) and that the flush command has to work (or you should do some other manual clean-up). To get the flush command to work see #567

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

7 participants