django-tenants / django-tenants

Django tenants using PostgreSQL Schemas
MIT License
1.49k stars 339 forks source link

How to use Pytest with Django-Tenants? #664

Open Gitouis opened 3 years ago

Gitouis commented 3 years ago

Hi,

So basically, I want to know if there is any way to use pytest with django-tenants.

By default only the Public schema is known by pytest, is there any way to extend this to other schemas used by django-tenants?

There's no documentation about this, besides the default django test runner.

I would really appreciate if I got any hint to where to start at!

Thank you so much.

rj76 commented 3 years ago

Hi,

I'm also using pytest for my project. I'm creating some tenants in the django_db_setup fixture which I then can use throughout the tests, like so https://gist.github.com/rj76/c1f254dfda2e0ec9bdcb069cb2f2fe67

lucasrcezimbra commented 4 months ago

I did it this way:


# conftest.py
from django_tenants.test.cases import TenantTestCase
from django_tenants.test.client import TenantClient

@pytest.fixture(scope="session")
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        TenantTestCase.setUpClass()

    yield

    with django_db_blocker.unblock():
        TenantTestCase.tearDownClass()

@pytest.fixture
def client(user):
    return TenantClient(TenantTestCase.tenant)