Skip to content

Commit

Permalink
Remove setUpTestData from PlaywrightTestCase
Browse files Browse the repository at this point in the history
Remove `setUpTestData` from `PlaywrightTestCase` because it doesn't work call this from
`setUpClass`, because all data will be removed after the first test.

The `PlaywrightTestCase` used `StaticLiveServerTestCase` that inherits from `TransactionTestCase`
that will flush the database after each tests.

The `LiveServerTestCase` doc string says:
```
It inherits from TransactionTestCase instead of TestCase because the
threads don't share the same transactions (unless if using in-memory sqlite)
and each thread needs to commit all their transactions so that the other
thread can see the changes.
```
See also: https://code.djangoproject.com/ticket/23640

We can call `setUpTestData` from `setUp`. But the class method `setUpTestData` originally stands for
initializing test data for the entire test case, so don't do this. It's up to the child class how to
create initial test data.

Raise a warning if there is a `setUpTestData` to clarify that it's not usable.

Additional fixes:

Remove pytest warning by add the marker to `[tool.pytest.ini_options]`:
```
PytestUnknownMarkWarning: Unknown pytest.mark.playwright - is this a typo?  You can register custom
marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.playwright
```

Split github actions PyPi cache, see: python-poetry/poetry#4568
  • Loading branch information
Jens Diemer committed Sep 21, 2022
1 parent 6edc656 commit ed4ebc6
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Setup Github Cache
uses: actions/cache@v3
with:
key: shared-cache
key: dot-cache-${{ matrix.os }}-${{ matrix.python-version }}-${{ matrix.django }}
path: ~/.cache/

- name: 'Install package'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Utilities to manage text fixtures in JSON files.
Use Playwright in Pytest and Unittest + Fast Django user login

* [`PlaywrightConfig()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/test_utils/playwright.py#L137-L155) - PlaywrightTestCase config from environment (PWBROWSER, PWHEADLESS, PWSKIP, PWSLOWMO)
* [`PlaywrightTestCase()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/test_utils/playwright.py#L158-L217) - StaticLiveServerTestCase with helpers for writing frontend tests using Playwright.
* [`PlaywrightTestCase()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/test_utils/playwright.py#L158-L215) - StaticLiveServerTestCase with helpers for writing frontend tests using Playwright.
* [`PyTestPlaywrightBaseTestCase()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/test_utils/playwright.py#L97-L134) - DEPRECATED: Will be removed in the future! Use new "PlaywrightTestCase"
* [`UnittestRunnerMixin()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/test_utils/playwright.py#L68-L94) - DEPRECATED: Will be removed in the future! Use new "PlaywrightTestCase"
* [`fast_login()`](https://github.com/boxine/bx_django_utils/blob/master/bx_django_utils/test_utils/playwright.py#L36-L65) - DEPRECATED: Will be removed in the future! Use new "PlaywrightTestCase"
Expand Down
2 changes: 1 addition & 1 deletion bx_django_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '33'
__version__ = '34'
12 changes: 5 additions & 7 deletions bx_django_utils/test_utils/playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ def test_foobar(self):
# Possible names are: "chromium", "firefox" or "webkit"
BROWSER_NAME = None

@classmethod
def setUpTestData(cls):
"""Load initial data for the TestCase."""
pass

@classmethod
def setUpClass(cls):
"""
Expand All @@ -199,8 +194,11 @@ def setUpClass(cls):
slow_mo=cls.pw_config.slow_mo,
)

# StaticLiveServerTestCase doesn't call the setUpTestData, call it manually:
cls.setUpTestData()
if hasattr(cls, 'setUpTestData'):
warnings.warn(
f'Remove setUpTestData() from {cls.__name__}'
' because it will never be called!'
)

@classmethod
def tearDownClass(cls):
Expand Down
7 changes: 3 additions & 4 deletions bx_django_utils_tests/tests/test_playwright_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@


class TestPlaywrightTestCase(PlaywrightTestCase):
@classmethod
def setUpTestData(cls):
super().setUpTestData()

cls.superuser = User.objects.create_superuser(
def setUp(self):
super().setUp()
self.superuser = User.objects.create_superuser(
username='a-superuser',
password='ThisIsNotAPassword!',
)
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = 'bx_django_utils'
version = "33"
version = "34"
description = 'Various Django utility functions'
homepage = "https://github.com/boxine/bx_django_utils/"
authors = [
Expand Down Expand Up @@ -102,6 +102,9 @@ omit=[".*"]
minversion = "6.0"
DJANGO_SETTINGS_MODULE="bx_django_utils_tests.test_project.settings"
norecursedirs = ".* __pycache__ coverage* dist htmlcov"
markers = [
"playwright: marks Playwright tests",
]
# sometimes helpfull "addopts" arguments:
# -vv
# --verbose
Expand Down

0 comments on commit ed4ebc6

Please sign in to comment.