From 6a51792fbd0f92050a4f9a8ac4b717826cff556a Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Fri, 29 Mar 2019 15:17:24 -0700 Subject: [PATCH] Switched test runs to pytest --- django_inlinecss/tests/constants.py | 12 -- django_inlinecss/tests/test_templatetags.py | 17 +-- run_tests.py | 66 ---------- setup.cfg | 7 +- setup.py | 2 + test_settings.py | 139 ++++++++++++++++++++ 6 files changed, 156 insertions(+), 87 deletions(-) delete mode 100644 django_inlinecss/tests/constants.py delete mode 100755 run_tests.py create mode 100644 test_settings.py diff --git a/django_inlinecss/tests/constants.py b/django_inlinecss/tests/constants.py deleted file mode 100644 index bc04d5c..0000000 --- a/django_inlinecss/tests/constants.py +++ /dev/null @@ -1,12 +0,0 @@ -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -import os - - -TESTS_ROOT = os.path.dirname(os.path.abspath(__file__)) - -TESTS_TEMPLATE_DIR = os.path.join(TESTS_ROOT, 'templates') -TESTS_STATIC_DIR = os.path.join(TESTS_ROOT, 'static') diff --git a/django_inlinecss/tests/test_templatetags.py b/django_inlinecss/tests/test_templatetags.py index 0dbf9ef..2a05836 100644 --- a/django_inlinecss/tests/test_templatetags.py +++ b/django_inlinecss/tests/test_templatetags.py @@ -11,17 +11,14 @@ import os +from django.conf import settings from django.template.loader import get_template from django.test import TestCase from django.test.utils import override_settings from django.utils.safestring import mark_safe from mock import patch -from django_inlinecss.tests.constants import TESTS_STATIC_DIR - -@override_settings( - STATIC_ROOT=TESTS_STATIC_DIR) class InlinecssTests(TestCase): def setUp(self): super(InlinecssTests, self).setUp() @@ -153,13 +150,14 @@ def test_comments_are_ignored(self): r'This is the "bar" div.\s+\s+') -@override_settings( - STATIC_ROOT=TESTS_STATIC_DIR) class DebugModeStaticfilesTests(TestCase): @override_settings(DEBUG=True) @patch('django.contrib.staticfiles.finders.find') def test_debug_mode_uses_staticfiles_finder(self, find): - full_path = os.path.join(TESTS_STATIC_DIR, "foobar.css") + full_path = os.path.join( + settings.STATIC_ROOT, + 'foobar.css', + ) find.return_value = full_path template = get_template('single_staticfiles_css.html') template.render({}) @@ -167,7 +165,10 @@ def test_debug_mode_uses_staticfiles_finder(self, find): @patch('django.contrib.staticfiles.storage.staticfiles_storage.path') def test_non_debug_mode_uses_staticfiles_storage(self, path): - full_path = os.path.join(TESTS_STATIC_DIR, "foobar.css") + full_path = os.path.join( + settings.STATIC_ROOT, + 'foobar.css', + ) path.return_value = full_path template = get_template('single_staticfiles_css.html') template.render({}) diff --git a/run_tests.py b/run_tests.py deleted file mode 100755 index 5edc752..0000000 --- a/run_tests.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python -# Short way to bootstrap Django test runner: -# taken from django-extensions (which also has -# a comment suggesting it was taken originally from -# from http://www.travisswicegood.com/2010/01/17/ -# django-virtualenv-pip-and-fabric/ -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - -import os -import sys - -import django -from django.conf import settings -from django.core.management import call_command - - -PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, PROJECT_ROOT) - - -def main(): - settings.configure( - INSTALLED_APPS=[ - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django_inlinecss' - ], - DATABASES={ - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - } - }, - TEMPLATES=[{ - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [ - os.path.join( - PROJECT_ROOT, - "django_inlinecss", - "tests", - "templates", - ), - ], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }], - STATIC_URL='/static/', - DEBUG=True - ) - - django.setup() - call_command('test', 'django_inlinecss') - - -if __name__ == '__main__': - main() diff --git a/setup.cfg b/setup.cfg index 779a3bc..427ef4c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [aliases] -test=./run_tests.py +test=pytest [flake8] max-line-length=120 @@ -14,3 +14,8 @@ force_single_line=True default_section=THIRDPARTY lines_after_imports=2 use_parentheses=True + +[tool:pytest] +django_find_project = false +DJANGO_SETTINGS_MODULE = test_settings +python_files = test_*.py diff --git a/setup.py b/setup.py index 952c045..9de20a6 100644 --- a/setup.py +++ b/setup.py @@ -17,6 +17,8 @@ # What packages are required only for tests? TESTS = [ 'mock==2.0.0', + 'pytest==4.3.1', + 'pytest-django==3.4.8', ] # What packages are optional? diff --git a/test_settings.py b/test_settings.py new file mode 100644 index 0000000..08b5b85 --- /dev/null +++ b/test_settings.py @@ -0,0 +1,139 @@ +""" +Django settings for test_project project. + +Generated by 'django-admin startproject' using Django 1.11.20. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/ref/settings/ +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +import os + + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(__file__) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'NOT_SECRET' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'django_inlinecss', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'test_project.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + os.path.join( + BASE_DIR, + 'django_inlinecss', + 'tests', + 'templates', + ), + ], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'test_project.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/1.11/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.11/howto/static-files/ + +STATIC_URL = '/static/' +STATIC_ROOT = os.path.join( + BASE_DIR, + 'django_inlinecss', + 'tests', + 'static', +)