Created
March 1, 2017 18:55
-
-
Save radeksvarz/91972acd851b8e34d8fa5546d21f9e60 to your computer and use it in GitHub Desktop.
Pytest and django - check admin pages of own apps - reading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[pytest] | |
addopts = --reuse-db | |
DJANGO_SETTINGS_MODULE = settings.dev | |
python_files = tests.py test_*.py *_tests.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# For the adoption - change lines 27 and 36 for your custom user model | |
# + change line 41 listing your own apps you need to test | |
import pytest | |
from django.apps import apps | |
from django.core.urlresolvers import reverse, NoReverseMatch | |
# adopted from django-pytest for custom User model | |
@pytest.fixture() | |
def a_user(db, django_user_model, django_username_field): | |
"""A Django admin user. | |
This uses an existing user with username "admin", or creates a new one with | |
password "password". | |
Modified from django-pytest due to the custom User model. | |
""" | |
UserModel = django_user_model | |
username_field = django_username_field | |
try: | |
user = UserModel._default_manager.get(**{username_field: 'admin'}) | |
except UserModel.DoesNotExist: | |
extra_fields = {} | |
user = UserModel._default_manager.create_superuser( | |
email='admin', password='password', **extra_fields) | |
# 'admin', '[email protected]', 'password', ** extra_fields) | |
return user | |
# adopted from django-pytest for custom User model | |
@pytest.fixture() | |
def aclient(db, a_user): | |
from django.test.client import Client | |
client = Client() | |
auth_result = client.login(email="admin", password="password") | |
# client.login(username=admin_user.username, password='password') | |
return client | |
admin_urls = [] | |
apps_to_test = ["myapp1", "myapp2"] | |
for app_to_test in apps_to_test: | |
app_models = apps.all_models[app_to_test] | |
for model in app_models: | |
try: | |
# admin list URI | |
admin_urls.append(reverse("admin:%s_%s_changelist" % (app_to_test, model,))) | |
# admin edit form URI (add) | |
admin_urls.append(reverse("admin:%s_%s_add" % (app_to_test, model,))) | |
except NoReverseMatch: | |
continue | |
@pytest.fixture(params=admin_urls) | |
def admin_page(request): | |
url = request.param | |
yield url | |
def test_admin_pages(aclient, admin_page): | |
r = aclient.get(admin_page) | |
assert r.status_code == 200 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment