From 779b44a9edc4b20b08102e8baaf3bf5697626cd2 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Sat, 3 Oct 2020 18:09:13 +0200 Subject: [PATCH] fix(django): usage with class based tests (#12) --- .github/workflows/ci.yml | 4 + local-requirements.txt | 1 + pytest_playwright/pytest_playwright.py | 5 ++ tests/assets/django/__init__.py | 0 tests/assets/django/settings.py | 102 +++++++++++++++++++++++++ tests/assets/django/urls.py | 6 ++ tests/test_playwright.py | 14 ++++ 7 files changed, 132 insertions(+) create mode 100644 tests/assets/django/__init__.py create mode 100644 tests/assets/django/settings.py create mode 100644 tests/assets/django/urls.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 66d8361..f853710 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,10 @@ jobs: - name: Test if: ${{ matrix.os != 'ubuntu-latest' }} run: pytest --cov=pytest_playwright --cov-report xml + env: + DJANGO_SETTINGS_MODULE: tests.assets.django.settings - name: Test on Linux if: ${{ matrix.os == 'ubuntu-latest' }} run: xvfb-run pytest --cov=pytest_playwright --cov-report xml + env: + DJANGO_SETTINGS_MODULE: tests.assets.django.settings diff --git a/local-requirements.txt b/local-requirements.txt index 13753ab..4dd1928 100644 --- a/local-requirements.txt +++ b/local-requirements.txt @@ -8,3 +8,4 @@ twisted==20.3.0 wheel==0.34.2 flake8==3.8.3 pre-commit==2.6.0 +Django==3.1.2 diff --git a/pytest_playwright/pytest_playwright.py b/pytest_playwright/pytest_playwright.py index c35f8e0..2d3a248 100644 --- a/pytest_playwright/pytest_playwright.py +++ b/pytest_playwright/pytest_playwright.py @@ -159,6 +159,11 @@ def is_chromium(browser_name: str) -> bool: return browser_name == "chromium" +@pytest.fixture(scope="session") +def browser_name() -> None: + return None + + def pytest_addoption(parser: Any) -> None: group = parser.getgroup("playwright", "Playwright") group.addoption( diff --git a/tests/assets/django/__init__.py b/tests/assets/django/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/assets/django/settings.py b/tests/assets/django/settings.py new file mode 100644 index 0000000..b535a07 --- /dev/null +++ b/tests/assets/django/settings.py @@ -0,0 +1,102 @@ +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "123" + +# 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", +] + +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 = "proj1.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "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 = "proj1.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/2.2/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/2.2/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/2.2/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/2.2/howto/static-files/ + +STATIC_URL = "/static/" diff --git a/tests/assets/django/urls.py b/tests/assets/django/urls.py new file mode 100644 index 0000000..083932c --- /dev/null +++ b/tests/assets/django/urls.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path("admin/", admin.site.urls), +] diff --git a/tests/test_playwright.py b/tests/test_playwright.py index ea86048..1dd230f 100644 --- a/tests/test_playwright.py +++ b/tests/test_playwright.py @@ -177,3 +177,17 @@ def test_base_url(page): result = testdir.runpytest("--browser", "test123") result.assert_outcomes(errors=1) assert "'test123' is not allowed" in "\n".join(result.outlines) + + +def test_django(testdir: Any) -> None: + testdir.makepyfile( + """ + from django.test import TestCase + class Proj1Test(TestCase): + def test_one(self): + self.assertTrue(True) + + """ + ) + result = testdir.runpytest() + result.assert_outcomes(passed=1)