Skip to content

Commit

Permalink
fix(django): usage with class based tests (microsoft#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Oct 3, 2020
1 parent 20a6e7c commit 779b44a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions local-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Empty file added tests/assets/django/__init__.py
Empty file.
102 changes: 102 additions & 0 deletions tests/assets/django/settings.py
Original file line number Diff line number Diff line change
@@ -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/"
6 changes: 6 additions & 0 deletions tests/assets/django/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from django.urls import path

urlpatterns = [
path("admin/", admin.site.urls),
]
14 changes: 14 additions & 0 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 779b44a

Please sign in to comment.