Parameterize testing using django settings

I am working on a django reusable package that I plan to use with multiple projects. I used pytest to build a test suite, I used a parameterization that helped pytest run a single test with several settings.

However, I would like to run all my tests using various combinations of settings

available_backends = [
    'django_profile.auth_backends.drf.RestFramework',
    'django_profile.auth_backends.kong.Kong',
]


def pytest_generate_tests(metafunc):
    # if 'stringinput' in metafunc.fixturenames:
    if 'auth_backend' in metafunc.fixturenames:
        metafunc.parametrize(
            'auth_backend',
            available_backends
        )


@pytest.fixture(params=['auth_backend', ])
def auth_backend(request, settings):
    settings.DJANGO_PROFILE_AUTH_BACKEND = request.auth_backend
    return settings

I experimented with the above approach, but it also means that I have to add auth_backendto each test case, I do not think this is ideal. Can anyone recommend me to run all my tests using different combinations of settings?

Hi

+4
source share
3 answers

please you tried:

  • conftest.py scope = "session"
  • _backends params = pytest_generate_tests

https://docs.pytest.org/en/latest/fixture.html#parametrizing-fixtures

, :

@pytest.fixture(scope='session', params=[
    'django_profile.auth_backends.drf.RestFramework',
    'django_profile.auth_backends.kong.Kong',
])
def auth_backend(request, settings):
    settings.DJANGO_PROFILE_AUTH_BACKEND = request.param
    yield settings
+2

, .

# conftest.py
@pytest.yield_fixture(params=available_backends)
def settings(request, settings):
    if request.param not in available_backends:
        raise ValueError('auth_backend {} not implemented.'.format(request.param))
    wrapper = SettingsWrapper()
    settings.DJANGO_PROFILE_AUTH_BACKEND = request.param
    yield wrapper
    wrapper.finalize()

# setup.cfg
usefixtures = settings
0

.

Django:

:

from django.test import TestCase
class LoginTestCase(TestCase):

    def test_login(self):

        # First check for the default behavior
        response = self.client.get('/sekrit/')
        self.assertRedirects(response, '/accounts/login/?next=/sekrit/')

        # Then override the LOGIN_URL setting
        with self.settings(LOGIN_URL='/other/login/'):
            response = self.client.get('/sekrit/')
            self.assertRedirects(response, '/other/login/?next=/sekrit/')

from django.test import TestCase, override_settings

class LoginTestCase(TestCase):

    @override_settings(LOGIN_URL='/other/login/')
    def test_login(self):
        response = self.client.get('/sekrit/')
        self.assertRedirects(response, '/other/login/?next=/sekrit/')
0

Source: https://habr.com/ru/post/1689136/


All Articles