diff --git a/.coveragerc b/.coveragerc index 168f57853..4198d9593 100644 --- a/.coveragerc +++ b/.coveragerc @@ -6,3 +6,6 @@ branch = 1 [report] include = pytest_django/*,pytest_django_test/*,tests/* skip_covered = 1 +exclude_lines = + pragma: no cover + if TYPE_CHECKING: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..719e408f6 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,154 @@ +name: main + +on: + push: + branches: + - master + tags: + - "*" + pull_request: + branches: + - master + +env: + PYTEST_ADDOPTS: "--color=yes" + +# Set permissions at the job level. +permissions: {} + +jobs: + test: + runs-on: ubuntu-20.04 + continue-on-error: ${{ matrix.allow_failure }} + timeout-minutes: 15 + permissions: + contents: read + steps: + - uses: actions/checkout@v2 + with: + persist-credentials: false + + - uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python }} + + - name: Setup mysql + if: contains(matrix.name, 'mysql') + run: | + sudo systemctl start mysql.service + echo "TEST_DB_USER=root" >> $GITHUB_ENV + echo "TEST_DB_PASSWORD=root" >> $GITHUB_ENV + + - name: Setup postgresql + if: contains(matrix.name, 'postgres') + run: | + sudo systemctl start postgresql.service + sudo -u postgres createuser --createdb $USER + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install tox==3.24.4 + + - name: Run tox + run: tox -e ${{ matrix.name }} + + - name: Report coverage + if: contains(matrix.name, 'coverage') + uses: codecov/codecov-action@v2 + with: + fail_ci_if_error: true + files: ./coverage.xml + + strategy: + fail-fast: false + matrix: + include: + - name: linting,docs + python: 3.8 + allow_failure: false + + - name: py310-dj40-postgres-xdist-coverage + python: '3.10' + allow_failure: false + + - name: py310-dj32-postgres-xdist-coverage + python: '3.10' + allow_failure: false + + - name: py39-dj32-postgres-xdist-coverage + python: 3.9 + allow_failure: false + + - name: py39-dj40-mysql_innodb-coverage + python: 3.9 + allow_failure: false + + - name: py36-dj22-sqlite-xdist-coverage + python: 3.6 + allow_failure: false + + - name: py37-dj22-sqlite-xdist-coverage + python: 3.7 + allow_failure: false + + - name: py38-dj32-sqlite-xdist-coverage + python: 3.8 + allow_failure: false + + - name: py38-dj40-sqlite-xdist-coverage + python: 3.8 + allow_failure: false + + - name: py39-djmain-sqlite-coverage + python: 3.9 + allow_failure: true + + # Explicitly test (older) pytest 5.4. + - name: py35-dj22-postgres-pytest54-coverage + python: 3.5 + allow_failure: false + + - name: py35-dj22-sqlite_file-coverage + python: 3.5 + allow_failure: false + + - name: py36-dj32-mysql_myisam-coverage + python: 3.6 + allow_failure: false + + # pypy3: not included with coverage reports (much slower then). + - name: pypy3-dj22-postgres + python: pypy3 + allow_failure: false + + deploy: + if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') && github.repository == 'pytest-dev/pytest-django' + runs-on: ubuntu-20.04 + timeout-minutes: 15 + permissions: + contents: read + + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 + persist-credentials: false + + - uses: actions/setup-python@v2 + with: + python-version: "3.8" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install --upgrade build + + - name: Build package + run: python -m build + + - name: Publish package + uses: pypa/gh-action-pypi-publish@v1.4.1 + with: + user: __token__ + password: ${{ secrets.pypi_token }} diff --git a/.gitignore b/.gitignore index 90131acf9..35f1856e7 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ _build .Python .eggs *.egg +# autogenerated by setuptools-scm +/pytest_django/_version.py diff --git a/.readthedocs.yml b/.readthedocs.yml new file mode 100644 index 000000000..27f596bb7 --- /dev/null +++ b/.readthedocs.yml @@ -0,0 +1,16 @@ +version: 2 + +sphinx: + configuration: docs/conf.py + +python: + version: 3 + install: + - method: pip + path: . + extra_requirements: + - docs + +formats: + - epub + - pdf diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 777e2b7cc..000000000 --- a/.travis.yml +++ /dev/null @@ -1,109 +0,0 @@ -language: python -dist: xenial -cache: false - -jobs: - fast_finish: true - include: - - stage: baseline - python: 3.6 - env: - - TOXENV=py36-dj20-postgres-xdist-coverage - # Test in verbose mode. - - PYTEST_ADDOPTS=-vv - services: - - postgresql - - python: 3.6 - env: TOXENV=py36-dj111-mysql_innodb-coverage - services: - - mysql - - python: 2.7 - env: TOXENV=py27-dj111-sqlite-xdist-coverage - - python: 3.6 - env: TOXENV=checkqa,docs - - - stage: test - python: 3.7 - env: TOXENV=py37-dj21-sqlite-coverage - - python: 3.7 - env: TOXENV=py37-dj22-sqlite-xdist-coverage - - python: 3.8 - env: TOXENV=py38-dj30-sqlite-xdist-coverage - - # Explicitly test (older) pytest 4.1. - - python: 3.7 - env: TOXENV=py37-dj21-sqlite-pytest41-coverage - - - python: 3.6 - env: TOXENV=py36-djmaster-sqlite-coverage - - - python: 3.5 - env: TOXENV=py35-dj110-postgres-coverage - services: - - postgresql - - - python: 3.4 - env: TOXENV=py34-dj19-sqlite_file-coverage - - - python: 2.7 - env: TOXENV=py27-dj111-mysql_myisam-coverage - services: - - mysql - - python: 2.7 - env: TOXENV=py27-dj18-postgres-coverage - services: - - postgresql - - # pypy/pypy3: not included with coverage reports (much slower then). - - python: pypy - env: TOXENV=pypy-dj111-sqlite_file - - python: pypy3 - env: TOXENV=pypy3-dj110-sqlite - - - stage: test_release - python: 3.6 - env: TOXENV=py36-dj20-postgres - services: - - postgresql - - - stage: release - script: skip - install: skip - after_success: true - deploy: - provider: pypi - user: blueyed - password: - secure: "FY7qbX/N0XRcH8hVk00SsQWvNIkuxKvY7Br4ghRnHvleHG3YulJ7WbJnik+9eoBGeMfJeNyzBfVjpeo1ZIq9IZBiyTdNfG/sZFsC5LOoG/CPxPH3nD9JktI2HoBMnlSbGg/MMHjY+wXuOY647U/3qNedcnQmGztYt6QWi5DRxu8=" - on: - tags: true - distributions: "sdist bdist_wheel" - - # NOTE: does not show up in "allowed failures" section, but is allowed to - # fail (for the "test" stage). - allow_failures: - - env: TOXENV=py36-djmaster-sqlite-coverage - -stages: - - name: baseline - if: tag IS NOT present - - name: test - if: tag IS NOT present - - name: test_release - if: tag IS present - - name: release - if: tag IS present - -install: - - pip install tox==3.9.0 - -script: - - tox - -after_success: - - | - set -ex - if [[ "${TOXENV%-coverage}" != "$TOXENV" ]]; then - bash <(curl -s https://codecov.io/bash) -Z -X gcov -X xcode -X gcovout - fi - set +ex diff --git a/AUTHORS b/AUTHORS index fbeb394b6..3f9b7ea65 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,6 +1,11 @@ Ben Firshman created the original version of pytest-django. -This fork is currently maintained by Andreas Pelme . +This project is currently maintained by Ran Benita . + +Previous maintainers are: + +Andreas Pelme +Daniel Hahler These people have provided bug fixes, new features, improved the documentation or just made pytest-django more awesome: @@ -12,4 +17,5 @@ Floris Bruynooghe Rafal Stozek Donald Stufft Nicolas Delaby -Daniel Hahler +Hasan Ramezani +Michael Howitz diff --git a/Makefile b/Makefile index 7ba0e09fa..ba5e3f500 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,8 @@ VENV:=build/venv export DJANGO_SETTINGS_MODULE?=pytest_django_test.settings_sqlite_file -testenv: $(VENV)/bin/pytest - test: $(VENV)/bin/pytest - $(VENV)/bin/pip install -e . - $(VENV)/bin/py.test + $(VENV)/bin/pytest $(VENV)/bin/python $(VENV)/bin/pip: virtualenv $(VENV) @@ -22,7 +19,7 @@ docs: # See setup.cfg for configuration. isort: - find pytest_django tests -name '*.py' -exec isort {} + + isort pytest_django pytest_django_test tests clean: rm -rf bin include/ lib/ man/ pytest_django.egg-info/ build/ diff --git a/README.rst b/README.rst index 47255c882..20be4f961 100644 --- a/README.rst +++ b/README.rst @@ -6,9 +6,13 @@ :alt: Supported Python versions :target: https://pypi.python.org/pypi/pytest-django -.. image:: https://travis-ci.org/pytest-dev/pytest-django.svg?branch=master +.. image:: https://github.com/pytest-dev/pytest-django/workflows/main/badge.svg :alt: Build Status - :target: https://travis-ci.org/pytest-dev/pytest-django + :target: https://github.com/pytest-dev/pytest-django/actions + +.. image:: https://img.shields.io/pypi/djversions/pytest-django.svg + :alt: Supported Django versions + :target: https://pypi.org/project/pytest-django/ .. image:: https://img.shields.io/codecov/c/github/pytest-dev/pytest-django.svg?style=flat :alt: Coverage @@ -28,10 +32,12 @@ pytest-django allows you to test your Django project/applications with the `_ * Version compatibility: - * Django: 1.8-1.11, 2.0-2.2, - and latest master branch (compatible at the time of each release) - * Python: CPython 2.7, 3.4-3.7 or PyPy 2, 3 - * pytest: >=3.6 + * Django: 2.2, 3.2, 4.0 and latest main branch (compatible at the time of + each release) + * Python: CPython>=3.5 or PyPy 3 + * pytest: >=5.4 + + For compatibility with older versions, use the pytest-django 3.*.* series. * Licence: BSD * Project maintainers: Andreas Pelme, Floris Bruynooghe and Daniel Hahler @@ -53,11 +59,11 @@ Why would I use this instead of Django's `manage.py test` command? Running your test suite with pytest-django allows you to tap into the features that are already present in pytest. Here are some advantages: -* `Manage test dependencies with pytest fixtures. `_ +* `Manage test dependencies with pytest fixtures. `_ * Less boilerplate tests: no need to import unittest, create a subclass with methods. Write tests as regular functions. * Database re-use: no need to re-create the test database for every test run. * Run tests in multiple processes for increased speed (with the pytest-xdist plugin). -* Make use of other `pytest plugins `_. +* Make use of other `pytest plugins `_. * Works with both worlds: Existing unittest-style TestCase's still work without any modifications. See the `pytest documentation `_ for more information on pytest itself. diff --git a/codecov.yml b/codecov.yml index 39bbacc0e..f1cc86973 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,6 +1,6 @@ +# reference: https://docs.codecov.io/docs/codecovyml-reference coverage: status: - project: true patch: true - changes: true + project: false comment: false diff --git a/docs/changelog.rst b/docs/changelog.rst index 645aee784..d120e7cc6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,217 @@ Changelog ========= +v4.5.2 (2021-12-07) +------------------- + +Bugfixes +^^^^^^^^ + +* Fix regression in v4.5.0 - ``pytest.mark.django_db(reset_sequence=True)`` now + implies ``transaction=True`` again. + + +v4.5.1 (2021-12-02) +------------------- + +Bugfixes +^^^^^^^^ + +* Fix regression in v4.5.0 - database tests inside (non-unittest) classes were + not ordered correctly to run before non-database tests, same for transactional + tests before non-transactional tests. + + +v4.5.0 (2021-12-01) +------------------- + +Improvements +^^^^^^^^^^^^ + +* Add support for :ref:`rollback emulation/serialized rollback + `. The :func:`pytest.mark.django_db` marker + has a new ``serialized_rollback`` option, and a + :fixture:`django_db_serialized_rollback` fixture is added. + +* Official Python 3.10 support. + +* Official Django 4.0 support (tested against 4.0rc1 at the time of release). + +* Drop official Django 3.0 support. Django 2.2 is still supported, and 3.0 + will likely keep working until 2.2 is dropped, but it's not tested. + +* Added pyproject.toml file. + +* Skip Django's `setUpTestData` mechanism in pytest-django tests. It is not + used for those, and interferes with some planned features. Note that this + does not affect ``setUpTestData`` in unittest tests (test classes which + inherit from Django's `TestCase`). + +Bugfixes +^^^^^^^^ + +* Fix :fixture:`live_server` when using an in-memory SQLite database. + +* Fix typing of ``assertTemplateUsed`` and ``assertTemplateNotUsed``. + + +v4.4.0 (2021-06-06) +------------------- + +Improvements +^^^^^^^^^^^^ + +* Add a fixture :fixture:`django_capture_on_commit_callbacks` to capture + :func:`transaction.on_commit() ` callbacks + in tests. + + +v4.3.0 (2021-05-15) +------------------- + +Improvements +^^^^^^^^^^^^ + +* Add experimental :ref:`multiple databases ` (multi db) support. + +* Add type annotations. If you previously excluded ``pytest_django`` from + your type-checker, you can remove the exclusion. + +* Documentation improvements. + + +v4.2.0 (2021-04-10) +------------------- + +Improvements +^^^^^^^^^^^^ + +* Official Django 3.2 support. + +* Documentation improvements. + +Bugfixes +^^^^^^^^ + +* Disable atomic durability check on non-transactional tests (#910). + + +v4.1.0 (2020-10-22) +------------------- + +Improvements +^^^^^^^^^^^^ + +* Add the :fixture:`async_client` and :fixture:`async_rf` fixtures (#864). + +* Add :ref:`django_debug_mode ` to configure how ``DEBUG`` is set in tests (#228). + +* Documentation improvements. + +Bugfixes +^^^^^^^^ + +* Make :fixture:`admin_user` work for custom user models without an ``email`` field. + + +v4.0.0 (2020-10-16) +------------------- + +Compatibility +^^^^^^^^^^^^^ + +This release contains no breaking changes, except dropping compatibility +with some older/unsupported versions. + +* Drop support for Python versions before 3.5 (#868). + + Previously 2.7 and 3.4 were supported. Running ``pip install pytest-django`` + on Python 2.7 or 3.4 would continue to install the compatible 3.x series. + +* Drop support for Django versions before 2.2 (#868). + + Previously Django>=1.8 was supported. + +* Drop support for pytest versions before 5.4 (#868). + + Previously pytest>=3.6 was supported. + +Improvements +^^^^^^^^^^^^ + +* Officially support Python 3.9. + +* Add ``pytest_django.__version__`` (#880). + +* Minor documentation improvements (#882). + +Bugfixes +^^^^^^^^ + +* Make the ``admin_user`` and ``admin_client`` fixtures compatible with custom + user models which don't have a ``username`` field (#457). + +* Change the ``admin_user`` fixture to use ``get_by_natural_key()`` to get the + user instead of directly using ``USERNAME_FIELD``, in case it is overridden, + and to match Django (#879). + +Misc +^^^^ + +* Fix pytest-django's own tests failing due to some deprecation warnings + (#875). + + +v3.10.0 (2020-08-25) +-------------------- + +Improvements +^^^^^^^^^^^^ + +* Officially support Django 3.1 + +* Preliminary support for upcoming Django 3.2 + +* Support for pytest-xdist 2.0 + + +Misc +^^^^ + +* Fix running pytest-django's own tests against pytest 6.0 (#855) + + +v3.9.0 (2020-03-31) +------------------- + +Improvements +^^^^^^^^^^^^ + +* Improve test ordering with Django test classes (#830) + +* Remove import of pkg_resources for parsing pytest version (performance) (#826) + +Bugfixes +^^^^^^^^ + +* Work around unittest issue with pytest 5.4.{0,1} (#825) + +* Don't break --failed-first when re-ordering tests (#819, #820) + +* pytest_addoption: use `group.addoption` (#833) + +Misc +^^^^ + +* Remove Django version from --nomigrations heading (#822) + +* docs: changelog: prefix headers with v for permalink anchors + +* changelog: add custom/fixed anchor for last version + +* setup.py: add Changelog to project_urls + + v3.8.0 (2020-01-14) -------------------- @@ -606,7 +817,7 @@ bugs. The tests for pytest-django itself has been greatly improved, paving the way for easier additions of new and exciting features in the future! -* Semantic version numbers will now be used for releases, see http://semver.org/. +* Semantic version numbers will now be used for releases, see https://semver.org/. * Do not allow database access in tests by default. Introduce ``pytest.mark.django_db`` to enable database access. @@ -670,7 +881,7 @@ v1.1 ---- * The initial release of this fork from `Ben Firshman original project - `__ + `__ * Added documentation * Uploaded to PyPI for easy installation * Added the ``transaction_test_case`` decorator for tests that needs real transactions diff --git a/docs/conf.py b/docs/conf.py index c64d2d2a2..5ccc34110 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import os import sys import datetime @@ -42,9 +40,9 @@ intersphinx_mapping = { 'python': ('https://docs.python.org/3', None), - 'django': ('https://docs.djangoproject.com/en/dev/', - 'https://docs.djangoproject.com/en/dev/_objects/'), - 'pytest': ('https://docs.pytest.org/en/latest/', None), + 'django': ('https://docs.djangoproject.com/en/stable/', + 'https://docs.djangoproject.com/en/stable/_objects/'), + 'pytest': ('https://docs.pytest.org/en/stable/', None), } diff --git a/docs/configuring_django.rst b/docs/configuring_django.rst index 21f4debf9..35ee0a452 100644 --- a/docs/configuring_django.rst +++ b/docs/configuring_django.rst @@ -9,17 +9,17 @@ the tests. The environment variable ``DJANGO_SETTINGS_MODULE`` --------------------------------------------------- -Running the tests with DJANGO_SETTINGS_MODULE defined will find the +Running the tests with ``DJANGO_SETTINGS_MODULE`` defined will find the Django settings the same way Django does by default. Example:: - $ export DJANGO_SETTINGS_MODULE=test_settings + $ export DJANGO_SETTINGS_MODULE=test.settings $ pytest or:: - $ DJANGO_SETTINGS_MODULE=test_settings pytest + $ DJANGO_SETTINGS_MODULE=test.settings pytest Command line option ``--ds=SETTINGS`` @@ -27,7 +27,7 @@ Command line option ``--ds=SETTINGS`` Example:: - $ pytest --ds=test_settings + $ pytest --ds=test.settings ``pytest.ini`` settings @@ -36,7 +36,7 @@ Example:: Example contents of pytest.ini:: [pytest] - DJANGO_SETTINGS_MODULE = test_settings + DJANGO_SETTINGS_MODULE = test.settings Order of choosing settings -------------------------- @@ -76,7 +76,8 @@ INI File Contents:: Using ``django.conf.settings.configure()`` ------------------------------------------ -Django settings can be set up by calling ``django.conf.settings.configure()``. +In case there is no ``DJANGO_SETTINGS_MODULE``, the ``settings`` object can be +created by calling ``django.conf.settings.configure()``. This can be done from your project's ``conftest.py`` file:: @@ -85,6 +86,22 @@ This can be done from your project's ``conftest.py`` file:: def pytest_configure(): settings.configure(DATABASES=...) +Overriding individual settings +------------------------------ + +Settings can be overridden by using the :fixture:`settings` fixture:: + + @pytest.fixture(autouse=True) + def use_dummy_cache_backend(settings): + settings.CACHES = { + "default": { + "BACKEND": "django.core.cache.backends.dummy.DummyCache", + } + } + +Here `autouse=True` is used, meaning the fixture is automatically applied to all tests, +but it can also be requested individually per-test. + Changing your app before Django gets set up ------------------------------------------- diff --git a/docs/contributing.rst b/docs/contributing.rst index 775e052ed..a104ac7ce 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -15,8 +15,11 @@ Community The fastest way to get feedback on contributions/bugs is usually to open an issue in the `issue tracker`_. -Discussions also happen via IRC in #pylib on irc.freenode.org. You may also -be interested in following `@andreaspelme`_ on Twitter. +Discussions also happen via IRC in #pytest `on irc.libera.chat +`_ (join using an IRC client, `via webchat +`_, or `via Matrix +`_). +You may also be interested in following `@andreaspelme`_ on Twitter. ************* In a nutshell @@ -178,10 +181,10 @@ Your coverage report is now ready in the ``htmlcov`` directory. Continuous integration ---------------------- -`Travis`_ is used to automatically run all tests against all supported versions +`GitHub Actions`_ is used to automatically run all tests against all supported versions of Python, Django and different database backends. -The `pytest-django Travis`_ page shows the latest test run. Travis will +The `pytest-django Actions`_ page shows the latest test run. The CI will automatically pick up pull requests, test them and report the result directly in the pull request. @@ -227,16 +230,16 @@ double cookie points. Seriously. You rock. .. _fork: https://github.com/pytest-dev/pytest-django .. _issue tracker: https://github.com/pytest-dev/pytest-django/issues -.. _Sphinx: http://sphinx.pocoo.org/ -.. _PEP8: http://www.python.org/dev/peps/pep-0008/ -.. _GitHub : http://www.github.com -.. _GitHub help : http://help.github.com -.. _freenode : http://freenode.net/ +.. _Sphinx: https://www.sphinx-doc.org/ +.. _PEP8: https://www.python.org/dev/peps/pep-0008/ +.. _GitHub : https://www.github.com +.. _GitHub help : https://help.github.com +.. _freenode : https://freenode.net/ .. _@andreaspelme : https://twitter.com/andreaspelme -.. _pull request : http://help.github.com/send-pull-requests/ -.. _git : http://git-scm.com/ -.. _restructuredText: http://docutils.sourceforge.net/docs/ref/rst/introduction.html +.. _pull request : https://help.github.com/send-pull-requests/ +.. _git : https://git-scm.com/ +.. _restructuredText: https://docutils.sourceforge.io/docs/ref/rst/introduction.html .. _django CMS: https://www.django-cms.org/ -.. _Travis: https://travis-ci.org/ -.. _pytest-django Travis: https://travis-ci.org/pytest-dev/pytest-django -.. _`subprocess section of coverage documentation`: http://nedbatchelder.com/code/coverage/subprocess.html +.. _GitHub Actions: https://github.com/features/actions +.. _pytest-django Actions: https://github.com/pytest-dev/pytest-django/actions +.. _`subprocess section of coverage documentation`: https://coverage.readthedocs.io/en/latest/subprocess.html diff --git a/docs/database.rst b/docs/database.rst index 75eb2eda6..c79cb4f95 100644 --- a/docs/database.rst +++ b/docs/database.rst @@ -1,19 +1,17 @@ -Database creation/re-use -======================== +Database access +=============== ``pytest-django`` takes a conservative approach to enabling database access. By default your tests will fail if they try to access the database. Only if you explicitly request database access will this be allowed. This encourages you to keep database-needing tests to a -minimum which is a best practice since next-to-no business logic -should be requiring the database. Moreover it makes it very clear -what code uses the database and catches any mistakes. +minimum which makes it very clear what code uses the database. Enabling database access in tests --------------------------------- -You can use `pytest marks `_ to -tell ``pytest-django`` your test needs database access:: +You can use :ref:`pytest marks ` to tell ``pytest-django`` your +test needs database access:: import pytest @@ -24,10 +22,8 @@ tell ``pytest-django`` your test needs database access:: It is also possible to mark all tests in a class or module at once. This demonstrates all the ways of marking, even though they overlap. -Just one of these marks would have been sufficient. See the `pytest -documentation -`_ -for detail:: +Just one of these marks would have been sufficient. See the :ref:`pytest +documentation ` for detail:: import pytest @@ -42,23 +38,21 @@ for detail:: By default ``pytest-django`` will set up the Django databases the -first time a test needs them. Once setup the database is cached for -used for all subsequent tests and rolls back transactions to isolate +first time a test needs them. Once setup, the database is cached to be +used for all subsequent tests and rolls back transactions, to isolate tests from each other. This is the same way the standard Django -`TestCase -`_ -uses the database. However ``pytest-django`` also caters for -transaction test cases and allows you to keep the test databases -configured across different test runs. +:class:`~django.test.TestCase` uses the database. However +``pytest-django`` also caters for transaction test cases and allows +you to keep the test databases configured across different test runs. Testing transactions -------------------- -Django itself has the ``TransactionTestCase`` which allows you to test -transactions and will flush the database between tests to isolate -them. The downside of this is that these tests are much slower to -set up due to the required flushing of the database. +Django itself has the :class:`~django.test.TransactionTestCase` which +allows you to test transactions and will flush the database between +tests to isolate them. The downside of this is that these tests are +much slower to set up due to the required flushing of the database. ``pytest-django`` also supports this style of tests, which you can select using an argument to the ``django_db`` mark:: @@ -66,25 +60,32 @@ select using an argument to the ``django_db`` mark:: def test_spam(): pass # test relying on transactions +.. _`multi-db`: Tests requiring multiple databases ---------------------------------- +.. versionadded:: 4.3 + +.. caution:: + + This support is **experimental** and is subject to change without + deprecation. We are still figuring out the best way to expose this + functionality. If you are using this successfully or unsuccessfully, + `let us know `_! + +``pytest-django`` has experimental support for multi-database configurations. Currently ``pytest-django`` does not specifically support Django's -multi-database support. +multi-database support, using the ``databases`` argument to the +:py:func:`django_db ` mark:: -You can however use normal :class:`~django.test.TestCase` instances to use its -:ref:`django:topics-testing-advanced-multidb` support. -In particular, if your database is configured for replication, be sure to read -about :ref:`django:topics-testing-primaryreplica`. + @pytest.mark.django_db(databases=['default', 'other']) + def test_spam(): + assert MyModel.objects.using('other').count() == 0 -If you have any ideas about the best API to support multiple databases -directly in ``pytest-django`` please get in touch, we are interested -in eventually supporting this but unsure about simply following -Django's approach. +For details see :py:attr:`django.test.TransactionTestCase.databases` and +:py:attr:`django.test.TestCase.databases`. -See `https://github.com/pytest-dev/pytest-django/pull/431` for an idea / -discussion to approach this. ``--reuse-db`` - reuse the testing database between test runs -------------------------------------------------------------- @@ -125,13 +126,13 @@ A good way to use ``--reuse-db`` and ``--create-db`` can be: * When you alter your database schema, run ``pytest --create-db``, to force re-creation of the test database. -``--nomigrations`` - Disable Django migrations ----------------------------------------------- +``--no-migrations`` - Disable Django migrations +----------------------------------------------- -Using ``--nomigrations`` will disable Django migrations and create the database +Using ``--no-migrations`` (alias: ``--nomigrations``) will disable Django migrations and create the database by inspecting all models. It may be faster when there are several migrations to run in the database setup. You can use ``--migrations`` to force running -migrations in case ``--nomigrations`` is used, e.g. in ``setup.cfg``. +migrations in case ``--no-migrations`` is used, e.g. in ``setup.cfg``. .. _advanced-database-configuration: @@ -184,8 +185,9 @@ django_db_modify_db_settings .. fixture:: django_db_modify_db_settings -This fixture allows modifying `django.conf.settings.DATABASES` just before the -databases are configured. +This fixture allows modifying +`django.conf.settings.DATABASES `_ +just before the databases are configured. If you need to customize the location of your test database, this is the fixture you want to override. @@ -238,7 +240,7 @@ Returns whether or not to use migrations to create the test databases. The default implementation returns the value of the -``--migrations``/``--nomigrations`` command line options. +``--migrations``/``--no-migrations`` command line options. This fixture is by default requested from :fixture:`django_db_setup`. @@ -333,7 +335,7 @@ Put this into ``conftest.py``:: conn.close() - @pytest.yield_fixture(scope='session') + @pytest.fixture(scope='session') def django_db_setup(): from django.conf import settings @@ -376,17 +378,17 @@ Put this into ``conftest.py``:: Populate the database with initial test data """""""""""""""""""""""""""""""""""""""""""" -This example shows how you can populate the test database with test data. The -test data will be saved in the database, i.e. it will not just be part of a -transactions. This example uses Django's fixture loading mechanism, but it can -be replaced with any way of loading data into the database. +In some cases you want to populate the test database before you start the +tests. Because of different ways you may use the test database, there are +different ways to populate it. -Notice that :fixture:`django_db_setup` is in the argument list. This may look -odd at first, but it will make sure that the original pytest-django fixture -is used to create the test database. When ``call_command`` is invoked, the -test database is already prepared and configured. +Populate the test database if you don't use transactional or live_server +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Put this in ``conftest.py``:: +If you are using the :func:`pytest.mark.django_db` marker or :fixture:`db` +fixture, you probably don't want to explicitly handle transactions in your +tests. In this case, it is sufficient to populate your database only +once. You can put code like this in ``conftest.py``:: import pytest @@ -395,13 +397,49 @@ Put this in ``conftest.py``:: @pytest.fixture(scope='session') def django_db_setup(django_db_setup, django_db_blocker): with django_db_blocker.unblock(): - call_command('loaddata', 'your_data_fixture.json') + call_command('loaddata', 'my_fixture.json') + +This loads the Django fixture ``my_fixture.json`` once for the entire test +session. This data will be available to tests marked with the +:func:`pytest.mark.django_db` mark, or tests which use the :fixture:`db` +fixture. The test data will be saved in the database and will not be reset. +This example uses Django's fixture loading mechanism, but it can be replaced +with any way of loading data into the database. + +Notice :fixture:`django_db_setup` in the argument list. This triggers the +original pytest-django fixture to create the test database, so that when +``call_command`` is invoked, the test database is already prepared and +configured. + +Populate the test database if you use transactional or live_server +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In case you use transactional tests (you use the :func:`pytest.mark.django_db` +marker with ``transaction=True``, or the :fixture:`transactional_db` fixture), +you need to repopulate your database every time a test starts, because the +database is cleared between tests. + +The :fixture:`live_server` fixture uses :fixture:`transactional_db`, so you +also need to populate the test database this way when using it. + +You can put this code into ``conftest.py``. Note that while it it is similar to +the previous one, the scope is changed from ``session`` to ``function``:: + + import pytest + + from myapp.models import Widget + + @pytest.fixture(scope='function') + def django_db_setup(django_db_setup, django_db_blocker): + with django_db_blocker.unblock(): + Widget.objects.create(...) + Use the same database for all xdist processes """"""""""""""""""""""""""""""""""""""""""""" By default, each xdist process gets its own database to run tests on. This is -needed to have transactional tests that does not interfere with eachother. +needed to have transactional tests that do not interfere with each other. If you instead want your tests to use the same database, override the :fixture:`django_db_modify_db_settings` to not do anything. Put this in diff --git a/docs/faq.rst b/docs/faq.rst index 1c6be1be2..68150f037 100644 --- a/docs/faq.rst +++ b/docs/faq.rst @@ -16,9 +16,10 @@ for more information. How can I make sure that all my tests run with a specific locale? ----------------------------------------------------------------- -Create a `pytest fixture `_ that is -automatically run before each test case. To run all tests with the english -locale, put the following code in your project's `conftest.py`_ file: +Create a :ref:`pytest fixture ` that is +automatically run before each test case. To run all tests with the English +locale, put the following code in your project's +:ref:`conftest.py ` file: .. code-block:: python @@ -28,8 +29,6 @@ locale, put the following code in your project's `conftest.py`_ file: def set_default_language(): activate('en') -.. _conftest.py: http://docs.pytest.org/en/latest/plugins.html - .. _faq-tests-not-being-picked-up: My tests are not being found. Why? @@ -55,7 +54,7 @@ When debugging test collection problems, the ``--collectonly`` flag and ``-rs`` (report skipped tests) can be helpful. .. _related pytest docs: - http://docs.pytest.org/en/latest/example/pythoncollection.html#changing-naming-conventions + https://docs.pytest.org/en/stable/example/pythoncollection.html#changing-naming-conventions Does pytest-django work with the pytest-xdist plugin? ----------------------------------------------------- @@ -76,7 +75,7 @@ test runner like this: .. code-block:: python - class PytestTestRunner(object): + class PytestTestRunner: """Runs pytest to discover and run tests.""" def __init__(self, verbosity=1, failfast=False, keepdb=False, **kwargs): @@ -84,6 +83,13 @@ test runner like this: self.failfast = failfast self.keepdb = keepdb + @classmethod + def add_arguments(cls, parser): + parser.add_argument( + '--keepdb', action='store_true', + help='Preserves the test DB between runs.' + ) + def run_tests(self, test_labels): """Run pytest and return the exitcode. @@ -143,8 +149,11 @@ If you think you've found a bug or something that is wrong in the documentation, feel free to `open an issue on the GitHub project`_ for pytest-django. -Direct help can be found in the #pylib IRC channel on irc.freenode.org. +Direct help can be found in the #pytest IRC channel `on irc.libera.chat +`_ (using an IRC client, `via webchat +`_, or `via Matrix +`_). -.. _pytest tag: http://stackoverflow.com/search?q=pytest +.. _pytest tag: https://stackoverflow.com/search?q=pytest .. _open an issue on the GitHub project: https://github.com/pytest-dev/pytest-django/issues/ diff --git a/docs/helpers.rst b/docs/helpers.rst index 76b32492a..07061ea46 100644 --- a/docs/helpers.rst +++ b/docs/helpers.rst @@ -16,65 +16,91 @@ All of Django's :py:class:`~django:django.test.TestCase` Markers ------- -``pytest-django`` registers and uses markers. See the pytest documentation_ -on what marks are and for notes on using_ them. - -.. _documentation: https://pytest.org/en/latest/mark.html -.. _using: https://pytest.org/en/latest/example/markers.html#marking-whole-classes-or-modules +``pytest-django`` registers and uses markers. See the pytest +:ref:`documentation ` on what marks are and for notes on +:ref:`using ` them. Remember that you can apply +marks at the single test level, the class level, the module level, and +dynamically in a hook or fixture. ``pytest.mark.django_db`` - request database access ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. :py:function:: pytest.mark.django_db([transaction=False, reset_sequences=False]): - -This is used to mark a test function as requiring the database. It -will ensure the database is set up correctly for the test. Each test -will run in its own transaction which will be rolled back at the end -of the test. This behavior is the same as Django's standard -`django.test.TestCase`_ class. - -In order for a test to have access to the database it must either -be marked using the ``django_db`` mark or request one of the ``db``, -``transactional_db`` or ``django_db_reset_sequences`` fixtures. Otherwise the -test will fail when trying to access the database. - -:type transaction: bool -:param transaction: - The ``transaction`` argument will allow the test to use real transactions. - With ``transaction=False`` (the default when not specified), transaction - operations are noops during the test. This is the same behavior that - `django.test.TestCase`_ - uses. When ``transaction=True``, the behavior will be the same as - `django.test.TransactionTestCase`_ - - -:type reset_sequences: bool -:param reset_sequences: - The ``reset_sequences`` argument will ask to reset auto increment sequence - values (e.g. primary keys) before running the test. Defaults to - ``False``. Must be used together with ``transaction=True`` to have an - effect. Please be aware that not all databases support this feature. - For details see :py:attr:`django.test.TransactionTestCase.reset_sequences`. +.. py:function:: pytest.mark.django_db([transaction=False, reset_sequences=False, databases=None]) + + This is used to mark a test function as requiring the database. It + will ensure the database is set up correctly for the test. Each test + will run in its own transaction which will be rolled back at the end + of the test. This behavior is the same as Django's standard + :class:`~django.test.TestCase` class. + + In order for a test to have access to the database it must either be marked + using the :func:`~pytest.mark.django_db` mark or request one of the :fixture:`db`, + :fixture:`transactional_db` or :fixture:`django_db_reset_sequences` fixtures. + Otherwise the test will fail when trying to access the database. + + :type transaction: bool + :param transaction: + The ``transaction`` argument will allow the test to use real transactions. + With ``transaction=False`` (the default when not specified), transaction + operations are noops during the test. This is the same behavior that + :class:`django.test.TestCase` uses. When ``transaction=True``, the behavior + will be the same as :class:`django.test.TransactionTestCase`. + + + :type reset_sequences: bool + :param reset_sequences: + The ``reset_sequences`` argument will ask to reset auto increment sequence + values (e.g. primary keys) before running the test. Defaults to + ``False``. Must be used together with ``transaction=True`` to have an + effect. Please be aware that not all databases support this feature. + For details see :py:attr:`django.test.TransactionTestCase.reset_sequences`. + + + :type databases: Union[Iterable[str], str, None] + :param databases: + .. caution:: + + This argument is **experimental** and is subject to change without + deprecation. We are still figuring out the best way to expose this + functionality. If you are using this successfully or unsuccessfully, + `let us know `_! + + The ``databases`` argument defines which databases in a multi-database + configuration will be set up and may be used by the test. Defaults to + only the ``default`` database. The special value ``"__all__"`` may be use + to specify all configured databases. + For details see :py:attr:`django.test.TransactionTestCase.databases` and + :py:attr:`django.test.TestCase.databases`. + + :type serialized_rollback: bool + :param serialized_rollback: + The ``serialized_rollback`` argument enables :ref:`rollback emulation + `. After a transactional test (or any test + using a database backend which doesn't support transactions) runs, the + database is flushed, destroying data created in data migrations. Setting + ``serialized_rollback=True`` tells Django to serialize the database content + during setup, and restore it during teardown. + + Note that this will slow down that test suite by approximately 3x. .. note:: - If you want access to the Django database *inside a fixture* - this marker will not help even if the function requesting your - fixture has this marker applied. To access the database in a - fixture, the fixture itself will have to request the ``db``, - ``transactional_db`` or ``django_db_reset_sequences`` fixture. See below - for a description of them. + If you want access to the Django database inside a *fixture*, this marker may + or may not help even if the function requesting your fixture has this marker + applied, depending on pytest's fixture execution order. To access the database + in a fixture, it is recommended that the fixture explicitly request one of the + :fixture:`db`, :fixture:`transactional_db`, + :fixture:`django_db_reset_sequences` or + :fixture:`django_db_serialized_rollback` fixtures. See below for a description + of them. .. note:: Automatic usage with ``django.test.TestCase``. - Test classes that subclass `django.test.TestCase`_ will have access to + Test classes that subclass :class:`django.test.TestCase` will have access to the database always to make them compatible with existing Django tests. - Test classes that subclass Python's ``unittest.TestCase`` need to have the - marker applied in order to access the database. - -.. _django.test.TestCase: https://docs.djangoproject.com/en/dev/topics/testing/overview/#testcase -.. _django.test.TransactionTestCase: https://docs.djangoproject.com/en/dev/topics/testing/overview/#transactiontestcase + Test classes that subclass Python's :class:`unittest.TestCase` need to have + the marker applied in order to access the database. ``pytest.mark.urls`` - override the urlconf @@ -93,7 +119,7 @@ test will fail when trying to access the database. @pytest.mark.urls('myapp.test_urls') def test_something(client): - assert 'Success!' in client.get('/some_url_defined_in_test_urls/').content + assert b'Success!' in client.get('/some_url_defined_in_test_urls/').content ``pytest.mark.ignore_template_errors`` - ignore invalid template variables @@ -104,8 +130,7 @@ test will fail when trying to access the database. Ignore errors when using the ``--fail-on-template-vars`` option, i.e. do not cause tests to fail if your templates contain invalid variables. - This marker sets the ``string_if_invalid`` template option, or - the older ``settings.TEMPLATE_STRING_IF_INVALID=None`` (Django up to 1.10). + This marker sets the ``string_if_invalid`` template option. See :ref:`django:invalid-template-variables`. Example usage:: @@ -119,16 +144,15 @@ Fixtures -------- pytest-django provides some pytest fixtures to provide dependencies for tests. -More information on fixtures is available in the `pytest documentation -`_. +More information on fixtures is available in the :ref:`pytest documentation +`. +.. fixture:: rf ``rf`` - ``RequestFactory`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -An instance of a `django.test.RequestFactory`_ - -.. _django.test.RequestFactory: https://docs.djangoproject.com/en/dev/topics/testing/advanced/#django.test.RequestFactory +An instance of a :class:`django.test.RequestFactory`. Example """"""" @@ -137,17 +161,46 @@ Example from myapp.views import my_view - def test_details(rf): + def test_details(rf, admin): request = rf.get('/customer/details') + # Remember that when using RequestFactory, the request does not pass + # through middleware. If your view expects fields such as request.user + # to be set, you need to set them explicitly. + # The following line sets request.user to an admin user. + request.user = admin + response = my_view(request) + assert response.status_code == 200 + +.. fixture:: async_rf + +``async_rf`` - ``AsyncRequestFactory`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +An instance of a `django.test.AsyncRequestFactory`_. + +.. _django.test.AsyncRequestFactory: https://docs.djangoproject.com/en/stable/topics/testing/advanced/#asyncrequestfactory + +Example +""""""" + +This example uses `pytest-asyncio `_. + +:: + + from myapp.views import my_view + + @pytest.mark.asyncio + async def test_details(async_rf): + request = await async_rf.get('/customer/details') response = my_view(request) assert response.status_code == 200 +.. fixture:: client + ``client`` - ``django.test.Client`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -An instance of a `django.test.Client`_ - -.. _django.test.Client: https://docs.djangoproject.com/en/dev/topics/testing/tools/#the-test-client +An instance of a :class:`django.test.Client`. Example """"""" @@ -158,23 +211,50 @@ Example response = client.get('/') assert response.content == 'Foobar' -To use `client` as an authenticated standard user, call its `login()` method before accessing a URL: +To use `client` as an authenticated standard user, call its +:meth:`force_login() ` or +:meth:`login() ` method before accessing a URL: :: def test_with_authenticated_client(client, django_user_model): username = "user1" password = "bar" - django_user_model.objects.create_user(username=username, password=password) + user = django_user_model.objects.create_user(username=username, password=password) + # Use this: + client.force_login(user) + # Or this: client.login(username=username, password=password) response = client.get('/private') assert response.content == 'Protected Area' +.. fixture:: async_client + +``async_client`` - ``django.test.AsyncClient`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +An instance of a `django.test.AsyncClient`_. + +.. _django.test.AsyncClient: https://docs.djangoproject.com/en/stable/topics/testing/tools/#testing-asynchronous-code + +Example +""""""" + +This example uses `pytest-asyncio `_. + +:: + + @pytest.mark.asyncio + async def test_with_async_client(async_client): + response = await async_client.get('/') + assert response.content == 'Foobar' + +.. fixture:: admin_client ``admin_client`` - ``django.test.Client`` logged in as admin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -An instance of a `django.test.Client`_, logged in as an admin user. +An instance of a :class:`django.test.Client`, logged in as an admin user. Example """"""" @@ -185,8 +265,8 @@ Example response = admin_client.get('/admin/') assert response.status_code == 200 -Using the `admin_client` fixture will cause the test to automatically be marked for database use (no need to specify the -``django_db`` mark). +Using the `admin_client` fixture will cause the test to automatically be marked +for database use (no need to specify the :func:`~pytest.mark.django_db` mark). .. fixture:: admin_user @@ -196,15 +276,17 @@ Using the `admin_client` fixture will cause the test to automatically be marked An instance of a superuser, with username "admin" and password "password" (in case there is no "admin" user yet). -Using the `admin_user` fixture will cause the test to automatically be marked for database use (no need to specify the -``django_db`` mark). +Using the `admin_user` fixture will cause the test to automatically be marked +for database use (no need to specify the :func:`~pytest.mark.django_db` mark). +.. fixture:: django_user_model ``django_user_model`` ~~~~~~~~~~~~~~~~~~~~~ A shortcut to the User model configured for use by the current Django project (aka the model referenced by -`settings.AUTH_USER_MODEL`). Use this fixture to make pluggable apps testable regardless what User model is configured +`settings.AUTH_USER_MODEL `_). +Use this fixture to make pluggable apps testable regardless what User model is configured in the containing Django project. Example @@ -215,23 +297,30 @@ Example def test_new_user(django_user_model): django_user_model.objects.create(username="someone", password="something") +.. fixture:: django_username_field ``django_username_field`` ~~~~~~~~~~~~~~~~~~~~~~~~~ -This fixture extracts the field name used for the username on the user model, i.e. resolves to the current -``settings.USERNAME_FIELD``. Use this fixture to make pluggable apps testable regardless what the username field +This fixture extracts the field name used for the username on the user model, i.e. +resolves to the user model's :attr:`~django.contrib.auth.models.CustomUser.USERNAME_FIELD`. +Use this fixture to make pluggable apps testable regardless what the username field is configured to be in the containing Django project. +.. fixture:: db + ``db`` ~~~~~~~ -.. fixture:: db - This fixture will ensure the Django database is set up. Only required for fixtures that want to use the database themselves. A -test function should normally use the ``pytest.mark.django_db`` -mark to signal it needs the database. +test function should normally use the :func:`pytest.mark.django_db` +mark to signal it needs the database. This fixture does +not return a database connection object. When you need a Django +database connection or cursor, import it from Django using +``from django.db import connection``. + +.. fixture:: transactional_db ``transactional_db`` ~~~~~~~~~~~~~~~~~~~~ @@ -239,27 +328,48 @@ mark to signal it needs the database. This fixture can be used to request access to the database including transaction support. This is only required for fixtures which need database access themselves. A test function should normally use the -``pytest.mark.django_db`` mark with ``transaction=True``. +:func:`pytest.mark.django_db` mark with ``transaction=True`` to signal +it needs the database. + +.. fixture:: django_db_reset_sequences ``django_db_reset_sequences`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. fixture:: django_db_reset_sequences - This fixture provides the same transactional database access as -``transactional_db``, with additional support for reset of auto increment -sequences (if your database supports it). This is only required for -fixtures which need database access themselves. A test function should -normally use the ``pytest.mark.django_db`` mark with ``transaction=True`` and ``reset_sequences=True``. +:fixture:`transactional_db`, with additional support for reset of auto +increment sequences (if your database supports it). This is only required for +fixtures which need database access themselves. A test function should normally +use the :func:`pytest.mark.django_db` mark with ``transaction=True`` and +``reset_sequences=True``. + +.. fixture:: django_db_serialized_rollback + +``django_db_serialized_rollback`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This fixture triggers :ref:`rollback emulation `. +This is only required for fixtures which need to enforce this behavior. A test +function should normally use :func:`pytest.mark.django_db` with +``serialized_rollback=True`` (and most likely also ``transaction=True``) to +request this behavior. + +.. fixture:: live_server ``live_server`` ~~~~~~~~~~~~~~~ This fixture runs a live Django server in a background thread. The server's URL can be retrieved using the ``live_server.url`` attribute -or by requesting it's string value: ``unicode(live_server)``. You can +or by requesting it's string value: ``str(live_server)``. You can also directly concatenate a string to form a URL: ``live_server + -'/foo``. +'/foo'``. + +Since the live server and the tests run in different threads, they +cannot share a database transaction. For this reason, ``live_server`` +depends on the ``transactional_db`` fixture. If tests depend on data +created in data migrations, you should add the +``django_db_serialized_rollback`` fixture. .. note:: Combining database access fixtures. @@ -268,10 +378,12 @@ also directly concatenate a string to form a URL: ``live_server + * ``db`` * ``transactional_db`` - * ``django_db_reset_sequences`` - In addition, using ``live_server`` will also trigger transactional - database access, if not specified. + In addition, using ``live_server`` or ``django_db_reset_sequences`` will also + trigger transactional database access, and ``django_db_serialized_rollback`` + regular database access, if not specified. + +.. fixture:: settings ``settings`` ~~~~~~~~~~~~ @@ -306,8 +418,8 @@ This fixture allows to check for an expected number of DB queries. If the assertion failed, the executed queries can be shown by using the verbose command line option. -It wraps `django.test.utils.CaptureQueriesContext` and yields the wrapped -CaptureQueriesContext instance. +It wraps ``django.test.utils.CaptureQueriesContext`` and yields the wrapped +``CaptureQueriesContext`` instance. Example usage:: @@ -343,6 +455,52 @@ Example usage:: Item.objects.create('bar') +.. fixture:: django_capture_on_commit_callbacks + +``django_capture_on_commit_callbacks`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. py:function:: django_capture_on_commit_callbacks(*, using=DEFAULT_DB_ALIAS, execute=False) + + :param using: + The alias of the database connection to capture callbacks for. + :param execute: + If True, all the callbacks will be called as the context manager exits, if + no exception occurred. This emulates a commit after the wrapped block of + code. + +.. versionadded:: 4.4 + +Returns a context manager that captures +:func:`transaction.on_commit() ` callbacks for +the given database connection. It returns a list that contains, on exit of the +context, the captured callback functions. From this list you can make assertions +on the callbacks or call them to invoke their side effects, emulating a commit. + +Avoid this fixture in tests using ``transaction=True``; you are not likely to +get useful results. + +This fixture is based on Django's :meth:`django.test.TestCase.captureOnCommitCallbacks` +helper. + +Example usage:: + + def test_on_commit(client, mailoutbox, django_capture_on_commit_callbacks): + with django_capture_on_commit_callbacks(execute=True) as callbacks: + response = client.post( + '/contact/', + {'message': 'I like your site'}, + ) + + assert response.status_code == 200 + assert len(callbacks) == 1 + assert len(mailoutbox) == 1 + assert mailoutbox[0].subject == 'Contact Form' + assert mailoutbox[0].body == 'I like your site' + + +.. fixture:: mailoutbox + ``mailoutbox`` ~~~~~~~~~~~~~~ diff --git a/docs/index.rst b/docs/index.rst index 49ecc4d59..6e73860d2 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -23,7 +23,7 @@ Make sure ``DJANGO_SETTINGS_MODULE`` is defined (see # -- FILE: pytest.ini (or tox.ini) [pytest] - DJANGO_SETTINGS_MODULE = test_settings + DJANGO_SETTINGS_MODULE = test.settings # -- recommended but optional: python_files = tests.py test_*.py *_tests.py @@ -39,22 +39,21 @@ Why would I use this instead of Django's manage.py test command? Running the test suite with pytest offers some features that are not present in Django's standard test mechanism: * Less boilerplate: no need to import unittest, create a subclass with methods. Just write tests as regular functions. -* `Manage test dependencies with fixtures`_. +* :ref:`Manage test dependencies with fixtures `. * Run tests in multiple processes for increased speed. * There are a lot of other nice plugins available for pytest. * Easy switching: Existing unittest-style tests will still work without any modifications. See the `pytest documentation`_ for more information on pytest. -.. _Manage test dependencies with fixtures: http://docs.pytest.org/en/latest/fixture.html -.. _pytest documentation: http://docs.pytest.org/ +.. _pytest documentation: https://docs.pytest.org/ Bugs? Feature Suggestions? ========================== Report issues and feature requests at the `GitHub issue tracker`_. -.. _GitHub issue tracker: http://github.com/pytest-dev/pytest-django/issues +.. _GitHub issue tracker: https://github.com/pytest-dev/pytest-django/issues Table of Contents ================= diff --git a/docs/managing_python_path.rst b/docs/managing_python_path.rst index a5dcd36a4..94af75d0b 100644 --- a/docs/managing_python_path.rst +++ b/docs/managing_python_path.rst @@ -5,7 +5,7 @@ Managing the Python path pytest needs to be able to import the code in your project. Normally, when interacting with Django code, the interaction happens via ``manage.py``, which -will implicilty add that directory to the Python path. +will implicitly add that directory to the Python path. However, when Python is started via the ``pytest`` command, some extra care is needed to have the Python path setup properly. There are two ways to handle @@ -60,7 +60,7 @@ This ``setup.py`` file is not sufficient to distribute your package to PyPI or more general packaging, but it should help you get started. Please refer to the `Python Packaging User Guide `_ -for more information on packaging Python applications.` +for more information on packaging Python applications. To install the project afterwards:: @@ -75,9 +75,39 @@ add this directly to your project's requirements.txt file like this:: pytest-django -Using pytest-pythonpath -~~~~~~~~~~~~~~~~~~~~~~~ +Using pytest's ``pythonpath`` option +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -You can also use the `pytest-pythonpath -`_ plugin to explicitly add paths to -the Python path. +You can explicitly add paths to the Python search path using pytest's +:pytest-confval:`pythonpath` option. +This option is available since pytest 7; for older versions you can use the +`pytest-pythonpath `_ plugin. + +Example: project with src layout +```````````````````````````````` + +For a Django package using the ``src`` layout, with test settings located in a +``tests`` package at the top level:: + + myproj + ├── pytest.ini + ├── src + │ └── myproj + │ ├── __init__.py + │ └── main.py + └── tests + ├── testapp + | ├── __init__.py + | └── apps.py + ├── __init__.py + ├── settings.py + └── test_main.py + +You'll need to specify both the top level directory and ``src`` for things to work:: + + [pytest] + DJANGO_SETTINGS_MODULE = tests.settings + pythonpath = . src + +If you don't specify ``.``, the settings module won't be found and +you'll get an import error: ``ImportError: No module named 'tests'``. diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 354af702d..d68635d2e 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -13,22 +13,22 @@ Talks, articles and blog posts * Talk from DjangoCon Europe 2014: `pytest: helps you write better Django apps, by Andreas Pelme `_ - * Talk from EuroPython 2013: `Testing Django application with pytest, by Andreas Pelme `_ + * Talk from EuroPython 2013: `Testing Django application with pytest, by Andreas Pelme `_ - * Three part blog post tutorial (part 3 mentions Django integration): `pytest: no-boilerplate testing, by Daniel Greenfeld `_ + * Three part blog post tutorial (part 3 mentions Django integration): `pytest: no-boilerplate testing, by Daniel Greenfeld `_ * Blog post: `Django Projects to Django Apps: Converting the Unit Tests, by John Costa `_. -For general information and tutorials on pytest, see the `pytest tutorial page `_. +For general information and tutorials on pytest, see the `pytest tutorial page `_. Step 1: Installation -------------------- pytest-django can be obtained directly from `PyPI -`_, and can be installed with +`_, and can be installed with ``pip``: .. code-block:: bash @@ -42,7 +42,7 @@ after installation, there is nothing more to configure. Step 2: Point pytest to your Django settings -------------------------------------------- -You need to tell pytest which Django settings that should be used for test +You need to tell pytest which Django settings should be used for test runs. The easiest way to achieve this is to create a pytest configuration file with this information. diff --git a/docs/usage.rst b/docs/usage.rst index 4c357e0ea..edfead5e9 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -20,7 +20,7 @@ the command line:: pytest test_something.py a_directory See the `pytest documentation on Usage and invocations -`_ for more help on available parameters. +`_ for more help on available parameters. Additional command line options ------------------------------- @@ -29,6 +29,30 @@ Additional command line options ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fail tests that render templates which make use of invalid template variables. +You can switch it on in `pytest.ini`:: + + [pytest] + FAIL_INVALID_TEMPLATE_VARS = True + +Additional pytest.ini settings +------------------------------ + +``django_debug_mode`` - change how DEBUG is set +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default tests run with the +`DEBUG `_ +setting set to ``False``. This is to ensure that the observed output of your +code matches what will be seen in a production setting. + +If you want ``DEBUG`` to be set:: + + [pytest] + django_debug_mode = true + +You can also use ``django_debug_mode = keep`` to disable the overriding and use +whatever is already set in the Django settings. + Running tests in parallel with pytest-xdist ------------------------------------------- pytest-django supports running tests on multiple processes to speed up test @@ -51,6 +75,6 @@ is set to "foo", the test database with xdist will be "test_foo_gw0", "test_foo_gw1" etc. See the full documentation on `pytest-xdist -`_ for more information. Among other -features, pytest-xdist can distribute/coordinate test execution on remote -machines. +`_ for more +information. Among other features, pytest-xdist can distribute/coordinate test +execution on remote machines. diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..6f907ba16 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,11 @@ +[build-system] +requires = [ + "setuptools>=45.0", + # sync with setup.cfg until we discard non-pep-517/518 + "setuptools-scm[toml]>=5.0.0", + "wheel", +] +build-backend = "setuptools.build_meta" + +[tool.setuptools_scm] +write_to = "pytest_django/_version.py" diff --git a/pytest_django/__init__.py b/pytest_django/__init__.py index e69de29bb..09f9c779e 100644 --- a/pytest_django/__init__.py +++ b/pytest_django/__init__.py @@ -0,0 +1,10 @@ +try: + from ._version import version as __version__ +except ImportError: # pragma: no cover + # Broken installation, we don't even try. + __version__ = "unknown" + + +__all__ = ( + "__version__", +) diff --git a/pytest_django/asserts.py b/pytest_django/asserts.py index 35fe979ba..a589abd2d 100644 --- a/pytest_django/asserts.py +++ b/pytest_django/asserts.py @@ -2,15 +2,20 @@ Dynamically load all Django assertion cases and expose them for importing. """ from functools import wraps +from typing import Any, Callable, Optional, Sequence, Set, Union + from django.test import ( - TestCase, SimpleTestCase, - LiveServerTestCase, TransactionTestCase + LiveServerTestCase, SimpleTestCase, TestCase, TransactionTestCase, ) -test_case = TestCase('run') + +TYPE_CHECKING = False + + +test_case = TestCase("run") -def _wrapper(name): +def _wrapper(name: str): func = getattr(test_case, name) @wraps(func) @@ -21,14 +26,186 @@ def assertion_func(*args, **kwargs): __all__ = [] -assertions_names = set() +assertions_names = set() # type: Set[str] assertions_names.update( - set(attr for attr in vars(TestCase) if attr.startswith('assert')), - set(attr for attr in vars(SimpleTestCase) if attr.startswith('assert')), - set(attr for attr in vars(LiveServerTestCase) if attr.startswith('assert')), - set(attr for attr in vars(TransactionTestCase) if attr.startswith('assert')), + {attr for attr in vars(TestCase) if attr.startswith("assert")}, + {attr for attr in vars(SimpleTestCase) if attr.startswith("assert")}, + {attr for attr in vars(LiveServerTestCase) if attr.startswith("assert")}, + {attr for attr in vars(TransactionTestCase) if attr.startswith("assert")}, ) for assert_func in assertions_names: globals()[assert_func] = _wrapper(assert_func) __all__.append(assert_func) + + +if TYPE_CHECKING: + from django.http import HttpResponse + + def assertRedirects( + response: HttpResponse, + expected_url: str, + status_code: int = ..., + target_status_code: int = ..., + msg_prefix: str = ..., + fetch_redirect_response: bool = ..., + ) -> None: + ... + + def assertURLEqual( + url1: str, + url2: str, + msg_prefix: str = ..., + ) -> None: + ... + + def assertContains( + response: HttpResponse, + text: object, + count: Optional[int] = ..., + status_code: int = ..., + msg_prefix: str = ..., + html: bool = False, + ) -> None: + ... + + def assertNotContains( + response: HttpResponse, + text: object, + status_code: int = ..., + msg_prefix: str = ..., + html: bool = False, + ) -> None: + ... + + def assertFormError( + response: HttpResponse, + form: str, + field: Optional[str], + errors: Union[str, Sequence[str]], + msg_prefix: str = ..., + ) -> None: + ... + + def assertFormsetError( + response: HttpResponse, + formset: str, + form_index: Optional[int], + field: Optional[str], + errors: Union[str, Sequence[str]], + msg_prefix: str = ..., + ) -> None: + ... + + def assertTemplateUsed( + response: Optional[HttpResponse] = ..., + template_name: Optional[str] = ..., + msg_prefix: str = ..., + count: Optional[int] = ..., + ): + ... + + def assertTemplateNotUsed( + response: Optional[HttpResponse] = ..., + template_name: Optional[str] = ..., + msg_prefix: str = ..., + ): + ... + + def assertRaisesMessage( + expected_exception: BaseException, + expected_message: str, + *args, + **kwargs + ): + ... + + def assertWarnsMessage( + expected_warning: Warning, + expected_message: str, + *args, + **kwargs + ): + ... + + def assertFieldOutput( + fieldclass, + valid, + invalid, + field_args=..., + field_kwargs=..., + empty_value: str = ..., + ) -> None: + ... + + def assertHTMLEqual( + html1: str, + html2: str, + msg: Optional[str] = ..., + ) -> None: + ... + + def assertHTMLNotEqual( + html1: str, + html2: str, + msg: Optional[str] = ..., + ) -> None: + ... + + def assertInHTML( + needle: str, + haystack: str, + count: Optional[int] = ..., + msg_prefix: str = ..., + ) -> None: + ... + + def assertJSONEqual( + raw: str, + expected_data: Any, + msg: Optional[str] = ..., + ) -> None: + ... + + def assertJSONNotEqual( + raw: str, + expected_data: Any, + msg: Optional[str] = ..., + ) -> None: + ... + + def assertXMLEqual( + xml1: str, + xml2: str, + msg: Optional[str] = ..., + ) -> None: + ... + + def assertXMLNotEqual( + xml1: str, + xml2: str, + msg: Optional[str] = ..., + ) -> None: + ... + + def assertQuerysetEqual( + qs, + values, + transform=..., + ordered: bool = ..., + msg: Optional[str] = ..., + ) -> None: + ... + + def assertNumQueries( + num: int, + func=..., + *args, + using: str = ..., + **kwargs + ): + ... + + # Fallback in case Django adds new asserts. + def __getattr__(name: str) -> Callable[..., Any]: + ... diff --git a/pytest_django/compat.py b/pytest_django/compat.py deleted file mode 100644 index 9203a50e1..000000000 --- a/pytest_django/compat.py +++ /dev/null @@ -1,15 +0,0 @@ -# This file cannot be imported from until Django sets up -try: - # Django 1.11 - from django.test.utils import setup_databases, teardown_databases # noqa: F401 -except ImportError: - # In Django prior to 1.11, teardown_databases is only available as a method on DiscoverRunner - from django.test.runner import ( # noqa: F401 - setup_databases, - DiscoverRunner as _DiscoverRunner, - ) - - def teardown_databases(db_cfg, verbosity): - _DiscoverRunner(verbosity=verbosity, interactive=False).teardown_databases( - db_cfg - ) diff --git a/pytest_django/django_compat.py b/pytest_django/django_compat.py index 18a2413e5..615e47011 100644 --- a/pytest_django/django_compat.py +++ b/pytest_django/django_compat.py @@ -2,7 +2,7 @@ # this is the case before you call them. -def is_django_unittest(request_or_item): +def is_django_unittest(request_or_item) -> bool: """Returns True if the request_or_item is a Django test case, otherwise False""" from django.test import SimpleTestCase diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index 62c020f86..556d7c39e 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -1,39 +1,54 @@ """All pytest-django fixtures""" - -from __future__ import with_statement - import os -import warnings from contextlib import contextmanager from functools import partial +from typing import ( + Any, Callable, Generator, Iterable, List, Optional, Tuple, Union, +) import pytest from . import live_server_helper from .django_compat import is_django_unittest -from .lazy_django import skip_if_no_django +from .lazy_django import get_django_version, skip_if_no_django + + +TYPE_CHECKING = False +if TYPE_CHECKING: + from typing import Literal + + import django + + _DjangoDbDatabases = Optional[Union["Literal['__all__']", Iterable[str]]] + # transaction, reset_sequences, databases, serialized_rollback + _DjangoDb = Tuple[bool, bool, _DjangoDbDatabases, bool] + __all__ = [ "django_db_setup", "db", "transactional_db", "django_db_reset_sequences", + "django_db_serialized_rollback", "admin_user", "django_user_model", "django_username_field", "client", + "async_client", "admin_client", "rf", + "async_rf", "settings", "live_server", "_live_server_helper", "django_assert_num_queries", "django_assert_max_num_queries", + "django_capture_on_commit_callbacks", ] @pytest.fixture(scope="session") -def django_db_modify_db_settings_tox_suffix(): +def django_db_modify_db_settings_tox_suffix() -> None: skip_if_no_django() tox_environment = os.getenv("TOX_PARALLEL_ENV") @@ -43,10 +58,10 @@ def django_db_modify_db_settings_tox_suffix(): @pytest.fixture(scope="session") -def django_db_modify_db_settings_xdist_suffix(request): +def django_db_modify_db_settings_xdist_suffix(request) -> None: skip_if_no_django() - xdist_suffix = getattr(request.config, "slaveinput", {}).get("slaveid") + xdist_suffix = getattr(request.config, "workerinput", {}).get("workerid") if xdist_suffix: # Put a suffix like _gw0, _gw1 etc on xdist processes _set_suffix_to_test_databases(suffix=xdist_suffix) @@ -54,49 +69,51 @@ def django_db_modify_db_settings_xdist_suffix(request): @pytest.fixture(scope="session") def django_db_modify_db_settings_parallel_suffix( - django_db_modify_db_settings_tox_suffix, - django_db_modify_db_settings_xdist_suffix, -): + django_db_modify_db_settings_tox_suffix: None, + django_db_modify_db_settings_xdist_suffix: None, +) -> None: skip_if_no_django() @pytest.fixture(scope="session") -def django_db_modify_db_settings(django_db_modify_db_settings_parallel_suffix): +def django_db_modify_db_settings( + django_db_modify_db_settings_parallel_suffix: None, +) -> None: skip_if_no_django() @pytest.fixture(scope="session") -def django_db_use_migrations(request): +def django_db_use_migrations(request) -> bool: return not request.config.getvalue("nomigrations") @pytest.fixture(scope="session") -def django_db_keepdb(request): +def django_db_keepdb(request) -> bool: return request.config.getvalue("reuse_db") @pytest.fixture(scope="session") -def django_db_createdb(request): +def django_db_createdb(request) -> bool: return request.config.getvalue("create_db") @pytest.fixture(scope="session") def django_db_setup( request, - django_test_environment, + django_test_environment: None, django_db_blocker, - django_db_use_migrations, - django_db_keepdb, - django_db_createdb, - django_db_modify_db_settings, -): + django_db_use_migrations: bool, + django_db_keepdb: bool, + django_db_createdb: bool, + django_db_modify_db_settings: None, +) -> None: """Top level fixture to ensure test databases are available""" - from .compat import setup_databases, teardown_databases + from django.test.utils import setup_databases, teardown_databases setup_databases_args = {} if not django_db_use_migrations: - _disable_native_migrations() + _disable_migrations() if django_db_keepdb and not django_db_createdb: setup_databases_args["keepdb"] = True @@ -108,7 +125,7 @@ def django_db_setup( **setup_databases_args ) - def teardown_database(): + def teardown_database() -> None: with django_db_blocker.unblock(): try: teardown_databases(db_cfg, verbosity=request.config.option.verbose) @@ -123,65 +140,145 @@ def teardown_database(): request.addfinalizer(teardown_database) -def _django_db_fixture_helper( - request, django_db_blocker, databases, transactional=False, reset_sequences=False -): +@pytest.fixture() +def _django_db_helper( + request, + django_db_setup: None, + django_db_blocker, +) -> None: + from django import VERSION + if is_django_unittest(request): return - if not transactional and "live_server" in request.fixturenames: - # Do nothing, we get called with transactional=True, too. - return + marker = request.node.get_closest_marker("django_db") + if marker: + ( + transactional, + reset_sequences, + databases, + serialized_rollback, + ) = validate_django_db(marker) + else: + ( + transactional, + reset_sequences, + databases, + serialized_rollback, + ) = False, False, None, False + + transactional = transactional or reset_sequences or ( + "transactional_db" in request.fixturenames + or "live_server" in request.fixturenames + ) + reset_sequences = reset_sequences or ( + "django_db_reset_sequences" in request.fixturenames + ) + serialized_rollback = serialized_rollback or ( + "django_db_serialized_rollback" in request.fixturenames + ) django_db_blocker.unblock() request.addfinalizer(django_db_blocker.restore) + import django.db + import django.test + if transactional: - from django.test import TransactionTestCase as django_case + test_case_class = django.test.TransactionTestCase + else: + test_case_class = django.test.TestCase + + _reset_sequences = reset_sequences + _serialized_rollback = serialized_rollback + _databases = databases + + class PytestDjangoTestCase(test_case_class): # type: ignore[misc,valid-type] + reset_sequences = _reset_sequences + serialized_rollback = _serialized_rollback + if _databases is not None: + databases = _databases + + # For non-transactional tests, skip executing `django.test.TestCase`'s + # `setUpClass`/`tearDownClass`, only execute the super class ones. + # + # `TestCase`'s class setup manages the `setUpTestData`/class-level + # transaction functionality. We don't use it; instead we (will) offer + # our own alternatives. So it only adds overhead, and does some things + # which conflict with our (planned) functionality, particularly, it + # closes all database connections in `tearDownClass` which inhibits + # wrapping tests in higher-scoped transactions. + # + # It's possible a new version of Django will add some unrelated + # functionality to these methods, in which case skipping them completely + # would not be desirable. Let's cross that bridge when we get there... + if not transactional: + @classmethod + def setUpClass(cls) -> None: + super(django.test.TestCase, cls).setUpClass() + if (3, 2) <= VERSION < (4, 1): + django.db.transaction.Atomic._ensure_durability = False + + @classmethod + def tearDownClass(cls) -> None: + if (3, 2) <= VERSION < (4, 1): + django.db.transaction.Atomic._ensure_durability = True + super(django.test.TestCase, cls).tearDownClass() + + PytestDjangoTestCase.setUpClass() + if VERSION >= (4, 0): + request.addfinalizer(PytestDjangoTestCase.doClassCleanups) + request.addfinalizer(PytestDjangoTestCase.tearDownClass) + + test_case = PytestDjangoTestCase(methodName="__init__") + test_case._pre_setup() + request.addfinalizer(test_case._post_teardown) - if reset_sequences: - class ResetSequenceTestCase(django_case): - reset_sequences = True +def validate_django_db(marker) -> "_DjangoDb": + """Validate the django_db marker. - django_case = ResetSequenceTestCase - else: - from django.test import TestCase as django_case + It checks the signature and creates the ``transaction``, + ``reset_sequences``, ``databases`` and ``serialized_rollback`` attributes on + the marker which will have the correct values. - def restore_databases(cls, dbs): - cls.databases = dbs + Sequence reset and serialized_rollback are only allowed when combined with + transaction. + """ - old_databases = getattr(django_case, "databases", None) - setattr(django_case, "databases", databases) + def apifun( + transaction: bool = False, + reset_sequences: bool = False, + databases: "_DjangoDbDatabases" = None, + serialized_rollback: bool = False, + ) -> "_DjangoDb": + return transaction, reset_sequences, databases, serialized_rollback - test_case = django_case(methodName="__init__") - test_case._pre_setup() - test_case.setUpClass() - - request.addfinalizer( - partial(restore_databases, cls=django_case, dbs=old_databases) - ) - request.addfinalizer(test_case.tearDownClass) - request.addfinalizer(test_case._post_teardown) + return apifun(*marker.args, **marker.kwargs) -def _disable_native_migrations(): +def _disable_migrations() -> None: from django.conf import settings from django.core.management.commands import migrate - from .migrations import DisableMigrations + class DisableMigrations: + def __contains__(self, item: str) -> bool: + return True + + def __getitem__(self, item: str) -> None: + return None settings.MIGRATION_MODULES = DisableMigrations() class MigrateSilentCommand(migrate.Command): def handle(self, *args, **kwargs): kwargs["verbosity"] = 0 - return super(MigrateSilentCommand, self).handle(*args, **kwargs) + return super().handle(*args, **kwargs) migrate.Command = MigrateSilentCommand -def _set_suffix_to_test_databases(suffix): +def _set_suffix_to_test_databases(suffix: str) -> None: from django.conf import settings for db_settings in settings.DATABASES.values(): @@ -201,33 +298,24 @@ def _set_suffix_to_test_databases(suffix): # ############### User visible fixtures ################ @pytest.fixture(scope="function") -def db(request, django_db_aliases, django_db_setup, django_db_blocker): +def db(_django_db_helper: None) -> None: """Require a django test database. This database will be setup with the default fixtures and will have the transaction management disabled. At the end of the test the outer transaction that wraps the test itself will be rolled back to undo any changes to the database (in case the backend supports transactions). - This is more limited than the ``transactional_db`` resource but + This is more limited than the ``transactional_db`` fixture but faster. - If multiple database fixtures are requested, they take precedence - over each other in the following order (the last one wins): ``db``, - ``transactional_db``, ``django_db_reset_sequences``. + If both ``db`` and ``transactional_db`` are requested, + ``transactional_db`` takes precedence. """ - if "django_db_reset_sequences" in request.fixturenames: - request.getfixturevalue("django_db_reset_sequences") - if ( - "transactional_db" in request.fixturenames - or "live_server" in request.fixturenames - ): - request.getfixturevalue("transactional_db") - else: - _django_db_fixture_helper(request, django_db_blocker, django_db_aliases, transactional=False) + # The `_django_db_helper` fixture checks if `db` is requested. @pytest.fixture(scope="function") -def transactional_db(request, django_db_aliases, django_db_setup, django_db_blocker): +def transactional_db(_django_db_helper: None) -> None: """Require a django test database with transaction support. This will re-initialise the django database for each test and is @@ -236,35 +324,51 @@ def transactional_db(request, django_db_aliases, django_db_setup, django_db_bloc If you want to use the database with transactions you must request this resource. - If multiple database fixtures are requested, they take precedence - over each other in the following order (the last one wins): ``db``, - ``transactional_db``, ``django_db_reset_sequences``. + If both ``db`` and ``transactional_db`` are requested, + ``transactional_db`` takes precedence. """ - if "django_db_reset_sequences" in request.fixturenames: - request.getfixturevalue("django_db_reset_sequences") - _django_db_fixture_helper(request, django_db_blocker, django_db_aliases, transactional=True) + # The `_django_db_helper` fixture checks if `transactional_db` is requested. @pytest.fixture(scope="function") -def django_db_reset_sequences(request, django_db_aliases, django_db_setup, django_db_blocker): +def django_db_reset_sequences( + _django_db_helper: None, + transactional_db: None, +) -> None: """Require a transactional test database with sequence reset support. - This behaves like the ``transactional_db`` fixture, with the addition - of enforcing a reset of all auto increment sequences. If the enquiring + This requests the ``transactional_db`` fixture, and additionally + enforces a reset of all auto increment sequences. If the enquiring test relies on such values (e.g. ids as primary keys), you should request this resource to ensure they are consistent across tests. + """ + # The `_django_db_helper` fixture checks if `django_db_reset_sequences` + # is requested. + - If multiple database fixtures are requested, they take precedence - over each other in the following order (the last one wins): ``db``, - ``transactional_db``, ``django_db_reset_sequences``. +@pytest.fixture(scope="function") +def django_db_serialized_rollback( + _django_db_helper: None, + db: None, +) -> None: + """Require a test database with serialized rollbacks. + + This requests the ``db`` fixture, and additionally performs rollback + emulation - serializes the database contents during setup and restores + it during teardown. + + This fixture may be useful for transactional tests, so is usually combined + with ``transactional_db``, but can also be useful on databases which do not + support transactions. + + Note that this will slow down that test suite by approximately 3x. """ - _django_db_fixture_helper( - request, django_db_blocker, django_db_aliases, transactional=True, reset_sequences=True - ) + # The `_django_db_helper` fixture checks if `django_db_serialized_rollback` + # is requested. @pytest.fixture() -def client(): +def client() -> "django.test.client.Client": """A Django test client instance.""" skip_if_no_django() @@ -274,7 +378,17 @@ def client(): @pytest.fixture() -def django_user_model(db): +def async_client() -> "django.test.client.AsyncClient": + """A Django test async client instance.""" + skip_if_no_django() + + from django.test.client import AsyncClient + + return AsyncClient() + + +@pytest.fixture() +def django_user_model(db: None): """The class of Django's user model.""" from django.contrib.auth import get_user_model @@ -282,13 +396,17 @@ def django_user_model(db): @pytest.fixture() -def django_username_field(django_user_model): +def django_username_field(django_user_model) -> str: """The fieldname for the username used with Django's user model.""" return django_user_model.USERNAME_FIELD @pytest.fixture() -def admin_user(db, django_user_model, django_username_field): +def admin_user( + db: None, + django_user_model, + django_username_field: str, +): """A Django admin user. This uses an existing user with username "admin", or creates a new one with @@ -299,29 +417,36 @@ def admin_user(db, django_user_model, django_username_field): username = "admin@example.com" if username_field == "email" else "admin" try: - user = UserModel._default_manager.get(**{username_field: username}) + # The default behavior of `get_by_natural_key()` is to look up by `username_field`. + # However the user model is free to override it with any sort of custom behavior. + # The Django authentication backend already assumes the lookup is by username, + # so we can assume so as well. + user = UserModel._default_manager.get_by_natural_key(username) except UserModel.DoesNotExist: - extra_fields = {} - if username_field not in ("username", "email"): - extra_fields[username_field] = "admin" - user = UserModel._default_manager.create_superuser( - username, "admin@example.com", "password", **extra_fields - ) + user_data = {} + if "email" in UserModel.REQUIRED_FIELDS: + user_data["email"] = "admin@example.com" + user_data["password"] = "password" + user_data[username_field] = username + user = UserModel._default_manager.create_superuser(**user_data) return user @pytest.fixture() -def admin_client(db, admin_user): +def admin_client( + db: None, + admin_user, +) -> "django.test.client.Client": """A Django test client logged in as an admin user.""" from django.test.client import Client client = Client() - client.login(username=admin_user.username, password="password") + client.force_login(admin_user) return client @pytest.fixture() -def rf(): +def rf() -> "django.test.client.RequestFactory": """RequestFactory instance""" skip_if_no_django() @@ -330,10 +455,20 @@ def rf(): return RequestFactory() -class SettingsWrapper(object): - _to_restore = [] +@pytest.fixture() +def async_rf() -> "django.test.client.AsyncRequestFactory": + """AsyncRequestFactory instance""" + skip_if_no_django() + + from django.test.client import AsyncRequestFactory + + return AsyncRequestFactory() + + +class SettingsWrapper: + _to_restore = [] # type: List[Any] - def __delattr__(self, attr): + def __delattr__(self, attr: str) -> None: from django.test import override_settings override = override_settings() @@ -344,26 +479,26 @@ def __delattr__(self, attr): self._to_restore.append(override) - def __setattr__(self, attr, value): + def __setattr__(self, attr: str, value) -> None: from django.test import override_settings override = override_settings(**{attr: value}) override.enable() self._to_restore.append(override) - def __getattr__(self, item): + def __getattr__(self, attr: str): from django.conf import settings - return getattr(settings, item) + return getattr(settings, attr) - def finalize(self): + def finalize(self) -> None: for override in reversed(self._to_restore): override.disable() del self._to_restore[:] -@pytest.yield_fixture() +@pytest.fixture() def settings(): """A Django settings object which restores changes after the testrun""" skip_if_no_django() @@ -380,8 +515,8 @@ def live_server(request): The address the server is started from is taken from the --liveserver command line option or if this is not provided from the DJANGO_LIVE_TEST_SERVER_ADDRESS environment variable. If - neither is provided ``localhost:8081,8100-8200`` is used. See the - Django documentation for its full syntax. + neither is provided ``localhost`` is used. See the Django + documentation for its full syntax. NOTE: If the live server needs database access to handle a request your test will have to request database access. Furthermore @@ -395,27 +530,9 @@ def live_server(request): """ skip_if_no_django() - import django - addr = request.config.getvalue("liveserver") or os.getenv( "DJANGO_LIVE_TEST_SERVER_ADDRESS" - ) - - if addr and ":" in addr: - if django.VERSION >= (1, 11): - ports = addr.split(":")[1] - if "-" in ports or "," in ports: - warnings.warn( - "Specifying multiple live server ports is not supported " - "in Django 1.11. This will be an error in a future " - "pytest-django release." - ) - - if not addr: - if django.VERSION < (1, 11): - addr = "localhost:8081,8100-8200" - else: - addr = "localhost" + ) or "localhost" server = live_server_helper.LiveServer(addr) request.addfinalizer(server.stop) @@ -423,7 +540,7 @@ def live_server(request): @pytest.fixture(autouse=True, scope="function") -def _live_server_helper(request): +def _live_server_helper(request) -> None: """Helper to make live_server work, internal to pytest-django. This helper will dynamically request the transactional_db fixture @@ -449,14 +566,22 @@ def _live_server_helper(request): @contextmanager -def _assert_num_queries(config, num, exact=True, connection=None, info=None): +def _assert_num_queries( + config, + num: int, + exact: bool = True, + connection=None, + info=None, +) -> Generator["django.test.utils.CaptureQueriesContext", None, None]: from django.test.utils import CaptureQueriesContext if connection is None: - from django.db import connection + from django.db import connection as conn + else: + conn = connection verbose = config.getoption("verbose") > 0 - with CaptureQueriesContext(connection) as context: + with CaptureQueriesContext(conn) as context: yield context num_performed = len(context) if exact: @@ -468,14 +593,14 @@ def _assert_num_queries(config, num, exact=True, connection=None, info=None): num, "" if exact else "or less ", "but {} done".format( - num_performed == 1 and "1 was" or "%d were" % (num_performed,) + num_performed == 1 and "1 was" or "{} were".format(num_performed) ), ) if info: msg += "\n{}".format(info) if verbose: sqls = (q["sql"] for q in context.captured_queries) - msg += "\n\nQueries:\n========\n\n%s" % "\n\n".join(sqls) + msg += "\n\nQueries:\n========\n\n" + "\n\n".join(sqls) else: msg += " (add -v option to show queries)" pytest.fail(msg) @@ -489,3 +614,38 @@ def django_assert_num_queries(pytestconfig): @pytest.fixture(scope="function") def django_assert_max_num_queries(pytestconfig): return partial(_assert_num_queries, pytestconfig, exact=False) + + +@contextmanager +def _capture_on_commit_callbacks( + *, + using: Optional[str] = None, + execute: bool = False +): + from django.db import DEFAULT_DB_ALIAS, connections + from django.test import TestCase + + if using is None: + using = DEFAULT_DB_ALIAS + + # Polyfill of Django code as of Django 3.2. + if get_django_version() < (3, 2): + callbacks = [] # type: List[Callable[[], Any]] + start_count = len(connections[using].run_on_commit) + try: + yield callbacks + finally: + run_on_commit = connections[using].run_on_commit[start_count:] + callbacks[:] = [func for sids, func in run_on_commit] + if execute: + for callback in callbacks: + callback() + + else: + with TestCase.captureOnCommitCallbacks(using=using, execute=execute) as callbacks: + yield callbacks + + +@pytest.fixture(scope="function") +def django_capture_on_commit_callbacks(): + return _capture_on_commit_callbacks diff --git a/pytest_django/lazy_django.py b/pytest_django/lazy_django.py index 9fd715bf9..6cf854914 100644 --- a/pytest_django/lazy_django.py +++ b/pytest_django/lazy_django.py @@ -1,20 +1,20 @@ """ Helpers to load Django lazily when Django settings can't be configured. """ - import os import sys +from typing import Any, Tuple import pytest -def skip_if_no_django(): +def skip_if_no_django() -> None: """Raises a skip exception when no Django settings are available""" if not django_settings_is_configured(): pytest.skip("no Django settings") -def django_settings_is_configured(): +def django_settings_is_configured() -> bool: """Return whether the Django settings module has been configured. This uses either the DJANGO_SETTINGS_MODULE environment variable, or the @@ -24,10 +24,13 @@ def django_settings_is_configured(): ret = bool(os.environ.get("DJANGO_SETTINGS_MODULE")) if not ret and "django.conf" in sys.modules: - return sys.modules["django.conf"].settings.configured + django_conf = sys.modules["django.conf"] # type: Any + return django_conf.settings.configured return ret -def get_django_version(): - return __import__("django").VERSION +def get_django_version() -> Tuple[int, int, int, str, int]: + import django + + return django.VERSION diff --git a/pytest_django/live_server_helper.py b/pytest_django/live_server_helper.py index 94ded3315..72ade43a8 100644 --- a/pytest_django/live_server_helper.py +++ b/pytest_django/live_server_helper.py @@ -1,33 +1,30 @@ -import six +from typing import Any, Dict -@six.python_2_unicode_compatible -class LiveServer(object): +class LiveServer: """The liveserver fixture This is the object that the ``live_server`` fixture returns. The ``live_server`` fixture handles creation and stopping. """ - def __init__(self, addr): - import django + def __init__(self, addr: str) -> None: from django.db import connections from django.test.testcases import LiveServerThread from django.test.utils import modify_settings + liveserver_kwargs = {} # type: Dict[str, Any] + connections_override = {} for conn in connections.all(): # If using in-memory sqlite databases, pass the connections to # the server thread. - if ( - conn.settings_dict["ENGINE"] == "django.db.backends.sqlite3" - and conn.settings_dict["NAME"] == ":memory:" - ): - # Explicitly enable thread-shareability for this connection - conn.allow_thread_sharing = True + if conn.vendor == "sqlite" and conn.is_in_memory_db(): + # Explicitly enable thread-shareability for this connection. + conn.inc_thread_sharing() connections_override[conn.alias] = conn - liveserver_kwargs = {"connections_override": connections_override} + liveserver_kwargs["connections_override"] = connections_override from django.conf import settings if "django.contrib.staticfiles" in settings.INSTALLED_APPS: @@ -39,71 +36,46 @@ def __init__(self, addr): liveserver_kwargs["static_handler"] = _StaticFilesHandler - if django.VERSION < (1, 11): - host, possible_ports = parse_addr(addr) - self.thread = LiveServerThread(host, possible_ports, **liveserver_kwargs) + try: + host, port = addr.split(":") + except ValueError: + host = addr else: - try: - host, port = addr.split(":") - except ValueError: - host = addr - else: - liveserver_kwargs["port"] = int(port) - self.thread = LiveServerThread(host, **liveserver_kwargs) + liveserver_kwargs["port"] = int(port) + self.thread = LiveServerThread(host, **liveserver_kwargs) self._live_server_modified_settings = modify_settings( ALLOWED_HOSTS={"append": host} ) + # `_live_server_modified_settings` is enabled and disabled by + # `_live_server_helper`. self.thread.daemon = True self.thread.start() self.thread.is_ready.wait() if self.thread.error: - raise self.thread.error + error = self.thread.error + self.stop() + raise error - def stop(self): + def stop(self) -> None: """Stop the server""" + # Terminate the live server's thread. self.thread.terminate() - self.thread.join() + # Restore shared connections' non-shareability. + for conn in self.thread.connections_override.values(): + conn.dec_thread_sharing() @property - def url(self): - return "http://%s:%s" % (self.thread.host, self.thread.port) + def url(self) -> str: + return "http://{}:{}".format(self.thread.host, self.thread.port) - def __str__(self): + def __str__(self) -> str: return self.url - def __add__(self, other): - return "%s%s" % (self, other) + def __add__(self, other) -> str: + return "{}{}".format(self, other) - def __repr__(self): + def __repr__(self) -> str: return "" % self.url - - -def parse_addr(specified_address): - """Parse the --liveserver argument into a host/IP address and port range""" - # This code is based on - # django.test.testcases.LiveServerTestCase.setUpClass - - # The specified ports may be of the form '8000-8010,8080,9200-9300' - # i.e. a comma-separated list of ports or ranges of ports, so we break - # it down into a detailed list of all possible ports. - possible_ports = [] - try: - host, port_ranges = specified_address.split(":") - for port_range in port_ranges.split(","): - # A port range can be of either form: '8000' or '8000-8010'. - extremes = list(map(int, port_range.split("-"))) - assert len(extremes) in (1, 2) - if len(extremes) == 1: - # Port range of the form '8000' - possible_ports.append(extremes[0]) - else: - # Port range of the form '8000-8010' - for port in range(extremes[0], extremes[1] + 1): - possible_ports.append(port) - except Exception: - raise Exception('Invalid address ("%s") for live server.' % specified_address) - - return host, possible_ports diff --git a/pytest_django/migrations.py b/pytest_django/migrations.py deleted file mode 100644 index 3c39cb9f6..000000000 --- a/pytest_django/migrations.py +++ /dev/null @@ -1,16 +0,0 @@ -# code snippet copied from https://gist.github.com/NotSqrt/5f3c76cd15e40ef62d09 -from pytest_django.lazy_django import get_django_version - - -class DisableMigrations(object): - def __init__(self): - self._django_version = get_django_version() - - def __contains__(self, item): - return True - - def __getitem__(self, item): - if self._django_version >= (1, 9): - return None - else: - return "notmigrations" diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index af4b2e657..6a2dbc126 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -6,64 +6,67 @@ import contextlib import inspect -from functools import reduce import os +import pathlib import sys -import types +from functools import reduce +from typing import Generator, List, Optional, Tuple, Union import pytest -from pkg_resources import parse_version from .django_compat import is_django_unittest # noqa -from .fixtures import django_assert_num_queries # noqa +from .fixtures import _django_db_helper # noqa +from .fixtures import _live_server_helper # noqa +from .fixtures import admin_client # noqa +from .fixtures import admin_user # noqa +from .fixtures import async_client # noqa +from .fixtures import async_rf # noqa +from .fixtures import client # noqa +from .fixtures import db # noqa from .fixtures import django_assert_max_num_queries # noqa -from .fixtures import django_db_setup # noqa -from .fixtures import django_db_use_migrations # noqa -from .fixtures import django_db_keepdb # noqa +from .fixtures import django_assert_num_queries # noqa +from .fixtures import django_capture_on_commit_callbacks # noqa from .fixtures import django_db_createdb # noqa +from .fixtures import django_db_keepdb # noqa from .fixtures import django_db_modify_db_settings # noqa from .fixtures import django_db_modify_db_settings_parallel_suffix # noqa from .fixtures import django_db_modify_db_settings_tox_suffix # noqa from .fixtures import django_db_modify_db_settings_xdist_suffix # noqa -from .fixtures import _live_server_helper # noqa -from .fixtures import admin_client # noqa -from .fixtures import admin_user # noqa -from .fixtures import client # noqa -from .fixtures import db # noqa +from .fixtures import django_db_reset_sequences # noqa +from .fixtures import django_db_serialized_rollback # noqa +from .fixtures import django_db_setup # noqa +from .fixtures import django_db_use_migrations # noqa from .fixtures import django_user_model # noqa from .fixtures import django_username_field # noqa from .fixtures import live_server # noqa -from .fixtures import django_db_reset_sequences # noqa from .fixtures import rf # noqa from .fixtures import settings # noqa from .fixtures import transactional_db # noqa - +from .fixtures import validate_django_db from .lazy_django import django_settings_is_configured, skip_if_no_django -try: - import pathlib -except ImportError: - import pathlib2 as pathlib + +TYPE_CHECKING = False +if TYPE_CHECKING: + from typing import ContextManager, NoReturn + + import django SETTINGS_MODULE_ENV = "DJANGO_SETTINGS_MODULE" CONFIGURATION_ENV = "DJANGO_CONFIGURATION" INVALID_TEMPLATE_VARS_ENV = "FAIL_INVALID_TEMPLATE_VARS" -PY2 = sys.version_info[0] == 2 - -# pytest 4.2 handles unittest setup/teardown itself via wrapping fixtures. -_handle_unittest_methods = parse_version(pytest.__version__) < parse_version("4.2") - _report_header = [] # ############### pytest hooks ################ -def pytest_addoption(parser): +@pytest.hookimpl() +def pytest_addoption(parser) -> None: group = parser.getgroup("django") - group._addoption( + group.addoption( "--reuse-db", action="store_true", dest="reuse_db", @@ -71,7 +74,7 @@ def pytest_addoption(parser): help="Re-use the testing database if it already exists, " "and do not remove it when the test finishes.", ) - group._addoption( + group.addoption( "--create-db", action="store_true", dest="create_db", @@ -79,7 +82,7 @@ def pytest_addoption(parser): help="Re-create the database, even if it exists. This " "option can be used to override --reuse-db.", ) - group._addoption( + group.addoption( "--ds", action="store", type=str, @@ -87,7 +90,7 @@ def pytest_addoption(parser): default=None, help="Set DJANGO_SETTINGS_MODULE.", ) - group._addoption( + group.addoption( "--dc", action="store", type=str, @@ -95,7 +98,7 @@ def pytest_addoption(parser): default=None, help="Set DJANGO_CONFIGURATION.", ) - group._addoption( + group.addoption( "--nomigrations", "--no-migrations", action="store_true", @@ -103,7 +106,7 @@ def pytest_addoption(parser): default=False, help="Disable Django migrations on test setup", ) - group._addoption( + group.addoption( "--migrations", action="store_false", dest="nomigrations", @@ -113,7 +116,7 @@ def pytest_addoption(parser): parser.addini( CONFIGURATION_ENV, "django-configurations class to use by pytest-django." ) - group._addoption( + group.addoption( "--liveserver", default=None, help="Address and port for the live_server fixture.", @@ -128,7 +131,13 @@ def pytest_addoption(parser): type="bool", default=True, ) - group._addoption( + parser.addini( + "django_debug_mode", + "How to set the Django DEBUG setting (default `False`). " + "Use `keep` to not override.", + default="False", + ) + group.addoption( "--fail-on-template-vars", action="store_true", dest="itv", @@ -165,7 +174,7 @@ def pytest_addoption(parser): @contextlib.contextmanager -def _handle_import_error(extra_message): +def _handle_import_error(extra_message: str) -> Generator[None, None, None]: try: yield except ImportError as e: @@ -174,29 +183,29 @@ def _handle_import_error(extra_message): raise ImportError(msg) -def _add_django_project_to_path(args): - def is_django_project(path): +def _add_django_project_to_path(args) -> str: + def is_django_project(path: pathlib.Path) -> bool: try: return path.is_dir() and (path / "manage.py").exists() except OSError: return False - def arg_to_path(arg): + def arg_to_path(arg: str) -> pathlib.Path: # Test classes or functions can be appended to paths separated by :: arg = arg.split("::", 1)[0] return pathlib.Path(arg) - def find_django_path(args): - args = map(str, args) - args = [arg_to_path(x) for x in args if not x.startswith("-")] + def find_django_path(args) -> Optional[pathlib.Path]: + str_args = (str(arg) for arg in args) + path_args = [arg_to_path(x) for x in str_args if not x.startswith("-")] cwd = pathlib.Path.cwd() - if not args: - args.append(cwd) - elif cwd not in args: - args.append(cwd) + if not path_args: + path_args.append(cwd) + elif cwd not in path_args: + path_args.append(cwd) - for arg in args: + for arg in path_args: if is_django_project(arg): return arg for parent in arg.parents: @@ -211,7 +220,7 @@ def find_django_path(args): return PROJECT_NOT_FOUND -def _setup_django(): +def _setup_django() -> None: if "django" not in sys.modules: return @@ -229,10 +238,14 @@ def _setup_django(): _blocking_manager.block() -def _get_boolean_value(x, name, default=None): +def _get_boolean_value( + x: Union[None, bool, str], + name: str, + default: Optional[bool] = None, +) -> bool: if x is None: - return default - if x in (True, False): + return bool(default) + if isinstance(x, bool): return x possible_values = {"true": True, "false": False, "1": True, "0": False} try: @@ -240,18 +253,30 @@ def _get_boolean_value(x, name, default=None): except KeyError: raise ValueError( "{} is not a valid value for {}. " - "It must be one of {}." % (x, name, ", ".join(possible_values.keys())) + "It must be one of {}.".format(x, name, ", ".join(possible_values.keys())) ) -def pytest_load_initial_conftests(early_config, parser, args): +@pytest.hookimpl() +def pytest_load_initial_conftests( + early_config, + parser, + args: List[str], +) -> None: # Register the marks early_config.addinivalue_line( "markers", - "django_db(transaction=False): Mark the test as using " - "the Django test database. The *transaction* argument marks will " - "allow you to use real transactions in the test like Django's " - "TransactionTestCase.", + "django_db(transaction=False, reset_sequences=False, databases=None, " + "serialized_rollback=False): " + "Mark the test as using the Django test database. " + "The *transaction* argument allows you to use real transactions " + "in the test like Django's TransactionTestCase. " + "The *reset_sequences* argument resets database sequences before " + "the test. " + "The *databases* argument sets which database aliases the test " + "uses (by default, only 'default'). Use '__all__' for all databases. " + "The *serialized_rollback* argument enables rollback emulation for " + "the test.", ) early_config.addinivalue_line( "markers", @@ -289,7 +314,10 @@ def pytest_load_initial_conftests(early_config, parser, args): ): os.environ[INVALID_TEMPLATE_VARS_ENV] = "true" - def _get_option_with_source(option, envname): + def _get_option_with_source( + option: Optional[str], + envname: str, + ) -> Union[Tuple[str, str], Tuple[None, None]]: if option: return option, "option" if envname in os.environ: @@ -303,11 +331,11 @@ def _get_option_with_source(option, envname): dc, dc_source = _get_option_with_source(options.dc, CONFIGURATION_ENV) if ds: - _report_header.append("settings: %s (from %s)" % (ds, ds_source)) + _report_header.append("settings: {} (from {})".format(ds, ds_source)) os.environ[SETTINGS_MODULE_ENV] = ds if dc: - _report_header.append("configuration: %s (from %s)" % (dc, dc_source)) + _report_header.append("configuration: {} (from {})".format(dc, dc_source)) os.environ[CONFIGURATION_ENV] = dc # Install the django-configurations importer @@ -325,123 +353,64 @@ def _get_option_with_source(option, envname): _setup_django() -def pytest_report_header(): +@pytest.hookimpl() +def pytest_report_header() -> Optional[List[str]]: if _report_header: return ["django: " + ", ".join(_report_header)] + return None -@pytest.mark.trylast -def pytest_configure(): +@pytest.hookimpl(trylast=True) +def pytest_configure() -> None: # Allow Django settings to be configured in a user pytest_configure call, # but make sure we call django.setup() _setup_django() -def _classmethod_is_defined_at_leaf(cls, method_name): - super_method = None - - for base_cls in cls.__mro__[1:]: # pragma: no branch - super_method = base_cls.__dict__.get(method_name) - if super_method is not None: - break - - assert super_method is not None, ( - "%s could not be found in base classes" % method_name - ) - - method = getattr(cls, method_name) - - try: - f = method.__func__ - except AttributeError: - pytest.fail("%s.%s should be a classmethod" % (cls, method_name)) - if PY2 and not ( - inspect.ismethod(method) - and inspect.isclass(method.__self__) - and issubclass(cls, method.__self__) - ): - pytest.fail("%s.%s should be a classmethod" % (cls, method_name)) - return f is not super_method.__func__ - - -_disabled_classmethods = {} - - -def _disable_class_methods(cls): - if cls in _disabled_classmethods: +@pytest.hookimpl(tryfirst=True) +def pytest_collection_modifyitems(items: List[pytest.Item]) -> None: + # If Django is not configured we don't need to bother + if not django_settings_is_configured(): return - _disabled_classmethods[cls] = ( - # Get the classmethod object (not the resulting bound method), - # otherwise inheritance will be broken when restoring. - cls.__dict__.get("setUpClass"), - _classmethod_is_defined_at_leaf(cls, "setUpClass"), - cls.__dict__.get("tearDownClass"), - _classmethod_is_defined_at_leaf(cls, "tearDownClass"), - ) - - cls.setUpClass = types.MethodType(lambda cls: None, cls) - cls.tearDownClass = types.MethodType(lambda cls: None, cls) - + from django.test import TestCase, TransactionTestCase -def _restore_class_methods(cls): - ( - setUpClass, - restore_setUpClass, - tearDownClass, - restore_tearDownClass, - ) = _disabled_classmethods.pop(cls) - - try: - del cls.setUpClass - except AttributeError: - raise - - try: - del cls.tearDownClass - except AttributeError: - pass - - if restore_setUpClass: - cls.setUpClass = setUpClass - - if restore_tearDownClass: - cls.tearDownClass = tearDownClass - - -def pytest_runtest_setup(item): - if _handle_unittest_methods: - if django_settings_is_configured() and is_django_unittest(item): - _disable_class_methods(item.cls) - - -@pytest.hookimpl(tryfirst=True) -def pytest_collection_modifyitems(items): - def get_order_number(test): - marker_db = test.get_closest_marker('django_db') - if marker_db: - transaction = validate_django_db(marker_db)[1] - if transaction is True: - return 1 + def get_order_number(test: pytest.Item) -> int: + test_cls = getattr(test, "cls", None) + if test_cls and issubclass(test_cls, TransactionTestCase): + # Note, TestCase is a subclass of TransactionTestCase. + uses_db = True + transactional = not issubclass(test_cls, TestCase) else: - transaction = None + marker_db = test.get_closest_marker("django_db") + if marker_db: + ( + transaction, + reset_sequences, + databases, + serialized_rollback, + ) = validate_django_db(marker_db) + uses_db = True + transactional = transaction or reset_sequences + else: + uses_db = False + transactional = False + fixtures = getattr(test, "fixturenames", []) + transactional = transactional or "transactional_db" in fixtures + uses_db = uses_db or "db" in fixtures - fixtures = getattr(test, 'fixturenames', []) - if "transactional_db" in fixtures: + if transactional: return 1 - - if transaction is False: + elif uses_db: return 0 - if "db" in fixtures: - return 0 - - return 2 + else: + return 2 - items[:] = sorted(items, key=get_order_number) + items.sort(key=get_order_number) @pytest.fixture(autouse=True, scope="session") -def django_test_environment(request): +def django_test_environment(request) -> None: """ Ensure that Django is loaded and has its testing environment setup. @@ -454,16 +423,22 @@ def django_test_environment(request): """ if django_settings_is_configured(): _setup_django() - from django.conf import settings as dj_settings - from django.test.utils import setup_test_environment, teardown_test_environment + from django.test.utils import ( + setup_test_environment, teardown_test_environment, + ) - dj_settings.DEBUG = False - setup_test_environment() + debug_ini = request.config.getini("django_debug_mode") + if debug_ini == "keep": + debug = None + else: + debug = _get_boolean_value(debug_ini, "django_debug_mode", False) + + setup_test_environment(debug=debug) request.addfinalizer(teardown_test_environment) @pytest.fixture(scope="session") -def django_db_blocker(): +def django_db_blocker() -> "Optional[_DatabaseBlocker]": """Wrapper around Django's database access. This object can be used to re-enable database access. This fixture is used @@ -483,68 +458,45 @@ def django_db_blocker(): @pytest.fixture(autouse=True) -def _django_db_marker(request): - """Implement the django_db marker, internal to pytest-django. - - This will dynamically request the ``db``, ``transactional_db`` or - ``django_db_reset_sequences`` fixtures as required by the django_db marker. - """ +def _django_db_marker(request) -> None: + """Implement the django_db marker, internal to pytest-django.""" marker = request.node.get_closest_marker("django_db") if marker: - databases, transaction, reset_sequences = validate_django_db(marker) - if reset_sequences: - request.getfixturevalue("django_db_reset_sequences") - elif transaction: - request.getfixturevalue("transactional_db") - else: - request.getfixturevalue("db") + request.getfixturevalue("_django_db_helper") @pytest.fixture(autouse=True, scope="class") -def _django_setup_unittest(request, django_db_blocker): +def _django_setup_unittest( + request, + django_db_blocker: "_DatabaseBlocker", +) -> Generator[None, None, None]: """Setup a django unittest, internal to pytest-django.""" if not django_settings_is_configured() or not is_django_unittest(request): yield return + # Fix/patch pytest. + # Before pytest 5.4: https://github.com/pytest-dev/pytest/issues/5991 + # After pytest 5.4: https://github.com/pytest-dev/pytest-django/issues/824 from _pytest.unittest import TestCaseFunction + original_runtest = TestCaseFunction.runtest - if "debug" in TestCaseFunction.runtest.__code__.co_names: - # Fix pytest (https://github.com/pytest-dev/pytest/issues/5991), only - # if "self._testcase.debug()" is being used (forward compatible). - from _pytest.monkeypatch import MonkeyPatch + def non_debugging_runtest(self) -> None: + self._testcase(result=self) - def non_debugging_runtest(self): - self._testcase(result=self) - - mp_debug = MonkeyPatch() - mp_debug.setattr("_pytest.unittest.TestCaseFunction.runtest", non_debugging_runtest) - else: - mp_debug = None - - request.getfixturevalue("django_db_setup") - - cls = request.node.cls - - with django_db_blocker.unblock(): - if _handle_unittest_methods: - _restore_class_methods(cls) - cls.setUpClass() - _disable_class_methods(cls) + try: + TestCaseFunction.runtest = non_debugging_runtest # type: ignore[assignment] - yield + request.getfixturevalue("django_db_setup") - _restore_class_methods(cls) - cls.tearDownClass() - else: + with django_db_blocker.unblock(): yield - - if mp_debug: - mp_debug.undo() + finally: + TestCaseFunction.runtest = original_runtest # type: ignore[assignment] @pytest.fixture(scope="function", autouse=True) -def _dj_autoclear_mailbox(): +def _dj_autoclear_mailbox() -> None: if not django_settings_is_configured(): return @@ -554,9 +506,12 @@ def _dj_autoclear_mailbox(): @pytest.fixture(scope="function") -def mailoutbox(django_mail_patch_dns, _dj_autoclear_mailbox): +def mailoutbox( + django_mail_patch_dns: None, + _dj_autoclear_mailbox: None, +) -> "Optional[List[django.core.mail.EmailMessage]]": if not django_settings_is_configured(): - return + return None from django.core import mail @@ -564,14 +519,17 @@ def mailoutbox(django_mail_patch_dns, _dj_autoclear_mailbox): @pytest.fixture(scope="function") -def django_mail_patch_dns(monkeypatch, django_mail_dnsname): +def django_mail_patch_dns( + monkeypatch, + django_mail_dnsname: str, +) -> None: from django.core import mail monkeypatch.setattr(mail.message, "DNS_NAME", django_mail_dnsname) @pytest.fixture(scope="function") -def django_mail_dnsname(): +def django_mail_dnsname() -> str: return "fake-tests.example.com" @@ -589,18 +547,13 @@ def django_db_aliases(request): @pytest.fixture(autouse=True, scope="function") -def _django_set_urlconf(request): +def _django_set_urlconf(request) -> None: """Apply the @pytest.mark.urls marker, internal to pytest-django.""" marker = request.node.get_closest_marker("urls") if marker: skip_if_no_django() import django.conf - - try: - from django.urls import clear_url_caches, set_urlconf - except ImportError: - # Removed in Django 2.0 - from django.core.urlresolvers import clear_url_caches, set_urlconf + from django.urls import clear_url_caches, set_urlconf urls = validate_urls(marker) original_urlconf = django.conf.settings.ROOT_URLCONF @@ -608,10 +561,10 @@ def _django_set_urlconf(request): clear_url_caches() set_urlconf(None) - def restore(): + def restore() -> None: django.conf.settings.ROOT_URLCONF = original_urlconf # Copy the pattern from - # https://github.com/django/django/blob/master/django/test/signals.py#L152 + # https://github.com/django/django/blob/main/django/test/signals.py#L152 clear_url_caches() set_urlconf(None) @@ -633,14 +586,13 @@ def _fail_for_invalid_template_variable(): ``pytest.mark.ignore_template_errors`` """ - class InvalidVarException(object): + class InvalidVarException: """Custom handler for invalid strings in templates.""" - def __init__(self): + def __init__(self) -> None: self.fail = True - def __contains__(self, key): - """There is a test for '%s' in TEMPLATE_STRING_IF_INVALID.""" + def __contains__(self, key: str) -> bool: return key == "%s" @staticmethod @@ -663,11 +615,11 @@ def _get_origin(): from django.template import Template # finding the ``render`` needle in the stack - frame = reduce( + frameinfo = reduce( lambda x, y: y[3] == "render" and "base.py" in y[1] and y or x, stack ) # assert 0, stack - frame = frame[0] + frame = frameinfo[0] # finding only the frame locals in all frame members f_locals = reduce( lambda x, y: y[0] == "f_locals" and y or x, inspect.getmembers(frame) @@ -677,11 +629,10 @@ def _get_origin(): if isinstance(template, Template): return template.name - def __mod__(self, var): - """Handle TEMPLATE_STRING_IF_INVALID % var.""" + def __mod__(self, var: str) -> str: origin = self._get_origin() if origin: - msg = "Undefined template variable '%s' in '%s'" % (var, origin) + msg = "Undefined template variable '{}' in '{}'".format(var, origin) else: msg = "Undefined template variable '%s'" % var if self.fail: @@ -696,15 +647,11 @@ def __mod__(self, var): from django.conf import settings as dj_settings if dj_settings.TEMPLATES: - dj_settings.TEMPLATES[0]["OPTIONS"][ - "string_if_invalid" - ] = InvalidVarException() - else: - dj_settings.TEMPLATE_STRING_IF_INVALID = InvalidVarException() + dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = InvalidVarException() @pytest.fixture(autouse=True) -def _template_string_if_invalid_marker(request): +def _template_string_if_invalid_marker(request) -> None: """Apply the @pytest.mark.ignore_template_errors marker, internal to pytest-django.""" marker = request.keywords.get("ignore_template_errors", None) @@ -714,12 +661,10 @@ def _template_string_if_invalid_marker(request): if dj_settings.TEMPLATES: dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"].fail = False - else: - dj_settings.TEMPLATE_STRING_IF_INVALID.fail = False @pytest.fixture(autouse=True, scope="function") -def _django_clear_site_cache(): +def _django_clear_site_cache() -> None: """Clears ``django.contrib.sites.models.SITE_CACHE`` to avoid unexpected behavior with cached site objects. """ @@ -736,18 +681,18 @@ def _django_clear_site_cache(): # ############### Helper Functions ################ -class _DatabaseBlockerContextManager(object): - def __init__(self, db_blocker): +class _DatabaseBlockerContextManager: + def __init__(self, db_blocker) -> None: self._db_blocker = db_blocker - def __enter__(self): + def __enter__(self) -> None: pass - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exc_value, traceback) -> None: self._db_blocker.restore() -class _DatabaseBlocker(object): +class _DatabaseBlocker: """Manager for django.db.backends.base.base.BaseDatabaseWrapper. This is the object returned by django_db_blocker. @@ -758,7 +703,7 @@ def __init__(self): self._real_ensure_connection = None @property - def _dj_db_wrapper(self): + def _dj_db_wrapper(self) -> "django.db.backends.base.base.BaseDatabaseWrapper": from django.db.backends.base.base import BaseDatabaseWrapper # The first time the _dj_db_wrapper is accessed, we will save a @@ -768,10 +713,10 @@ def _dj_db_wrapper(self): return BaseDatabaseWrapper - def _save_active_wrapper(self): - return self._history.append(self._dj_db_wrapper.ensure_connection) + def _save_active_wrapper(self) -> None: + self._history.append(self._dj_db_wrapper.ensure_connection) - def _blocking_wrapper(*args, **kwargs): + def _blocking_wrapper(*args, **kwargs) -> "NoReturn": __tracebackhide__ = True __tracebackhide__ # Silence pyflakes raise RuntimeError( @@ -780,49 +725,33 @@ def _blocking_wrapper(*args, **kwargs): '"db" or "transactional_db" fixtures to enable it.' ) - def unblock(self): + def unblock(self) -> "ContextManager[None]": """Enable access to the Django database.""" self._save_active_wrapper() self._dj_db_wrapper.ensure_connection = self._real_ensure_connection return _DatabaseBlockerContextManager(self) - def block(self): + def block(self) -> "ContextManager[None]": """Disable access to the Django database.""" self._save_active_wrapper() self._dj_db_wrapper.ensure_connection = self._blocking_wrapper return _DatabaseBlockerContextManager(self) - def restore(self): + def restore(self) -> None: self._dj_db_wrapper.ensure_connection = self._history.pop() _blocking_manager = _DatabaseBlocker() -def validate_django_db(marker): - """Validate the django_db marker. - - It checks the signature and creates the ``transaction`` and - ``reset_sequences`` attributes on the marker which will have the - correct values. - - A sequence reset is only allowed when combined with a transaction. - """ - - def apifun(databases=None, transaction=False, reset_sequences=False): - return databases, transaction, reset_sequences - - return apifun(*marker.args, **marker.kwargs) - - -def validate_urls(marker): +def validate_urls(marker) -> List[str]: """Validate the urls marker. It checks the signature and creates the `urls` attribute on the marker which will have the correct value. """ - def apifun(urls): + def apifun(urls: List[str]) -> List[str]: return urls return apifun(*marker.args, **marker.kwargs) diff --git a/pytest_django/py.typed b/pytest_django/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/pytest_django_test/app/migrations/0001_initial.py b/pytest_django_test/app/migrations/0001_initial.py index 7791cafcf..8953f3be6 100644 --- a/pytest_django_test/app/migrations/0001_initial.py +++ b/pytest_django_test/app/migrations/0001_initial.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.9a1 on 2016-06-22 04:33 -from __future__ import unicode_literals +from typing import List, Tuple from django.db import migrations, models @@ -9,7 +7,7 @@ class Migration(migrations.Migration): initial = True - dependencies = [] + dependencies = [] # type: List[Tuple[str, str]] operations = [ migrations.CreateModel( @@ -26,5 +24,20 @@ class Migration(migrations.Migration): ), ("name", models.CharField(max_length=100)), ], - ) + ), + migrations.CreateModel( + name="SecondItem", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=100)), + ], + ), ] diff --git a/pytest_django_test/app/models.py b/pytest_django_test/app/models.py index 381ce30aa..5186adc41 100644 --- a/pytest_django_test/app/models.py +++ b/pytest_django_test/app/models.py @@ -1,5 +1,11 @@ from django.db import models +# Routed to database "main". class Item(models.Model): - name = models.CharField(max_length=100) + name = models.CharField(max_length=100) # type: str + + +# Routed to database "second". +class SecondItem(models.Model): + name = models.CharField(max_length=100) # type: str diff --git a/pytest_django_test/app/views.py b/pytest_django_test/app/views.py index b400f408b..72b463569 100644 --- a/pytest_django_test/app/views.py +++ b/pytest_django_test/app/views.py @@ -1,14 +1,14 @@ -from django.http import HttpResponse +from django.http import HttpRequest, HttpResponse from django.template import Template from django.template.context import Context from .models import Item -def admin_required_view(request): +def admin_required_view(request: HttpRequest) -> HttpResponse: assert request.user.is_staff return HttpResponse(Template("You are an admin").render(Context())) -def item_count(request): +def item_count(request: HttpRequest) -> HttpResponse: return HttpResponse("Item count: %d" % Item.objects.count()) diff --git a/pytest_django_test/compat.py b/pytest_django_test/compat.py deleted file mode 100644 index 0c9ce91f1..000000000 --- a/pytest_django_test/compat.py +++ /dev/null @@ -1,4 +0,0 @@ -try: - from urllib2 import urlopen, HTTPError -except ImportError: - from urllib.request import urlopen, HTTPError # noqa: F401 diff --git a/pytest_django_test/db_helpers.py b/pytest_django_test/db_helpers.py index 29f095711..d984b1d12 100644 --- a/pytest_django_test/db_helpers.py +++ b/pytest_django_test/db_helpers.py @@ -1,9 +1,8 @@ import os -import subprocess import sqlite3 +import subprocess import pytest - from django.conf import settings from django.utils.encoding import force_str @@ -16,6 +15,8 @@ if _settings["ENGINE"] == "django.db.backends.sqlite3" and TEST_DB_NAME is None: TEST_DB_NAME = ":memory:" + SECOND_DB_NAME = ":memory:" + SECOND_TEST_DB_NAME = ":memory:" else: DB_NAME += "_inner" @@ -26,31 +27,59 @@ # An explicit test db name was given, is that as the base name TEST_DB_NAME = "{}_inner".format(TEST_DB_NAME) + SECOND_DB_NAME = DB_NAME + '_second' if DB_NAME is not None else None + SECOND_TEST_DB_NAME = TEST_DB_NAME + '_second' if DB_NAME is not None else None + def get_db_engine(): return _settings["ENGINE"].split(".")[-1] -class CmdResult(object): +class CmdResult: def __init__(self, status_code, std_out, std_err): self.status_code = status_code self.std_out = std_out self.std_err = std_err -def run_cmd(*args): - r = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) +def run_cmd(*args, env=None): + r = subprocess.Popen( + args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env={**os.environ, **(env or {})}, + ) stdoutdata, stderrdata = r.communicate() ret = r.wait() return CmdResult(ret, stdoutdata, stderrdata) +def run_psql(*args): + env = {} + user = _settings.get("USER") + if user: # pragma: no branch + args = ("-U", user, *args) + password = _settings.get("PASSWORD") + if password: # pragma: no branch + env["PGPASSWORD"] = password + host = _settings.get("HOST") + if host: # pragma: no branch + args = ("-h", host, *args) + return run_cmd("psql", *args, env=env) + + def run_mysql(*args): - user = _settings.get("USER", None) + user = _settings.get("USER") if user: # pragma: no branch - args = ("-u", user) + tuple(args) - args = ("mysql",) + tuple(args) - return run_cmd(*args) + args = ("-u", user, *args) + password = _settings.get("PASSWORD") + if password: # pragma: no branch + # Note: "-ppassword" must be a single argument. + args = ("-p" + password, *args) + host = _settings.get("HOST") + if host: # pragma: no branch + args = ("-h", host, *args) + return run_cmd("mysql", *args) def skip_if_sqlite_in_memory(): @@ -64,7 +93,7 @@ def skip_if_sqlite_in_memory(): def _get_db_name(db_suffix=None): name = TEST_DB_NAME if db_suffix: - name = "%s_%s" % (name, db_suffix) + name = "{}_{}".format(name, db_suffix) return name @@ -72,8 +101,8 @@ def drop_database(db_suffix=None): name = _get_db_name(db_suffix) db_engine = get_db_engine() - if db_engine == "postgresql_psycopg2": - r = run_cmd("psql", "postgres", "-c", "DROP DATABASE %s" % name) + if db_engine == "postgresql": + r = run_psql("postgres", "-c", "DROP DATABASE %s" % name) assert "DROP DATABASE" in force_str( r.std_out ) or "does not exist" in force_str(r.std_err) @@ -94,8 +123,8 @@ def db_exists(db_suffix=None): name = _get_db_name(db_suffix) db_engine = get_db_engine() - if db_engine == "postgresql_psycopg2": - r = run_cmd("psql", name, "-c", "SELECT 1") + if db_engine == "postgresql": + r = run_psql(name, "-c", "SELECT 1") return r.status_code == 0 if db_engine == "mysql": @@ -111,8 +140,8 @@ def db_exists(db_suffix=None): def mark_database(): db_engine = get_db_engine() - if db_engine == "postgresql_psycopg2": - r = run_cmd("psql", TEST_DB_NAME, "-c", "CREATE TABLE mark_table();") + if db_engine == "postgresql": + r = run_psql(TEST_DB_NAME, "-c", "CREATE TABLE mark_table();") assert r.status_code == 0 return @@ -136,11 +165,10 @@ def mark_database(): def mark_exists(): db_engine = get_db_engine() - if db_engine == "postgresql_psycopg2": - r = run_cmd("psql", TEST_DB_NAME, "-c", "SELECT 1 FROM mark_table") + if db_engine == "postgresql": + r = run_psql(TEST_DB_NAME, "-c", "SELECT 1 FROM mark_table") - # When something pops out on std_out, we are good - return bool(r.std_out) + return r.status_code == 0 if db_engine == "mysql": r = run_mysql(TEST_DB_NAME, "-e", "SELECT 1 FROM mark_table") diff --git a/pytest_django_test/db_router.py b/pytest_django_test/db_router.py new file mode 100644 index 000000000..c2486e957 --- /dev/null +++ b/pytest_django_test/db_router.py @@ -0,0 +1,14 @@ +class DbRouter: + def db_for_read(self, model, **hints): + if model._meta.app_label == 'app' and model._meta.model_name == 'seconditem': + return 'second' + return None + + def db_for_write(self, model, **hints): + if model._meta.app_label == 'app' and model._meta.model_name == 'seconditem': + return 'second' + return None + + def allow_migrate(self, db, app_label, model_name=None, **hints): + if app_label == 'app' and model_name == 'seconditem': + return db == 'second' diff --git a/pytest_django_test/settings_base.py b/pytest_django_test/settings_base.py index 050386299..d1694cd28 100644 --- a/pytest_django_test/settings_base.py +++ b/pytest_django_test/settings_base.py @@ -1,5 +1,3 @@ -import django - ROOT_URLCONF = "pytest_django_test.urls" INSTALLED_APPS = [ "django.contrib.auth", @@ -20,9 +18,6 @@ "django.contrib.messages.middleware.MessageMiddleware", ] -if django.VERSION < (1, 10): - MIDDLEWARE_CLASSES = MIDDLEWARE - TEMPLATES = [ { @@ -32,3 +27,7 @@ "OPTIONS": {}, } ] + +DATABASE_ROUTERS = ['pytest_django_test.db_router.DbRouter'] + +USE_TZ = True diff --git a/pytest_django_test/settings_mysql_innodb.py b/pytest_django_test/settings_mysql_innodb.py index 1fa08885a..062cfac03 100644 --- a/pytest_django_test/settings_mysql_innodb.py +++ b/pytest_django_test/settings_mysql_innodb.py @@ -1,11 +1,53 @@ +from os import environ + from .settings_base import * # noqa: F401 F403 + DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", - "NAME": "pytest_django_should_never_get_accessed", - "HOST": "localhost", - "USER": "root", - "OPTIONS": {"init_command": "SET default_storage_engine=InnoDB"}, - } + "NAME": "pytest_django_tests_default", + "USER": environ.get("TEST_DB_USER", "root"), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", "localhost"), + "OPTIONS": { + "init_command": "SET default_storage_engine=InnoDB", + "charset": "utf8mb4", + }, + "TEST": { + "CHARSET": "utf8mb4", + "COLLATION": "utf8mb4_unicode_ci", + }, + }, + "replica": { + "ENGINE": "django.db.backends.mysql", + "NAME": "pytest_django_tests_replica", + "USER": environ.get("TEST_DB_USER", "root"), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", "localhost"), + "OPTIONS": { + "init_command": "SET default_storage_engine=InnoDB", + "charset": "utf8mb4", + }, + "TEST": { + "MIRROR": "default", + "CHARSET": "utf8mb4", + "COLLATION": "utf8mb4_unicode_ci", + }, + }, + "second": { + "ENGINE": "django.db.backends.mysql", + "NAME": "pytest_django_tests_second", + "USER": environ.get("TEST_DB_USER", "root"), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", "localhost"), + "OPTIONS": { + "init_command": "SET default_storage_engine=InnoDB", + "charset": "utf8mb4", + }, + "TEST": { + "CHARSET": "utf8mb4", + "COLLATION": "utf8mb4_unicode_ci", + }, + }, } diff --git a/pytest_django_test/settings_mysql_myisam.py b/pytest_django_test/settings_mysql_myisam.py index d0a89afac..d939b7cb9 100644 --- a/pytest_django_test/settings_mysql_myisam.py +++ b/pytest_django_test/settings_mysql_myisam.py @@ -1,11 +1,53 @@ +from os import environ + from .settings_base import * # noqa: F401 F403 + DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", - "NAME": "pytest_django_should_never_get_accessed", - "HOST": "localhost", - "USER": "root", - "OPTIONS": {"init_command": "SET default_storage_engine=MyISAM"}, - } + "NAME": "pytest_django_tests_default", + "USER": environ.get("TEST_DB_USER", "root"), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", "localhost"), + "OPTIONS": { + "init_command": "SET default_storage_engine=MyISAM", + "charset": "utf8mb4", + }, + "TEST": { + "CHARSET": "utf8mb4", + "COLLATION": "utf8mb4_unicode_ci", + }, + }, + "replica": { + "ENGINE": "django.db.backends.mysql", + "NAME": "pytest_django_tests_replica", + "USER": environ.get("TEST_DB_USER", "root"), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", "localhost"), + "OPTIONS": { + "init_command": "SET default_storage_engine=MyISAM", + "charset": "utf8mb4", + }, + "TEST": { + "MIRROR": "default", + "CHARSET": "utf8mb4", + "COLLATION": "utf8mb4_unicode_ci", + }, + }, + "second": { + "ENGINE": "django.db.backends.mysql", + "NAME": "pytest_django_tests_second", + "USER": environ.get("TEST_DB_USER", "root"), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", "localhost"), + "OPTIONS": { + "init_command": "SET default_storage_engine=MyISAM", + "charset": "utf8mb4", + }, + "TEST": { + "CHARSET": "utf8mb4", + "COLLATION": "utf8mb4_unicode_ci", + }, + }, } diff --git a/pytest_django_test/settings_postgres.py b/pytest_django_test/settings_postgres.py index 2598beec9..2661fbc5a 100644 --- a/pytest_django_test/settings_postgres.py +++ b/pytest_django_test/settings_postgres.py @@ -1,8 +1,11 @@ +from os import environ + from .settings_base import * # noqa: F401 F403 + # PyPy compatibility try: - from psycopg2ct import compat + from psycopg2cffi import compat compat.register() except ImportError: @@ -11,9 +14,27 @@ DATABASES = { "default": { - "ENGINE": "django.db.backends.postgresql_psycopg2", - "NAME": "pytest_django_should_never_get_accessed", - "HOST": "localhost", - "USER": "", - } + "ENGINE": "django.db.backends.postgresql", + "NAME": "pytest_django_tests_default", + "USER": environ.get("TEST_DB_USER", ""), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", ""), + }, + "replica": { + "ENGINE": "django.db.backends.postgresql", + "NAME": "pytest_django_tests_replica", + "USER": environ.get("TEST_DB_USER", ""), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", ""), + "TEST": { + "MIRROR": "default", + }, + }, + "second": { + "ENGINE": "django.db.backends.postgresql", + "NAME": "pytest_django_tests_second", + "USER": environ.get("TEST_DB_USER", ""), + "PASSWORD": environ.get("TEST_DB_PASSWORD", ""), + "HOST": environ.get("TEST_DB_HOST", ""), + }, } diff --git a/pytest_django_test/settings_sqlite.py b/pytest_django_test/settings_sqlite.py index 8ace0293b..057b83449 100644 --- a/pytest_django_test/settings_sqlite.py +++ b/pytest_django_test/settings_sqlite.py @@ -1,8 +1,20 @@ from .settings_base import * # noqa: F401 F403 + DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", - "NAME": "/should_not_be_accessed", - } + "NAME": ":memory:", + }, + "replica": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": ":memory:", + "TEST": { + "MIRROR": "default", + }, + }, + "second": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": ":memory:", + }, } diff --git a/pytest_django_test/settings_sqlite_file.py b/pytest_django_test/settings_sqlite_file.py index a4e77ab11..206b658a2 100644 --- a/pytest_django_test/settings_sqlite_file.py +++ b/pytest_django_test/settings_sqlite_file.py @@ -2,16 +2,36 @@ from .settings_base import * # noqa: F401 F403 + # This is a SQLite configuration, which uses a file based database for # tests (via setting TEST_NAME / TEST['NAME']). # The name as expected / used by Django/pytest_django (tests/db_helpers.py). -_fd, _filename = tempfile.mkstemp(prefix="test_") +_fd, _filename_default = tempfile.mkstemp(prefix="test_") +_fd, _filename_replica = tempfile.mkstemp(prefix="test_") +_fd, _filename_second = tempfile.mkstemp(prefix="test_") DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", - "NAME": "/pytest_django_should_never_get_accessed", - "TEST": {"NAME": _filename}, - } + "NAME": "/pytest_django_tests_default", + "TEST": { + "NAME": _filename_default, + }, + }, + "replica": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": "/pytest_django_tests_replica", + "TEST": { + "MIRROR": "default", + "NAME": _filename_replica, + }, + }, + "second": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": "/pytest_django_tests_second", + "TEST": { + "NAME": _filename_second, + }, + }, } diff --git a/pytest_django_test/urls.py b/pytest_django_test/urls.py index e96a371df..956dcef93 100644 --- a/pytest_django_test/urls.py +++ b/pytest_django_test/urls.py @@ -1,8 +1,9 @@ -from django.conf.urls import url +from django.urls import path from .app import views + urlpatterns = [ - url(r"^item_count/$", views.item_count), - url(r"^admin-required/$", views.admin_required_view), + path("item_count/", views.item_count), + path("admin-required/", views.admin_required_view), ] diff --git a/pytest_django_test/urls_overridden.py b/pytest_django_test/urls_overridden.py index eca54e663..b84507fed 100644 --- a/pytest_django_test/urls_overridden.py +++ b/pytest_django_test/urls_overridden.py @@ -1,6 +1,7 @@ -from django.conf.urls import url from django.http import HttpResponse +from django.urls import path + urlpatterns = [ - url(r"^overridden_url/$", lambda r: HttpResponse("Overridden urlconf works!")) + path("overridden_url/", lambda r: HttpResponse("Overridden urlconf works!")) ] diff --git a/setup.cfg b/setup.cfg index a26f8c40b..259fb749d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,13 +1,67 @@ +[metadata] +name = pytest-django +description = A Django plugin for pytest. +long_description = file: README.rst +long_description_content_type = text/x-rst +author = Andreas Pelme +author_email = andreas@pelme.se +maintainer = Andreas Pelme +maintainer_email = andreas@pelme.se +url = https://pytest-django.readthedocs.io/ +license = BSD-3-Clause +license_file = LICENSE +classifiers = + Development Status :: 5 - Production/Stable + Framework :: Django + Framework :: Django :: 2.2 + Framework :: Django :: 3.2 + Framework :: Django :: 4.0 + Intended Audience :: Developers + License :: OSI Approved :: BSD License + Operating System :: OS Independent + Programming Language :: Python + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: Implementation :: CPython + Programming Language :: Python :: Implementation :: PyPy + Topic :: Software Development :: Testing +project_urls = + Source=https://github.com/pytest-dev/pytest-django + Changelog=https://pytest-django.readthedocs.io/en/latest/changelog.html + +[options] +packages = pytest_django +python_requires = >=3.5 +setup_requires = setuptools_scm>=5.0.0 +install_requires = pytest>=5.4.0 +zip_safe = no + +[options.entry_points] +pytest11 = + django = pytest_django.plugin + +[options.extras_require] +docs = + sphinx + sphinx_rtd_theme +testing = + Django + django-configurations>=2.0 + +[options.package_data] +pytest_django = py.typed + [tool:pytest] -# --strict: warnings become errors. +# --strict-markers: error on using unregistered marker. # -ra: show extra test summary info for everything. -addopts = --strict -ra +addopts = --strict-markers -ra DJANGO_SETTINGS_MODULE = pytest_django_test.settings_sqlite_file testpaths = tests -[wheel] -universal = 1 - [flake8] # W503 line break before binary operator ignore = W503 @@ -15,8 +69,28 @@ max-line-length = 99 exclude = lib/,src/,docs/,bin/ [isort] -# NOTE: local imports are handled special (they do not get handled as "tests"). -forced_separate=tests,pytest_django,pytest_django_test +forced_separate = tests,pytest_django,pytest_django_test +combine_as_imports = true +default_section = THIRDPARTY +include_trailing_comma = true +line_length = 79 +multi_line_output = 5 +lines_after_imports = 2 -[metadata] -license_file=LICENSE +[mypy] +check_untyped_defs = True +disallow_any_generics = True +no_implicit_optional = True +show_error_codes = True +strict_equality = True +warn_redundant_casts = True +warn_unreachable = True +warn_unused_configs = True +no_implicit_reexport = True + +[mypy-django.*] +ignore_missing_imports = True +[mypy-configurations.*] +ignore_missing_imports = True +[mypy-psycopg2cffi.*] +ignore_missing_imports = True diff --git a/setup.py b/setup.py index 217593232..7f1a1763c 100755 --- a/setup.py +++ b/setup.py @@ -1,77 +1,4 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -import codecs -import os - from setuptools import setup - -# Utility function to read the README file. -# Used for the long_description. It's nice, because now 1) we have a top level -# README file and 2) it's easier to type in the README file than to put a raw -# string in below ... -def read(fname): - file_path = os.path.join(os.path.dirname(__file__), fname) - return codecs.open(file_path, encoding='utf-8').read() - - -setup( - name='pytest-django', - use_scm_version=True, - description='A Django plugin for pytest.', - author='Andreas Pelme', - author_email='andreas@pelme.se', - maintainer="Andreas Pelme", - maintainer_email="andreas@pelme.se", - url='https://pytest-django.readthedocs.io/', - license='BSD-3-Clause', - packages=['pytest_django'], - long_description=read('README.rst'), - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - setup_requires=['setuptools_scm>=1.11.1'], - install_requires=[ - 'pytest>=3.6', - 'pathlib2;python_version<"3.4"', - ], - extras_require={ - 'docs': [ - 'sphinx', - 'sphinx_rtd_theme', - ], - 'testing': [ - 'Django', - 'django-configurations>=2.0', - 'six', - ], - }, - classifiers=['Development Status :: 5 - Production/Stable', - 'Framework :: Django', - 'Framework :: Django :: 1.8', - 'Framework :: Django :: 1.9', - 'Framework :: Django :: 1.10', - 'Framework :: Django :: 1.11', - 'Framework :: Django :: 2.0', - 'Framework :: Django :: 2.1', - 'Framework :: Django :: 2.2', - 'Framework :: Django :: 3.0', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', - 'Operating System :: OS Independent', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: Implementation :: CPython', - 'Programming Language :: Python :: Implementation :: PyPy', - 'Topic :: Software Development :: Testing', - ], - project_urls={ - 'Source': 'https://github.com/pytest-dev/pytest-django', - 'Changelog': 'https://pytest-django.readthedocs.io/en/latest/changelog.html', - }, - # the following makes a plugin available to pytest - entry_points={'pytest11': ['django = pytest_django.plugin']}) +if __name__ == "__main__": + setup() diff --git a/tests/conftest.py b/tests/conftest.py index 8b76aba29..beb6cfff2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,28 +1,29 @@ import copy +import pathlib import shutil from textwrap import dedent +from typing import Optional import pytest -import six from django.conf import settings -try: - import pathlib -except ImportError: - import pathlib2 as pathlib pytest_plugins = "pytester" REPOSITORY_ROOT = pathlib.Path(__file__).parent -def pytest_configure(config): +def pytest_configure(config) -> None: config.addinivalue_line( "markers", "django_project: options for the django_testdir fixture" ) -def _marker_apifun(extra_settings="", create_manage_py=False, project_root=None): +def _marker_apifun( + extra_settings: str = "", + create_manage_py: bool = False, + project_root: Optional[str] = None, +): return { "extra_settings": extra_settings, "create_manage_py": create_manage_py, @@ -38,7 +39,9 @@ def testdir(testdir, monkeypatch): @pytest.fixture(scope="function") def django_testdir(request, testdir, monkeypatch): - from pytest_django_test.db_helpers import DB_NAME, TEST_DB_NAME + from pytest_django_test.db_helpers import ( + DB_NAME, SECOND_DB_NAME, SECOND_TEST_DB_NAME, TEST_DB_NAME, + ) marker = request.node.get_closest_marker("django_project") @@ -50,6 +53,8 @@ def django_testdir(request, testdir, monkeypatch): db_settings = copy.deepcopy(settings.DATABASES) db_settings["default"]["NAME"] = DB_NAME db_settings["default"]["TEST"]["NAME"] = TEST_DB_NAME + db_settings["second"]["NAME"] = SECOND_DB_NAME + db_settings["second"].setdefault("TEST", {})["NAME"] = SECOND_TEST_DB_NAME test_settings = ( dedent( @@ -58,13 +63,14 @@ def django_testdir(request, testdir, monkeypatch): # Pypy compatibility try: - from psycopg2ct import compat + from psycopg2cffi import compat except ImportError: pass else: compat.register() DATABASES = %(db_settings)s + DATABASE_ROUTERS = ['pytest_django_test.db_router.DbRouter'] INSTALLED_APPS = [ 'django.contrib.auth', @@ -81,9 +87,6 @@ def django_testdir(request, testdir, monkeypatch): 'django.contrib.messages.middleware.MessageMiddleware', ] - if django.VERSION < (1, 10): - MIDDLEWARE_CLASSES = MIDDLEWARE - TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', @@ -118,17 +121,17 @@ def django_testdir(request, testdir, monkeypatch): test_app_path = tpkg_path.join("app") # Copy the test app to make it available in the new test run - shutil.copytree(six.text_type(app_source), six.text_type(test_app_path)) + shutil.copytree(str(app_source), str(test_app_path)) tpkg_path.join("the_settings.py").write(test_settings) monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.the_settings") - def create_test_module(test_code, filename="test_the_test.py"): + def create_test_module(test_code: str, filename: str = "test_the_test.py"): r = tpkg_path.join(filename) r.write(dedent(test_code), ensure=True) return r - def create_app_file(code, filename): + def create_app_file(code: str, filename: str): r = test_app_path.join(filename) r.write(dedent(code), ensure=True) return r @@ -140,7 +143,7 @@ def create_app_file(code, filename): testdir.makeini( """ [pytest] - addopts = --strict + addopts = --strict-markers console_output_style=classic """ ) diff --git a/tests/test_asserts.py b/tests/test_asserts.py index 578fb05ab..0434f1682 100644 --- a/tests/test_asserts.py +++ b/tests/test_asserts.py @@ -2,23 +2,25 @@ Tests the dynamic loading of all Django assertion cases. """ import inspect +from typing import List import pytest -import pytest_django +import pytest_django from pytest_django.asserts import __all__ as asserts_all -def _get_actual_assertions_names(): +def _get_actual_assertions_names() -> List[str]: """ Returns list with names of all assertion helpers in Django. """ - from django.test import TestCase as DjangoTestCase from unittest import TestCase as DefaultTestCase + from django.test import TestCase as DjangoTestCase + obj = DjangoTestCase('run') - def is_assert(func): + def is_assert(func) -> bool: return func.startswith('assert') and '_' not in func base_methods = [name for name, member in @@ -29,7 +31,7 @@ def is_assert(func): if is_assert(name) and name not in base_methods] -def test_django_asserts_available(): +def test_django_asserts_available() -> None: django_assertions = _get_actual_assertions_names() expected_assertions = asserts_all assert set(django_assertions) == set(expected_assertions) @@ -39,8 +41,9 @@ def test_django_asserts_available(): @pytest.mark.django_db -def test_sanity(): +def test_sanity() -> None: from django.http import HttpResponse + from pytest_django.asserts import assertContains, assertNumQueries response = HttpResponse('My response') diff --git a/tests/test_database.py b/tests/test_database.py index 7bcb06289..510f4bffb 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -1,13 +1,11 @@ -from __future__ import with_statement - import pytest -from django.db import connection -from django.test.testcases import connections_support_transactions +from django.db import connection, transaction -from pytest_django_test.app.models import Item +from pytest_django.lazy_django import get_django_version +from pytest_django_test.app.models import Item, SecondItem -def db_supports_reset_sequences(): +def db_supports_reset_sequences() -> bool: """Return if the current db engine supports `reset_sequences`.""" return ( connection.features.supports_transactions @@ -15,7 +13,7 @@ def db_supports_reset_sequences(): ) -def test_noaccess(): +def test_noaccess() -> None: with pytest.raises(RuntimeError): Item.objects.create(name="spam") with pytest.raises(RuntimeError): @@ -23,20 +21,20 @@ def test_noaccess(): @pytest.fixture -def noaccess(): +def noaccess() -> None: with pytest.raises(RuntimeError): Item.objects.create(name="spam") with pytest.raises(RuntimeError): Item.objects.count() -def test_noaccess_fixture(noaccess): +def test_noaccess_fixture(noaccess: None) -> None: # Setup will fail if this test needs to fail pass @pytest.fixture -def non_zero_sequences_counter(db): +def non_zero_sequences_counter(db: None) -> None: """Ensure that the db's internal sequence counter is > 1. This is used to test the `reset_sequences` feature. @@ -50,43 +48,54 @@ def non_zero_sequences_counter(db): class TestDatabaseFixtures: """Tests for the different database fixtures.""" - @pytest.fixture(params=["db", "transactional_db", "django_db_reset_sequences"]) - def all_dbs(self, request): + @pytest.fixture(params=[ + "db", + "transactional_db", + "django_db_reset_sequences", + "django_db_serialized_rollback", + ]) + def all_dbs(self, request) -> None: if request.param == "django_db_reset_sequences": return request.getfixturevalue("django_db_reset_sequences") elif request.param == "transactional_db": return request.getfixturevalue("transactional_db") elif request.param == "db": return request.getfixturevalue("db") + elif request.param == "django_db_serialized_rollback": + return request.getfixturevalue("django_db_serialized_rollback") + else: + assert False # pragma: no cover - def test_access(self, all_dbs): + def test_access(self, all_dbs: None) -> None: Item.objects.create(name="spam") - def test_clean_db(self, all_dbs): + def test_clean_db(self, all_dbs: None) -> None: # Relies on the order: test_access created an object assert Item.objects.count() == 0 - def test_transactions_disabled(self, db): - if not connections_support_transactions(): + def test_transactions_disabled(self, db: None) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") assert connection.in_atomic_block - def test_transactions_enabled(self, transactional_db): - if not connections_support_transactions(): + def test_transactions_enabled(self, transactional_db: None) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") assert not connection.in_atomic_block - def test_transactions_enabled_via_reset_seq(self, django_db_reset_sequences): - if not connections_support_transactions(): + def test_transactions_enabled_via_reset_seq( + self, django_db_reset_sequences: None, + ) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") assert not connection.in_atomic_block def test_django_db_reset_sequences_fixture( - self, db, django_testdir, non_zero_sequences_counter - ): + self, db: None, django_testdir, non_zero_sequences_counter: None, + ) -> None: if not db_supports_reset_sequences(): pytest.skip( @@ -113,62 +122,130 @@ def test_django_db_reset_sequences_requested( ["*test_django_db_reset_sequences_requested PASSED*"] ) + def test_serialized_rollback(self, db: None, django_testdir) -> None: + django_testdir.create_app_file( + """ + from django.db import migrations + + def load_data(apps, schema_editor): + Item = apps.get_model("app", "Item") + Item.objects.create(name="loaded-in-migration") + + class Migration(migrations.Migration): + dependencies = [ + ("app", "0001_initial"), + ] + + operations = [ + migrations.RunPython(load_data), + ] + """, + "migrations/0002_data_migration.py", + ) + + django_testdir.create_test_module( + """ + import pytest + from .app.models import Item + + @pytest.mark.django_db(transaction=True, serialized_rollback=True) + def test_serialized_rollback_1(): + assert Item.objects.filter(name="loaded-in-migration").exists() + + @pytest.mark.django_db(transaction=True) + def test_serialized_rollback_2(django_db_serialized_rollback): + assert Item.objects.filter(name="loaded-in-migration").exists() + Item.objects.create(name="test2") + + @pytest.mark.django_db(transaction=True, serialized_rollback=True) + def test_serialized_rollback_3(): + assert Item.objects.filter(name="loaded-in-migration").exists() + assert not Item.objects.filter(name="test2").exists() + """ + ) + + result = django_testdir.runpytest_subprocess("-v") + assert result.ret == 0 + @pytest.fixture - def mydb(self, all_dbs): + def mydb(self, all_dbs: None) -> None: # This fixture must be able to access the database Item.objects.create(name="spam") - def test_mydb(self, mydb): - if not connections_support_transactions(): + def test_mydb(self, mydb: None) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") # Check the fixture had access to the db item = Item.objects.get(name="spam") assert item - def test_fixture_clean(self, all_dbs): + def test_fixture_clean(self, all_dbs: None) -> None: # Relies on the order: test_mydb created an object # See https://github.com/pytest-dev/pytest-django/issues/17 assert Item.objects.count() == 0 @pytest.fixture - def fin(self, request, all_dbs): + def fin(self, request, all_dbs: None) -> None: # This finalizer must be able to access the database request.addfinalizer(lambda: Item.objects.create(name="spam")) - def test_fin(self, fin): + def test_fin(self, fin: None) -> None: # Check finalizer has db access (teardown will fail if not) pass + @pytest.mark.skipif(get_django_version() < (3, 2), reason="Django >= 3.2 required") + def test_durable_transactions(self, all_dbs: None) -> None: + with transaction.atomic(durable=True): + item = Item.objects.create(name="foo") + assert Item.objects.get() == item + class TestDatabaseFixturesAllOrder: @pytest.fixture - def fixture_with_db(self, db): + def fixture_with_db(self, db: None) -> None: Item.objects.create(name="spam") @pytest.fixture - def fixture_with_transdb(self, transactional_db): + def fixture_with_transdb(self, transactional_db: None) -> None: Item.objects.create(name="spam") @pytest.fixture - def fixture_with_reset_sequences(self, django_db_reset_sequences): + def fixture_with_reset_sequences(self, django_db_reset_sequences: None) -> None: Item.objects.create(name="spam") - def test_trans(self, fixture_with_transdb): + @pytest.fixture + def fixture_with_serialized_rollback(self, django_db_serialized_rollback: None) -> None: + Item.objects.create(name="ham") + + def test_trans(self, fixture_with_transdb: None) -> None: pass - def test_db(self, fixture_with_db): + def test_db(self, fixture_with_db: None) -> None: pass - def test_db_trans(self, fixture_with_db, fixture_with_transdb): + def test_db_trans(self, fixture_with_db: None, fixture_with_transdb: None) -> None: pass - def test_trans_db(self, fixture_with_transdb, fixture_with_db): + def test_trans_db(self, fixture_with_transdb: None, fixture_with_db: None) -> None: pass def test_reset_sequences( - self, fixture_with_reset_sequences, fixture_with_transdb, fixture_with_db - ): + self, + fixture_with_reset_sequences: None, + fixture_with_transdb: None, + fixture_with_db: None, + ) -> None: + pass + + # The test works when transactions are not supported, but it interacts + # badly with other tests. + @pytest.mark.skipif('not connection.features.supports_transactions') + def test_serialized_rollback( + self, + fixture_with_serialized_rollback: None, + fixture_with_db: None, + ) -> None: pass @@ -176,47 +253,105 @@ class TestDatabaseMarker: "Tests for the django_db marker." @pytest.mark.django_db - def test_access(self): + def test_access(self) -> None: Item.objects.create(name="spam") @pytest.mark.django_db - def test_clean_db(self): + def test_clean_db(self) -> None: # Relies on the order: test_access created an object. assert Item.objects.count() == 0 @pytest.mark.django_db - def test_transactions_disabled(self): - if not connections_support_transactions(): + def test_transactions_disabled(self) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") assert connection.in_atomic_block @pytest.mark.django_db(transaction=False) - def test_transactions_disabled_explicit(self): - if not connections_support_transactions(): + def test_transactions_disabled_explicit(self) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") assert connection.in_atomic_block @pytest.mark.django_db(transaction=True) - def test_transactions_enabled(self): - if not connections_support_transactions(): + def test_transactions_enabled(self) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") assert not connection.in_atomic_block @pytest.mark.django_db - def test_reset_sequences_disabled(self, request): + def test_reset_sequences_disabled(self, request) -> None: marker = request.node.get_closest_marker("django_db") assert not marker.kwargs @pytest.mark.django_db(reset_sequences=True) - def test_reset_sequences_enabled(self, request): + def test_reset_sequences_enabled(self, request) -> None: marker = request.node.get_closest_marker("django_db") assert marker.kwargs["reset_sequences"] + @pytest.mark.django_db(transaction=True, reset_sequences=True) + def test_transaction_reset_sequences_enabled(self, request) -> None: + marker = request.node.get_closest_marker("django_db") + assert marker.kwargs["reset_sequences"] + + @pytest.mark.django_db(databases=['default', 'replica', 'second']) + def test_databases(self, request) -> None: + marker = request.node.get_closest_marker("django_db") + assert marker.kwargs["databases"] == ['default', 'replica', 'second'] + + @pytest.mark.django_db(databases=['second']) + def test_second_database(self, request) -> None: + SecondItem.objects.create(name="spam") + + @pytest.mark.django_db(databases=['default']) + def test_not_allowed_database(self, request) -> None: + with pytest.raises(AssertionError, match='not allowed'): + SecondItem.objects.count() + with pytest.raises(AssertionError, match='not allowed'): + SecondItem.objects.create(name="spam") + + @pytest.mark.django_db(databases=['replica']) + def test_replica_database(self, request) -> None: + Item.objects.using('replica').count() + + @pytest.mark.django_db(databases=['replica']) + def test_replica_database_not_allowed(self, request) -> None: + with pytest.raises(AssertionError, match='not allowed'): + Item.objects.count() + + @pytest.mark.django_db(transaction=True, databases=['default', 'replica']) + def test_replica_mirrors_default_database(self, request) -> None: + Item.objects.create(name='spam') + Item.objects.using('replica').create(name='spam') + + assert Item.objects.count() == 2 + assert Item.objects.using('replica').count() == 2 + + @pytest.mark.django_db(databases='__all__') + def test_all_databases(self, request) -> None: + Item.objects.count() + Item.objects.create(name="spam") + SecondItem.objects.count() + SecondItem.objects.create(name="spam") + + @pytest.mark.django_db + def test_serialized_rollback_disabled(self, request): + marker = request.node.get_closest_marker("django_db") + assert not marker.kwargs + + # The test works when transactions are not supported, but it interacts + # badly with other tests. + @pytest.mark.skipif('not connection.features.supports_transactions') + @pytest.mark.django_db(serialized_rollback=True) + def test_serialized_rollback_enabled(self, request): + marker = request.node.get_closest_marker("django_db") + assert marker.kwargs["serialized_rollback"] + -def test_unittest_interaction(django_testdir): +def test_unittest_interaction(django_testdir) -> None: "Test that (non-Django) unittests cannot access the DB." django_testdir.create_test_module( @@ -261,7 +396,7 @@ def test_db_access_3(self): class Test_database_blocking: - def test_db_access_in_conftest(self, django_testdir): + def test_db_access_in_conftest(self, django_testdir) -> None: """Make sure database access in conftest module is prohibited.""" django_testdir.makeconftest( @@ -279,7 +414,7 @@ def test_db_access_in_conftest(self, django_testdir): ] ) - def test_db_access_in_test_module(self, django_testdir): + def test_db_access_in_test_module(self, django_testdir) -> None: django_testdir.create_test_module( """ from tpkg.app.models import Item diff --git a/tests/test_db_access_in_repr.py b/tests/test_db_access_in_repr.py index 89158c40e..64ae4132f 100644 --- a/tests/test_db_access_in_repr.py +++ b/tests/test_db_access_in_repr.py @@ -1,4 +1,4 @@ -def test_db_access_with_repr_in_report(django_testdir): +def test_db_access_with_repr_in_report(django_testdir) -> None: django_testdir.create_test_module( """ import pytest @@ -21,7 +21,7 @@ def test_via_db_fixture(db): "tpkg/test_the_test.py:8: ", 'self = *RuntimeError*Database access not allowed*', "E *DoesNotExist: Item matching query does not exist.", - "* 2 failed in *", + "* 2 failed*", ]) assert "INTERNALERROR" not in str(result.stdout) + str(result.stderr) assert result.ret == 1 diff --git a/tests/test_db_setup.py b/tests/test_db_setup.py index 5b8fcd38c..8f10a6804 100644 --- a/tests/test_db_setup.py +++ b/tests/test_db_setup.py @@ -1,16 +1,12 @@ import pytest -from pytest_django.lazy_django import get_django_version from pytest_django_test.db_helpers import ( - db_exists, - drop_database, - mark_database, - mark_exists, + db_exists, drop_database, mark_database, mark_exists, skip_if_sqlite_in_memory, ) -def test_db_reuse_simple(django_testdir): +def test_db_reuse_simple(django_testdir) -> None: "A test for all backends to check that `--reuse-db` works." django_testdir.create_test_module( """ @@ -29,11 +25,15 @@ def test_db_can_be_accessed(): result.stdout.fnmatch_lines(["*test_db_can_be_accessed PASSED*"]) -def test_db_order(django_testdir): +def test_db_order(django_testdir) -> None: """Test order in which tests are being executed.""" django_testdir.create_test_module(''' import pytest + from unittest import TestCase + from django.test import SimpleTestCase + from django.test import TestCase as DjangoTestCase + from django.test import TransactionTestCase from .app.models import Item @@ -44,24 +44,67 @@ def test_run_second_decorator(): def test_run_second_fixture(transactional_db): pass + def test_run_second_reset_sequences_fixture(django_db_reset_sequences): + pass + + class MyTransactionTestCase(TransactionTestCase): + def test_run_second_transaction_test_case(self): + pass + def test_run_first_fixture(db): pass + class TestClass: + def test_run_second_fixture_class(self, transactional_db): + pass + + def test_run_first_fixture_class(self, db): + pass + + @pytest.mark.django_db(reset_sequences=True) + def test_run_second_reset_sequences_decorator(): + pass + + class MyDjangoTestCase(DjangoTestCase): + def test_run_first_django_test_case(self): + pass + + class MySimpleTestCase(SimpleTestCase): + def test_run_last_simple_test_case(self): + pass + @pytest.mark.django_db def test_run_first_decorator(): pass + + @pytest.mark.django_db(serialized_rollback=True) + def test_run_first_serialized_rollback_decorator(): + pass + + class MyTestCase(TestCase): + def test_run_last_test_case(self): + pass ''') - result = django_testdir.runpytest_subprocess('-v', '-s') + result = django_testdir.runpytest_subprocess('-q', '--collect-only') assert result.ret == 0 result.stdout.fnmatch_lines([ "*test_run_first_fixture*", + "*test_run_first_fixture_class*", + "*test_run_first_django_test_case*", "*test_run_first_decorator*", + "*test_run_first_serialized_rollback_decorator*", "*test_run_second_decorator*", "*test_run_second_fixture*", - ]) + "*test_run_second_reset_sequences_fixture*", + "*test_run_second_transaction_test_case*", + "*test_run_second_fixture_class*", + "*test_run_second_reset_sequences_decorator*", + "*test_run_last_simple_test_case*", + "*test_run_last_test_case*", + ], consecutive=True) -def test_db_reuse(django_testdir): +def test_db_reuse(django_testdir) -> None: """ Test the re-use db functionality. """ @@ -123,7 +166,7 @@ class TestSqlite: } } - def test_sqlite_test_name_used(self, django_testdir): + def test_sqlite_test_name_used(self, django_testdir) -> None: django_testdir.create_test_module( """ @@ -146,7 +189,7 @@ def test_a(): result.stdout.fnmatch_lines(["*test_a*PASSED*"]) -def test_xdist_with_reuse(django_testdir): +def test_xdist_with_reuse(django_testdir) -> None: pytest.importorskip("xdist") skip_if_sqlite_in_memory() @@ -230,7 +273,7 @@ class TestSqliteWithXdist: } } - def test_sqlite_in_memory_used(self, django_testdir): + def test_sqlite_in_memory_used(self, django_testdir) -> None: pytest.importorskip("xdist") django_testdir.create_test_module( @@ -267,7 +310,7 @@ class TestSqliteWithMultipleDbsAndXdist: } } - def test_sqlite_database_renamed(self, django_testdir): + def test_sqlite_database_renamed(self, django_testdir) -> None: pytest.importorskip("xdist") django_testdir.create_test_module( @@ -313,7 +356,7 @@ class TestSqliteWithTox: } } - def test_db_with_tox_suffix(self, django_testdir, monkeypatch): + def test_db_with_tox_suffix(self, django_testdir, monkeypatch) -> None: "A test to check that Tox DB suffix works when running in parallel." monkeypatch.setenv("TOX_PARALLEL_ENV", "py37-django22") @@ -337,7 +380,7 @@ def test_inner(): assert result.ret == 0 result.stdout.fnmatch_lines(["*test_inner*PASSED*"]) - def test_db_with_empty_tox_suffix(self, django_testdir, monkeypatch): + def test_db_with_empty_tox_suffix(self, django_testdir, monkeypatch) -> None: "A test to check that Tox DB suffix is not used when suffix would be empty." monkeypatch.setenv("TOX_PARALLEL_ENV", "") @@ -372,7 +415,7 @@ class TestSqliteWithToxAndXdist: } } - def test_db_with_tox_suffix(self, django_testdir, monkeypatch): + def test_db_with_tox_suffix(self, django_testdir, monkeypatch) -> None: "A test to check that both Tox and xdist suffixes work together." pytest.importorskip("xdist") monkeypatch.setenv("TOX_PARALLEL_ENV", "py37-django22") @@ -408,7 +451,7 @@ class TestSqliteInMemoryWithXdist: } } - def test_sqlite_in_memory_used(self, django_testdir): + def test_sqlite_in_memory_used(self, django_testdir) -> None: pytest.importorskip("xdist") django_testdir.create_test_module( @@ -431,36 +474,10 @@ def test_a(): result.stdout.fnmatch_lines(["*PASSED*test_a*"]) -@pytest.mark.skipif( - get_django_version() >= (1, 9), - reason=( - "Django 1.9 requires migration and has no concept of initial data fixtures" - ), -) -def test_initial_data(django_testdir_initial): - """Test that initial data gets loaded.""" - django_testdir_initial.create_test_module( - """ - import pytest - - from .app.models import Item - - @pytest.mark.django_db - def test_inner(): - assert [x.name for x in Item.objects.all()] \ - == ["mark_initial_data"] - """ - ) - - result = django_testdir_initial.runpytest_subprocess("--tb=short", "-v") - assert result.ret == 0 - result.stdout.fnmatch_lines(["*test_inner*PASSED*"]) - +class TestMigrations: + """Tests for Django Migrations.""" -class TestNativeMigrations(object): - """ Tests for Django Migrations """ - - def test_no_migrations(self, django_testdir): + def test_no_migrations(self, django_testdir) -> None: django_testdir.create_test_module( """ import pytest @@ -472,12 +489,11 @@ def test_inner_migrations(): """ ) - migration_file = django_testdir.project_root.join( - "tpkg/app/migrations/0001_initial.py" - ) - assert migration_file.isfile() - migration_file.write( - 'raise Exception("This should not get imported.")', ensure=True + django_testdir.create_test_module( + """ + raise Exception("This should not get imported.") + """, + "migrations/0001_initial.py", ) result = django_testdir.runpytest_subprocess( @@ -485,9 +501,9 @@ def test_inner_migrations(): ) assert result.ret == 0 assert "Operations to perform:" not in result.stdout.str() - result.stdout.fnmatch_lines(["*= 1 passed in *"]) + result.stdout.fnmatch_lines(["*= 1 passed*"]) - def test_migrations_run(self, django_testdir): + def test_migrations_run(self, django_testdir) -> None: testdir = django_testdir testdir.create_test_module( """ @@ -524,6 +540,15 @@ class Migration(migrations.Migration): }, bases=(models.Model,), ), + migrations.CreateModel( + name='SecondItem', + fields=[ + ('id', models.AutoField(serialize=False, + auto_created=True, + primary_key=True)), + ('name', models.CharField(max_length=100)), + ], + ), migrations.RunPython( print_it, ), diff --git a/tests/test_django_configurations.py b/tests/test_django_configurations.py index 11d9c0ffd..e8d3e8add 100644 --- a/tests/test_django_configurations.py +++ b/tests/test_django_configurations.py @@ -4,6 +4,7 @@ """ import pytest + pytest.importorskip("configurations") @@ -23,7 +24,7 @@ class MySettings(Configuration): """ -def test_dc_env(testdir, monkeypatch): +def test_dc_env(testdir, monkeypatch) -> None: monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env") monkeypatch.setenv("DJANGO_CONFIGURATION", "MySettings") @@ -42,12 +43,12 @@ def test_settings(): result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines([ 'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)', - "* 1 passed in*", + "* 1 passed*", ]) assert result.ret == 0 -def test_dc_env_overrides_ini(testdir, monkeypatch): +def test_dc_env_overrides_ini(testdir, monkeypatch) -> None: monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env") monkeypatch.setenv("DJANGO_CONFIGURATION", "MySettings") @@ -73,12 +74,12 @@ def test_ds(): result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines([ 'django: settings: tpkg.settings_env (from env), configuration: MySettings (from env)', - "* 1 passed in*", + "* 1 passed*", ]) assert result.ret == 0 -def test_dc_ini(testdir, monkeypatch): +def test_dc_ini(testdir, monkeypatch) -> None: monkeypatch.delenv("DJANGO_SETTINGS_MODULE") testdir.makeini( @@ -103,12 +104,12 @@ def test_ds(): result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines([ 'django: settings: tpkg.settings_ini (from ini), configuration: MySettings (from ini)', - "* 1 passed in*", + "* 1 passed*", ]) assert result.ret == 0 -def test_dc_option(testdir, monkeypatch): +def test_dc_option(testdir, monkeypatch) -> None: monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DO_NOT_USE_env") monkeypatch.setenv("DJANGO_CONFIGURATION", "DO_NOT_USE_env") @@ -135,6 +136,6 @@ def test_ds(): result.stdout.fnmatch_lines([ 'django: settings: tpkg.settings_opt (from option),' ' configuration: MySettings (from option)', - "* 1 passed in*", + "* 1 passed*", ]) assert result.ret == 0 diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index f7c0a5d83..fb008e12f 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -3,7 +3,6 @@ If these tests fail you probably forgot to run "python setup.py develop". """ -import django import pytest @@ -19,7 +18,7 @@ """ -def test_ds_ini(testdir, monkeypatch): +def test_ds_ini(testdir, monkeypatch) -> None: monkeypatch.delenv("DJANGO_SETTINGS_MODULE") testdir.makeini( """ @@ -40,12 +39,12 @@ def test_ds(): result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines([ "django: settings: tpkg.settings_ini (from ini)", - "*= 1 passed in *", + "*= 1 passed*", ]) assert result.ret == 0 -def test_ds_env(testdir, monkeypatch): +def test_ds_env(testdir, monkeypatch) -> None: monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env") pkg = testdir.mkpydir("tpkg") settings = pkg.join("settings_env.py") @@ -61,11 +60,11 @@ def test_settings(): result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines([ "django: settings: tpkg.settings_env (from env)", - "*= 1 passed in *", + "*= 1 passed*", ]) -def test_ds_option(testdir, monkeypatch): +def test_ds_option(testdir, monkeypatch) -> None: monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DO_NOT_USE_env") testdir.makeini( """ @@ -87,11 +86,11 @@ def test_ds(): result = testdir.runpytest_subprocess("--ds=tpkg.settings_opt") result.stdout.fnmatch_lines([ "django: settings: tpkg.settings_opt (from option)", - "*= 1 passed in *", + "*= 1 passed*", ]) -def test_ds_env_override_ini(testdir, monkeypatch): +def test_ds_env_override_ini(testdir, monkeypatch) -> None: "DSM env should override ini." monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "tpkg.settings_env") testdir.makeini( @@ -116,7 +115,7 @@ def test_ds(): assert result.ret == 0 -def test_ds_non_existent(testdir, monkeypatch): +def test_ds_non_existent(testdir, monkeypatch) -> None: """ Make sure we do not fail with INTERNALERROR if an incorrect DJANGO_SETTINGS_MODULE is given. @@ -128,7 +127,7 @@ def test_ds_non_existent(testdir, monkeypatch): assert result.ret != 0 -def test_ds_after_user_conftest(testdir, monkeypatch): +def test_ds_after_user_conftest(testdir, monkeypatch) -> None: """ Test that the settings module can be imported, after pytest has adjusted the sys.path. @@ -138,11 +137,11 @@ def test_ds_after_user_conftest(testdir, monkeypatch): testdir.makepyfile(settings_after_conftest="SECRET_KEY='secret'") # testdir.makeconftest("import sys; print(sys.path)") result = testdir.runpytest_subprocess("-v") - result.stdout.fnmatch_lines(["* 1 passed in*"]) + result.stdout.fnmatch_lines(["* 1 passed*"]) assert result.ret == 0 -def test_ds_in_pytest_configure(testdir, monkeypatch): +def test_ds_in_pytest_configure(testdir, monkeypatch) -> None: monkeypatch.delenv("DJANGO_SETTINGS_MODULE") pkg = testdir.mkpydir("tpkg") settings = pkg.join("settings_ds.py") @@ -172,7 +171,7 @@ def test_anything(): assert r.ret == 0 -def test_django_settings_configure(testdir, monkeypatch): +def test_django_settings_configure(testdir, monkeypatch) -> None: """ Make sure Django can be configured without setting DJANGO_SETTINGS_MODULE altogether, relying on calling @@ -226,10 +225,10 @@ def test_user_count(): """ ) result = testdir.runpython(p) - result.stdout.fnmatch_lines(["* 4 passed in*"]) + result.stdout.fnmatch_lines(["* 4 passed*"]) -def test_settings_in_hook(testdir, monkeypatch): +def test_settings_in_hook(testdir, monkeypatch) -> None: monkeypatch.delenv("DJANGO_SETTINGS_MODULE") testdir.makeconftest( """ @@ -262,7 +261,7 @@ def test_user_count(): assert r.ret == 0 -def test_django_not_loaded_without_settings(testdir, monkeypatch): +def test_django_not_loaded_without_settings(testdir, monkeypatch) -> None: """ Make sure Django is not imported at all if no Django settings is specified. """ @@ -275,11 +274,11 @@ def test_settings(): """ ) result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(["* 1 passed in*"]) + result.stdout.fnmatch_lines(["* 1 passed*"]) assert result.ret == 0 -def test_debug_false(testdir, monkeypatch): +def test_debug_false_by_default(testdir, monkeypatch) -> None: monkeypatch.delenv("DJANGO_SETTINGS_MODULE") testdir.makeconftest( """ @@ -308,10 +307,78 @@ def test_debug_is_false(): assert r.ret == 0 -@pytest.mark.skipif( - not hasattr(django, "setup"), - reason="This Django version does not support app loading", -) +@pytest.mark.parametrize('django_debug_mode', (False, True)) +def test_django_debug_mode_true_false(testdir, monkeypatch, django_debug_mode: bool) -> None: + monkeypatch.delenv("DJANGO_SETTINGS_MODULE") + testdir.makeini( + """ + [pytest] + django_debug_mode = {} + """.format(django_debug_mode) + ) + testdir.makeconftest( + """ + from django.conf import settings + + def pytest_configure(): + settings.configure(SECRET_KEY='set from pytest_configure', + DEBUG=%s, + DATABASES={'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:'}}, + INSTALLED_APPS=['django.contrib.auth', + 'django.contrib.contenttypes',]) + """ % (not django_debug_mode) + ) + + testdir.makepyfile( + """ + from django.conf import settings + def test_debug_is_false(): + assert settings.DEBUG is {} + """.format(django_debug_mode) + ) + + r = testdir.runpytest_subprocess() + assert r.ret == 0 + + +@pytest.mark.parametrize('settings_debug', (False, True)) +def test_django_debug_mode_keep(testdir, monkeypatch, settings_debug: bool) -> None: + monkeypatch.delenv("DJANGO_SETTINGS_MODULE") + testdir.makeini( + """ + [pytest] + django_debug_mode = keep + """ + ) + testdir.makeconftest( + """ + from django.conf import settings + + def pytest_configure(): + settings.configure(SECRET_KEY='set from pytest_configure', + DEBUG=%s, + DATABASES={'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:'}}, + INSTALLED_APPS=['django.contrib.auth', + 'django.contrib.contenttypes',]) + """ % settings_debug + ) + + testdir.makepyfile( + """ + from django.conf import settings + def test_debug_is_false(): + assert settings.DEBUG is {} + """.format(settings_debug) + ) + + r = testdir.runpytest_subprocess() + assert r.ret == 0 + + @pytest.mark.django_project( extra_settings=""" INSTALLED_APPS = [ @@ -319,7 +386,7 @@ def test_debug_is_false(): ] """ ) -def test_django_setup_sequence(django_testdir): +def test_django_setup_sequence(django_testdir) -> None: django_testdir.create_app_file( """ from django.apps import apps, AppConfig @@ -329,10 +396,7 @@ class TestApp(AppConfig): name = 'tpkg.app' def ready(self): - try: - populating = apps.loading - except AttributeError: # Django < 2.0 - populating = apps._lock.locked() + populating = apps.loading print('READY(): populating=%r' % populating) """, "apps.py", @@ -342,10 +406,7 @@ def ready(self): """ from django.apps import apps - try: - populating = apps.loading - except AttributeError: # Django < 2.0 - populating = apps._lock.locked() + populating = apps.loading print('IMPORT: populating=%r,ready=%r' % (populating, apps.ready)) SOME_THING = 1234 @@ -360,10 +421,7 @@ def ready(self): from tpkg.app.models import SOME_THING def test_anything(): - try: - populating = apps.loading - except AttributeError: # Django < 2.0 - populating = apps._lock.locked() + populating = apps.loading print('TEST: populating=%r,ready=%r' % (populating, apps.ready)) """ @@ -372,14 +430,11 @@ def test_anything(): result = django_testdir.runpytest_subprocess("-s", "--tb=line") result.stdout.fnmatch_lines(["*IMPORT: populating=True,ready=False*"]) result.stdout.fnmatch_lines(["*READY(): populating=True*"]) - if django.VERSION < (2, 0): - result.stdout.fnmatch_lines(["*TEST: populating=False,ready=True*"]) - else: - result.stdout.fnmatch_lines(["*TEST: populating=True,ready=True*"]) + result.stdout.fnmatch_lines(["*TEST: populating=True,ready=True*"]) assert result.ret == 0 -def test_no_ds_but_django_imported(testdir, monkeypatch): +def test_no_ds_but_django_imported(testdir, monkeypatch) -> None: """pytest-django should not bail out, if "django" has been imported somewhere, e.g. via pytest-splinter.""" @@ -406,7 +461,7 @@ def test_cfg(pytestconfig): assert r.ret == 0 -def test_no_ds_but_django_conf_imported(testdir, monkeypatch): +def test_no_ds_but_django_conf_imported(testdir, monkeypatch) -> None: """pytest-django should not bail out, if "django.conf" has been imported somewhere, e.g. via hypothesis (#599).""" @@ -443,7 +498,7 @@ def test_cfg(pytestconfig): assert r.ret == 0 -def test_no_django_settings_but_django_imported(testdir, monkeypatch): +def test_no_django_settings_but_django_imported(testdir, monkeypatch) -> None: """Make sure we do not crash when Django happens to be imported, but settings is not properly configured""" monkeypatch.delenv("DJANGO_SETTINGS_MODULE") diff --git a/tests/test_environment.py b/tests/test_environment.py index 95f903a1b..450847fce 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -1,14 +1,11 @@ -from __future__ import with_statement - import os import pytest -from django.contrib.sites.models import Site from django.contrib.sites import models as site_models +from django.contrib.sites.models import Site from django.core import mail from django.db import connection from django.test import TestCase -from pytest_django.lazy_django import get_django_version from pytest_django_test.app.models import Item @@ -20,7 +17,7 @@ @pytest.mark.parametrize("subject", ["subject1", "subject2"]) -def test_autoclear_mailbox(subject): +def test_autoclear_mailbox(subject: str) -> None: assert len(mail.outbox) == 0 mail.send_mail(subject, "body", "from@example.com", ["to@example.com"]) assert len(mail.outbox) == 1 @@ -33,15 +30,15 @@ def test_autoclear_mailbox(subject): class TestDirectAccessWorksForDjangoTestCase(TestCase): - def _do_test(self): + def _do_test(self) -> None: assert len(mail.outbox) == 0 mail.send_mail("subject", "body", "from@example.com", ["to@example.com"]) assert len(mail.outbox) == 1 - def test_one(self): + def test_one(self) -> None: self._do_test() - def test_two(self): + def test_two(self) -> None: self._do_test() @@ -54,14 +51,14 @@ def test_two(self): ROOT_URLCONF = 'tpkg.app.urls' """ ) -def test_invalid_template_variable(django_testdir): +def test_invalid_template_variable(django_testdir) -> None: django_testdir.create_app_file( """ - from django.conf.urls import url + from django.urls import path from tpkg.app import views - urlpatterns = [url(r'invalid_template/', views.invalid_template)] + urlpatterns = [path('invalid_template/', views.invalid_template)] """, "urls.py", ) @@ -95,10 +92,7 @@ def test_ignore(client): ) result = django_testdir.runpytest_subprocess("-s", "--fail-on-template-vars") - if get_django_version() >= (1, 9): - origin = "'*/tpkg/app/templates/invalid_template_base.html'" - else: - origin = "'invalid_template.html'" + origin = "'*/tpkg/app/templates/invalid_template_base.html'" result.stdout.fnmatch_lines_random( [ "tpkg/test_the_test.py F.*", @@ -118,7 +112,7 @@ def test_ignore(client): ROOT_URLCONF = 'tpkg.app.urls' """ ) -def test_invalid_template_with_default_if_none(django_testdir): +def test_invalid_template_with_default_if_none(django_testdir) -> None: django_testdir.create_app_file( """
{{ data.empty|default:'d' }}
@@ -160,14 +154,14 @@ def test_for_invalid_template(): ROOT_URLCONF = 'tpkg.app.urls' """ ) -def test_invalid_template_variable_opt_in(django_testdir): +def test_invalid_template_variable_opt_in(django_testdir) -> None: django_testdir.create_app_file( """ - from django.conf.urls import url + from django.urls import path from tpkg.app import views - urlpatterns = [url(r'invalid_template/', views.invalid_template)] + urlpatterns = [path('invalid_template', views.invalid_template)] """, "urls.py", ) @@ -201,24 +195,24 @@ def test_ignore(client): @pytest.mark.django_db -def test_database_rollback(): +def test_database_rollback() -> None: assert Item.objects.count() == 0 Item.objects.create(name="blah") assert Item.objects.count() == 1 @pytest.mark.django_db -def test_database_rollback_again(): +def test_database_rollback_again() -> None: test_database_rollback() @pytest.mark.django_db -def test_database_name(): +def test_database_name() -> None: dirname, name = os.path.split(connection.settings_dict["NAME"]) assert "file:memorydb" in name or name == ":memory:" or name.startswith("test_") -def test_database_noaccess(): +def test_database_noaccess() -> None: with pytest.raises(RuntimeError): Item.objects.count() @@ -241,30 +235,25 @@ def test_inner_testrunner(): ) return django_testdir - def test_default(self, testdir): + def test_default(self, testdir) -> None: """Not verbose by default.""" result = testdir.runpytest_subprocess("-s") result.stdout.fnmatch_lines(["tpkg/test_the_test.py .*"]) - def test_vq_verbosity_0(self, testdir): + def test_vq_verbosity_0(self, testdir) -> None: """-v and -q results in verbosity 0.""" result = testdir.runpytest_subprocess("-s", "-v", "-q") result.stdout.fnmatch_lines(["tpkg/test_the_test.py .*"]) - def test_verbose_with_v(self, testdir): + def test_verbose_with_v(self, testdir) -> None: """Verbose output with '-v'.""" result = testdir.runpytest_subprocess("-s", "-v") result.stdout.fnmatch_lines_random(["tpkg/test_the_test.py:*", "*PASSED*"]) - if get_django_version() >= (2, 2): - result.stderr.fnmatch_lines( - ["*Destroying test database for alias 'default'*"] - ) - else: - result.stdout.fnmatch_lines( - ["*Destroying test database for alias 'default'...*"] - ) + result.stderr.fnmatch_lines( + ["*Destroying test database for alias 'default'*"] + ) - def test_more_verbose_with_vv(self, testdir): + def test_more_verbose_with_vv(self, testdir) -> None: """More verbose output with '-v -v'.""" result = testdir.runpytest_subprocess("-s", "-v", "-v") result.stdout.fnmatch_lines_random( @@ -275,42 +264,27 @@ def test_more_verbose_with_vv(self, testdir): "*PASSED*", ] ) - if get_django_version() >= (2, 2): - result.stderr.fnmatch_lines( - [ - "*Creating test database for alias*", - "*Destroying test database for alias 'default'*", - ] - ) - else: - result.stdout.fnmatch_lines( - [ - "*Creating test database for alias*", - "*Destroying test database for alias 'default'*", - ] - ) + result.stderr.fnmatch_lines( + [ + "*Creating test database for alias*", + "*Destroying test database for alias 'default'*", + ] + ) - def test_more_verbose_with_vv_and_reusedb(self, testdir): + def test_more_verbose_with_vv_and_reusedb(self, testdir) -> None: """More verbose output with '-v -v', and --create-db.""" result = testdir.runpytest_subprocess("-s", "-v", "-v", "--create-db") result.stdout.fnmatch_lines(["tpkg/test_the_test.py:*", "*PASSED*"]) - if get_django_version() >= (2, 2): - result.stderr.fnmatch_lines(["*Creating test database for alias*"]) - assert ( - "*Destroying test database for alias 'default' ('*')...*" - not in result.stderr.str() - ) - else: - result.stdout.fnmatch_lines(["*Creating test database for alias*"]) - assert ( - "*Destroying test database for alias 'default' ('*')...*" - not in result.stdout.str() - ) + result.stderr.fnmatch_lines(["*Creating test database for alias*"]) + assert ( + "*Destroying test database for alias 'default' ('*')...*" + not in result.stderr.str() + ) @pytest.mark.django_db @pytest.mark.parametrize("site_name", ["site1", "site2"]) -def test_clear_site_cache(site_name, rf, monkeypatch): +def test_clear_site_cache(site_name: str, rf, monkeypatch) -> None: request = rf.get("/") monkeypatch.setattr(request, "get_host", lambda: "foo.com") Site.objects.create(domain="foo.com", name=site_name) @@ -319,7 +293,7 @@ def test_clear_site_cache(site_name, rf, monkeypatch): @pytest.mark.django_db @pytest.mark.parametrize("site_name", ["site1", "site2"]) -def test_clear_site_cache_check_site_cache_size(site_name, settings): +def test_clear_site_cache_check_site_cache_size(site_name: str, settings) -> None: assert len(site_models.SITE_CACHE) == 0 site = Site.objects.create(domain="foo.com", name=site_name) settings.SITE_ID = site.id diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 2c718c54f..427c6c8a7 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -3,27 +3,25 @@ Not quite all fixtures are tested here, the db and transactional_db fixtures are tested in test_database. """ - -from __future__ import with_statement - import socket from contextlib import contextmanager +from typing import Generator +from urllib.error import HTTPError +from urllib.request import urlopen import pytest from django.conf import settings as real_settings from django.core import mail from django.db import connection, transaction from django.test.client import Client, RequestFactory -from django.test.testcases import connections_support_transactions from django.utils.encoding import force_str from pytest_django.lazy_django import get_django_version from pytest_django_test.app.models import Item -from pytest_django_test.compat import HTTPError, urlopen @contextmanager -def nonverbose_config(config): +def nonverbose_config(config) -> Generator[None, None, None]: """Ensure that pytest's config.option.verbose is <= 0.""" if config.option.verbose <= 0: yield @@ -34,38 +32,68 @@ def nonverbose_config(config): config.option.verbose = saved -def test_client(client): +def test_client(client) -> None: assert isinstance(client, Client) +@pytest.mark.skipif(get_django_version() < (3, 1), reason="Django >= 3.1 required") +def test_async_client(async_client) -> None: + from django.test.client import AsyncClient + + assert isinstance(async_client, AsyncClient) + + @pytest.mark.django_db -def test_admin_client(admin_client): +def test_admin_client(admin_client: Client) -> None: assert isinstance(admin_client, Client) resp = admin_client.get("/admin-required/") assert force_str(resp.content) == "You are an admin" -def test_admin_client_no_db_marker(admin_client): +def test_admin_client_no_db_marker(admin_client: Client) -> None: assert isinstance(admin_client, Client) resp = admin_client.get("/admin-required/") assert force_str(resp.content) == "You are an admin" +# For test below. +@pytest.fixture +def existing_admin_user(django_user_model): + return django_user_model._default_manager.create_superuser('admin', None, None) + + +def test_admin_client_existing_user( + db: None, + existing_admin_user, + admin_user, + admin_client: Client, +) -> None: + resp = admin_client.get("/admin-required/") + assert force_str(resp.content) == "You are an admin" + + @pytest.mark.django_db -def test_admin_user(admin_user, django_user_model): +def test_admin_user(admin_user, django_user_model) -> None: assert isinstance(admin_user, django_user_model) -def test_admin_user_no_db_marker(admin_user, django_user_model): +def test_admin_user_no_db_marker(admin_user, django_user_model) -> None: assert isinstance(admin_user, django_user_model) -def test_rf(rf): +def test_rf(rf) -> None: assert isinstance(rf, RequestFactory) +@pytest.mark.skipif(get_django_version() < (3, 1), reason="Django >= 3.1 required") +def test_async_rf(async_rf) -> None: + from django.test.client import AsyncRequestFactory + + assert isinstance(async_rf, AsyncRequestFactory) + + @pytest.mark.django_db -def test_django_assert_num_queries_db(request, django_assert_num_queries): +def test_django_assert_num_queries_db(request, django_assert_num_queries) -> None: with nonverbose_config(request.config): with django_assert_num_queries(3): Item.objects.create(name="foo") @@ -83,7 +111,7 @@ def test_django_assert_num_queries_db(request, django_assert_num_queries): @pytest.mark.django_db -def test_django_assert_max_num_queries_db(request, django_assert_max_num_queries): +def test_django_assert_max_num_queries_db(request, django_assert_max_num_queries) -> None: with nonverbose_config(request.config): with django_assert_max_num_queries(2): Item.objects.create(name="1-foo") @@ -105,8 +133,8 @@ def test_django_assert_max_num_queries_db(request, django_assert_max_num_queries @pytest.mark.django_db(transaction=True) def test_django_assert_num_queries_transactional_db( - request, transactional_db, django_assert_num_queries -): + request, transactional_db: None, django_assert_num_queries +) -> None: with nonverbose_config(request.config): with transaction.atomic(): with django_assert_num_queries(3): @@ -119,7 +147,7 @@ def test_django_assert_num_queries_transactional_db( Item.objects.create(name="quux") -def test_django_assert_num_queries_output(django_testdir): +def test_django_assert_num_queries_output(django_testdir) -> None: django_testdir.create_test_module( """ from django.contrib.contenttypes.models import ContentType @@ -137,7 +165,7 @@ def test_queries(django_assert_num_queries): assert result.ret == 1 -def test_django_assert_num_queries_output_verbose(django_testdir): +def test_django_assert_num_queries_output_verbose(django_testdir) -> None: django_testdir.create_test_module( """ from django.contrib.contenttypes.models import ContentType @@ -158,7 +186,7 @@ def test_queries(django_assert_num_queries): @pytest.mark.django_db -def test_django_assert_num_queries_db_connection(django_assert_num_queries): +def test_django_assert_num_queries_db_connection(django_assert_num_queries) -> None: from django.db import connection with django_assert_num_queries(1, connection=connection): @@ -173,7 +201,7 @@ def test_django_assert_num_queries_db_connection(django_assert_num_queries): @pytest.mark.django_db -def test_django_assert_num_queries_output_info(django_testdir): +def test_django_assert_num_queries_output_info(django_testdir) -> None: django_testdir.create_test_module( """ from django.contrib.contenttypes.models import ContentType @@ -202,43 +230,107 @@ def test_queries(django_assert_num_queries): assert result.ret == 1 +@pytest.mark.django_db +def test_django_capture_on_commit_callbacks(django_capture_on_commit_callbacks) -> None: + if not connection.features.supports_transactions: + pytest.skip("transactions required for this test") + + scratch = [] + with django_capture_on_commit_callbacks() as callbacks: + transaction.on_commit(lambda: scratch.append("one")) + assert len(callbacks) == 1 + assert scratch == [] + callbacks[0]() + assert scratch == ["one"] + + scratch = [] + with django_capture_on_commit_callbacks(execute=True) as callbacks: + transaction.on_commit(lambda: scratch.append("two")) + transaction.on_commit(lambda: scratch.append("three")) + assert len(callbacks) == 2 + assert scratch == ["two", "three"] + callbacks[0]() + assert scratch == ["two", "three", "two"] + + +@pytest.mark.django_db(databases=["default", "second"]) +def test_django_capture_on_commit_callbacks_multidb(django_capture_on_commit_callbacks) -> None: + if not connection.features.supports_transactions: + pytest.skip("transactions required for this test") + + scratch = [] + with django_capture_on_commit_callbacks(using="default", execute=True) as callbacks: + transaction.on_commit(lambda: scratch.append("one")) + assert len(callbacks) == 1 + assert scratch == ["one"] + + scratch = [] + with django_capture_on_commit_callbacks(using="second", execute=True) as callbacks: + transaction.on_commit(lambda: scratch.append("two")) # pragma: no cover + assert len(callbacks) == 0 + assert scratch == [] + + scratch = [] + with django_capture_on_commit_callbacks(using="default", execute=True) as callbacks: + transaction.on_commit(lambda: scratch.append("ten")) + transaction.on_commit(lambda: scratch.append("twenty"), using="second") # pragma: no cover + transaction.on_commit(lambda: scratch.append("thirty")) + assert len(callbacks) == 2 + assert scratch == ["ten", "thirty"] + + +@pytest.mark.django_db(transaction=True) +def test_django_capture_on_commit_callbacks_transactional( + django_capture_on_commit_callbacks, +) -> None: + if not connection.features.supports_transactions: + pytest.skip("transactions required for this test") + + # Bad usage: no transaction (executes immediately). + scratch = [] + with django_capture_on_commit_callbacks() as callbacks: + transaction.on_commit(lambda: scratch.append("one")) + assert len(callbacks) == 0 + assert scratch == ["one"] + + class TestSettings: """Tests for the settings fixture, order matters""" - def test_modify_existing(self, settings): + def test_modify_existing(self, settings) -> None: assert settings.SECRET_KEY == "foobar" assert real_settings.SECRET_KEY == "foobar" settings.SECRET_KEY = "spam" assert settings.SECRET_KEY == "spam" assert real_settings.SECRET_KEY == "spam" - def test_modify_existing_again(self, settings): + def test_modify_existing_again(self, settings) -> None: assert settings.SECRET_KEY == "foobar" assert real_settings.SECRET_KEY == "foobar" - def test_new(self, settings): + def test_new(self, settings) -> None: assert not hasattr(settings, "SPAM") assert not hasattr(real_settings, "SPAM") settings.SPAM = "ham" assert settings.SPAM == "ham" assert real_settings.SPAM == "ham" - def test_new_again(self, settings): + def test_new_again(self, settings) -> None: assert not hasattr(settings, "SPAM") assert not hasattr(real_settings, "SPAM") - def test_deleted(self, settings): + def test_deleted(self, settings) -> None: assert hasattr(settings, "SECRET_KEY") assert hasattr(real_settings, "SECRET_KEY") del settings.SECRET_KEY assert not hasattr(settings, "SECRET_KEY") assert not hasattr(real_settings, "SECRET_KEY") - def test_deleted_again(self, settings): + def test_deleted_again(self, settings) -> None: assert hasattr(settings, "SECRET_KEY") assert hasattr(real_settings, "SECRET_KEY") - def test_signals(self, settings): + def test_signals(self, settings) -> None: result = [] def assert_signal(signal, sender, setting, value, enter): @@ -260,7 +352,7 @@ def assert_signal(signal, sender, setting, value, enter): settings.FOOBAR = "abc123" assert sorted(result) == [("FOOBAR", "abc123", True)] - def test_modification_signal(self, django_testdir): + def test_modification_signal(self, django_testdir) -> None: django_testdir.create_test_module( """ import pytest @@ -318,81 +410,77 @@ def test_set_non_existent(settings): class TestLiveServer: - def test_settings_before(self): + def test_settings_before(self) -> None: from django.conf import settings assert ( - "%s.%s" % (settings.__class__.__module__, settings.__class__.__name__) + "{}.{}".format(settings.__class__.__module__, settings.__class__.__name__) == "django.conf.Settings" ) - TestLiveServer._test_settings_before_run = True + TestLiveServer._test_settings_before_run = True # type: ignore[attr-defined] - def test_url(self, live_server): + def test_url(self, live_server) -> None: assert live_server.url == force_str(live_server) - def test_change_settings(self, live_server, settings): + def test_change_settings(self, live_server, settings) -> None: assert live_server.url == force_str(live_server) - def test_settings_restored(self): + def test_settings_restored(self) -> None: """Ensure that settings are restored after test_settings_before.""" - import django from django.conf import settings - assert TestLiveServer._test_settings_before_run is True + assert TestLiveServer._test_settings_before_run is True # type: ignore[attr-defined] assert ( - "%s.%s" % (settings.__class__.__module__, settings.__class__.__name__) + "{}.{}".format(settings.__class__.__module__, settings.__class__.__name__) == "django.conf.Settings" ) - if django.VERSION >= (1, 11): - assert settings.ALLOWED_HOSTS == ["testserver"] - else: - assert settings.ALLOWED_HOSTS == ["*"] + assert settings.ALLOWED_HOSTS == ["testserver"] - def test_transactions(self, live_server): - if not connections_support_transactions(): + def test_transactions(self, live_server) -> None: + if not connection.features.supports_transactions: pytest.skip("transactions required for this test") assert not connection.in_atomic_block - def test_db_changes_visibility(self, live_server): + def test_db_changes_visibility(self, live_server) -> None: response_data = urlopen(live_server + "/item_count/").read() assert force_str(response_data) == "Item count: 0" Item.objects.create(name="foo") response_data = urlopen(live_server + "/item_count/").read() assert force_str(response_data) == "Item count: 1" - def test_fixture_db(self, db, live_server): + def test_fixture_db(self, db: None, live_server) -> None: Item.objects.create(name="foo") response_data = urlopen(live_server + "/item_count/").read() assert force_str(response_data) == "Item count: 1" - def test_fixture_transactional_db(self, transactional_db, live_server): + def test_fixture_transactional_db(self, transactional_db: None, live_server) -> None: Item.objects.create(name="foo") response_data = urlopen(live_server + "/item_count/").read() assert force_str(response_data) == "Item count: 1" @pytest.fixture - def item(self): + def item(self) -> None: # This has not requested database access explicitly, but the # live_server fixture auto-uses the transactional_db fixture. Item.objects.create(name="foo") - def test_item(self, item, live_server): + def test_item(self, item, live_server) -> None: pass @pytest.fixture - def item_db(self, db): + def item_db(self, db: None) -> Item: return Item.objects.create(name="foo") - def test_item_db(self, item_db, live_server): + def test_item_db(self, item_db: Item, live_server) -> None: response_data = urlopen(live_server + "/item_count/").read() assert force_str(response_data) == "Item count: 1" @pytest.fixture - def item_transactional_db(self, transactional_db): + def item_transactional_db(self, transactional_db: None) -> Item: return Item.objects.create(name="foo") - def test_item_transactional_db(self, item_transactional_db, live_server): + def test_item_transactional_db(self, item_transactional_db: Item, live_server) -> None: response_data = urlopen(live_server + "/item_count/").read() assert force_str(response_data) == "Item count: 1" @@ -410,19 +498,16 @@ def test_item_transactional_db(self, item_transactional_db, live_server): STATIC_URL = '/static/' """ ) - def test_serve_static_with_staticfiles_app(self, django_testdir, settings): + def test_serve_static_with_staticfiles_app(self, django_testdir, settings) -> None: """ LiveServer always serves statics with ``django.contrib.staticfiles`` handler. """ django_testdir.create_test_module( """ - from django.utils.encoding import force_str + from urllib.request import urlopen - try: - from urllib2 import urlopen - except ImportError: - from urllib.request import urlopen + from django.utils.encoding import force_str class TestLiveServer: def test_a(self, live_server, settings): @@ -437,7 +522,7 @@ def test_a(self, live_server, settings): result.stdout.fnmatch_lines(["*test_a*PASSED*"]) assert result.ret == 0 - def test_serve_static_dj17_without_staticfiles_app(self, live_server, settings): + def test_serve_static_dj17_without_staticfiles_app(self, live_server, settings) -> None: """ Because ``django.contrib.staticfiles`` is not installed LiveServer can not serve statics with django >= 1.7 . @@ -445,29 +530,7 @@ def test_serve_static_dj17_without_staticfiles_app(self, live_server, settings): with pytest.raises(HTTPError): urlopen(live_server + "/static/a_file.txt").read() - @pytest.mark.skipif( - get_django_version() < (1, 11), reason="Django >= 1.11 required" - ) - def test_specified_port_range_error_message_django_111(self, django_testdir): - django_testdir.create_test_module( - """ - def test_with_live_server(live_server): - pass - """ - ) - - result = django_testdir.runpytest_subprocess("--liveserver=localhost:1234-2345") - result.stdout.fnmatch_lines( - [ - "*Specifying multiple live server ports is not supported in Django 1.11. This " - "will be an error in a future pytest-django release.*" - ] - ) - - @pytest.mark.skipif( - get_django_version() < (1, 11, 2), reason="Django >= 1.11.2 required" - ) - def test_specified_port_django_111(self, django_testdir): + def test_specified_port_django_111(self, django_testdir) -> None: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.bind(("", 0)) @@ -500,26 +563,52 @@ def test_with_live_server(live_server): ROOT_URLCONF = 'tpkg.app.urls' """ ) -def test_custom_user_model(django_testdir, username_field): +def test_custom_user_model(django_testdir, username_field) -> None: django_testdir.create_app_file( """ - from django.contrib.auth.models import AbstractUser + from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin from django.db import models - class MyCustomUser(AbstractUser): + class MyCustomUserManager(BaseUserManager): + def create_user(self, {username_field}, password=None, **extra_fields): + extra_fields.setdefault('is_staff', False) + extra_fields.setdefault('is_superuser', False) + user = self.model({username_field}={username_field}, **extra_fields) + user.set_password(password) + user.save() + return user + + def create_superuser(self, {username_field}, password=None, **extra_fields): + extra_fields.setdefault('is_staff', True) + extra_fields.setdefault('is_superuser', True) + return self.create_user( + {username_field}={username_field}, + password=password, + **extra_fields + ) + + class MyCustomUser(AbstractBaseUser, PermissionsMixin): + email = models.EmailField(max_length=100, unique=True) identifier = models.CharField(unique=True, max_length=100) + is_staff = models.BooleanField( + 'staff status', + default=False, + help_text='Designates whether the user can log into this admin site.' + ) - USERNAME_FIELD = '%s' - """ - % (username_field), + objects = MyCustomUserManager() + + USERNAME_FIELD = '{username_field}' + """.format(username_field=username_field), "models.py", ) django_testdir.create_app_file( """ - from django.conf.urls import url + from django.urls import path + from tpkg.app import views - urlpatterns = [url(r'admin-required/', views.admin_required_view)] + urlpatterns = [path('admin-required/', views.admin_required_view)] """, "urls.py", ) @@ -550,9 +639,6 @@ def test_custom_user_model(admin_client): django_testdir.create_app_file("", "migrations/__init__.py") django_testdir.create_app_file( """ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations import django.utils.timezone import django.core.validators @@ -573,19 +659,13 @@ class Migration(migrations.Migration): ('password', models.CharField(max_length=128, verbose_name='password')), ('last_login', models.DateTimeField(null=True, verbose_name='last login', blank=True)), ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), - ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, max_length=30, validators=[django.core.validators.RegexValidator(r'^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.', 'invalid')], help_text='Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.', unique=True, verbose_name='username')), - ('first_name', models.CharField(max_length=30, verbose_name='first name', blank=True)), - ('last_name', models.CharField(max_length=30, verbose_name='last name', blank=True)), - ('email', models.EmailField(max_length=254, verbose_name='email address', blank=True)), + ('email', models.EmailField(error_messages={'unique': 'A user with that email address already exists.'}, max_length=100, unique=True, verbose_name='email address')), ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), - ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), - ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), ('identifier', models.CharField(unique=True, max_length=100)), ('groups', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Group', blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', verbose_name='groups')), ('user_permissions', models.ManyToManyField(related_query_name='user', related_name='user_set', to='auth.Permission', blank=True, help_text='Specific permissions for this user.', verbose_name='user permissions')), ], options={ - 'abstract': False, 'verbose_name': 'user', 'verbose_name_plural': 'users', }, @@ -597,13 +677,13 @@ class Migration(migrations.Migration): ) result = django_testdir.runpytest_subprocess("-s") - result.stdout.fnmatch_lines(["* 1 passed in*"]) + result.stdout.fnmatch_lines(["* 1 passed*"]) assert result.ret == 0 class Test_django_db_blocker: @pytest.mark.django_db - def test_block_manually(self, django_db_blocker): + def test_block_manually(self, django_db_blocker) -> None: try: django_db_blocker.block() with pytest.raises(RuntimeError): @@ -612,24 +692,24 @@ def test_block_manually(self, django_db_blocker): django_db_blocker.restore() @pytest.mark.django_db - def test_block_with_block(self, django_db_blocker): + def test_block_with_block(self, django_db_blocker) -> None: with django_db_blocker.block(): with pytest.raises(RuntimeError): Item.objects.exists() - def test_unblock_manually(self, django_db_blocker): + def test_unblock_manually(self, django_db_blocker) -> None: try: django_db_blocker.unblock() Item.objects.exists() finally: django_db_blocker.restore() - def test_unblock_with_block(self, django_db_blocker): + def test_unblock_with_block(self, django_db_blocker) -> None: with django_db_blocker.unblock(): Item.objects.exists() -def test_mail(mailoutbox): +def test_mail(mailoutbox) -> None: assert ( mailoutbox is mail.outbox ) # check that mail.outbox and fixture value is same object @@ -643,18 +723,18 @@ def test_mail(mailoutbox): assert list(m.to) == ["to@example.com"] -def test_mail_again(mailoutbox): +def test_mail_again(mailoutbox) -> None: test_mail(mailoutbox) -def test_mail_message_uses_mocked_DNS_NAME(mailoutbox): +def test_mail_message_uses_mocked_DNS_NAME(mailoutbox) -> None: mail.send_mail("subject", "body", "from@example.com", ["to@example.com"]) m = mailoutbox[0] message = m.message() assert message["Message-ID"].endswith("@fake-tests.example.com>") -def test_mail_message_uses_django_mail_dnsname_fixture(django_testdir): +def test_mail_message_uses_django_mail_dnsname_fixture(django_testdir) -> None: django_testdir.create_test_module( """ from django.core import mail @@ -677,7 +757,7 @@ def test_mailbox_inner(mailoutbox): assert result.ret == 0 -def test_mail_message_dns_patching_can_be_skipped(django_testdir): +def test_mail_message_dns_patching_can_be_skipped(django_testdir) -> None: django_testdir.create_test_module( """ from django.core import mail diff --git a/tests/test_initialization.py b/tests/test_initialization.py index 33544bd26..d8da80147 100644 --- a/tests/test_initialization.py +++ b/tests/test_initialization.py @@ -1,7 +1,7 @@ from textwrap import dedent -def test_django_setup_order_and_uniqueness(django_testdir, monkeypatch): +def test_django_setup_order_and_uniqueness(django_testdir, monkeypatch) -> None: """ The django.setup() function shall not be called multiple times by pytest-django, since it resets logging conf each time. @@ -54,7 +54,7 @@ def test_ds(): "conftest", "pytest_configure: conftest", "pytest_configure: plugin", - "* 1 passed in*", + "* 1 passed*", ] ) assert result.ret == 0 diff --git a/tests/test_manage_py_scan.py b/tests/test_manage_py_scan.py index 8a0f9aad3..490882b05 100644 --- a/tests/test_manage_py_scan.py +++ b/tests/test_manage_py_scan.py @@ -2,11 +2,11 @@ @pytest.mark.django_project(project_root="django_project_root", create_manage_py=True) -def test_django_project_found(django_testdir): +def test_django_project_found(django_testdir) -> None: # XXX: Important: Do not chdir() to django_project_root since runpytest_subprocess - # will call "python /path/to/pytest.py", which will impliclity add cwd to + # will call "python /path/to/pytest.py", which will implicitly add cwd to # the path. By instead calling "python /path/to/pytest.py - # django_project_root", we avoid impliclity adding the project to sys.path + # django_project_root", we avoid implicitly adding the project to sys.path # This matches the behaviour when pytest is called directly as an # executable (cwd is not added to the Python path) @@ -25,7 +25,7 @@ def test_foobar(): @pytest.mark.django_project(project_root="django_project_root", create_manage_py=True) -def test_django_project_found_with_k(django_testdir, monkeypatch): +def test_django_project_found_with_k(django_testdir, monkeypatch) -> None: """Test that cwd is checked as fallback with non-args via '-k foo'.""" testfile = django_testdir.create_test_module( """ @@ -44,7 +44,7 @@ def test_foobar(): @pytest.mark.django_project(project_root="django_project_root", create_manage_py=True) -def test_django_project_found_with_k_and_cwd(django_testdir, monkeypatch): +def test_django_project_found_with_k_and_cwd(django_testdir, monkeypatch) -> None: """Cover cwd not used as fallback if present already in args.""" testfile = django_testdir.create_test_module( """ @@ -63,7 +63,7 @@ def test_foobar(): @pytest.mark.django_project(project_root="django_project_root", create_manage_py=True) -def test_django_project_found_absolute(django_testdir, monkeypatch): +def test_django_project_found_absolute(django_testdir, monkeypatch) -> None: """This only tests that "." is added as an absolute path (#637).""" django_testdir.create_test_module( """ @@ -82,7 +82,7 @@ def test_dot_not_in_syspath(): @pytest.mark.django_project(project_root="django_project_root", create_manage_py=True) -def test_django_project_found_invalid_settings(django_testdir, monkeypatch): +def test_django_project_found_invalid_settings(django_testdir, monkeypatch) -> None: monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST") result = django_testdir.runpytest_subprocess("django_project_root") @@ -92,7 +92,7 @@ def test_django_project_found_invalid_settings(django_testdir, monkeypatch): result.stderr.fnmatch_lines(["*pytest-django found a Django project*"]) -def test_django_project_scan_disabled_invalid_settings(django_testdir, monkeypatch): +def test_django_project_scan_disabled_invalid_settings(django_testdir, monkeypatch) -> None: monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST") django_testdir.makeini( @@ -112,13 +112,18 @@ def test_django_project_scan_disabled_invalid_settings(django_testdir, monkeypat @pytest.mark.django_project(project_root="django_project_root", create_manage_py=True) -def test_django_project_found_invalid_settings_version(django_testdir, monkeypatch): +def test_django_project_found_invalid_settings_version(django_testdir, monkeypatch) -> None: """Invalid DSM should not cause an error with --help or --version.""" monkeypatch.setenv("DJANGO_SETTINGS_MODULE", "DOES_NOT_EXIST") - result = django_testdir.runpytest_subprocess("django_project_root", "--version") + result = django_testdir.runpytest_subprocess("django_project_root", "--version", "--version") assert result.ret == 0 - result.stderr.fnmatch_lines(["*This is pytest version*"]) + if hasattr(pytest, "version_tuple") and pytest.version_tuple >= (7, 0): + version_out = result.stdout + else: + version_out = result.stderr + + version_out.fnmatch_lines(["*This is pytest version*"]) result = django_testdir.runpytest_subprocess("django_project_root", "--help") assert result.ret == 0 @@ -126,7 +131,7 @@ def test_django_project_found_invalid_settings_version(django_testdir, monkeypat @pytest.mark.django_project(project_root="django_project_root", create_manage_py=True) -def test_runs_without_error_on_long_args(django_testdir): +def test_runs_without_error_on_long_args(django_testdir) -> None: django_testdir.create_test_module( """ def test_this_is_a_long_message_which_caused_a_bug_when_scanning_for_manage_py_12346712341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234123412341234112341234112451234123412341234123412341234123412341234123412341234123412341234123412341234123412341234(): diff --git a/tests/test_unittest.py b/tests/test_unittest.py index 985e868ed..665a5f1e4 100644 --- a/tests/test_unittest.py +++ b/tests/test_unittest.py @@ -1,6 +1,5 @@ import pytest from django.test import TestCase -from pkg_resources import parse_version from pytest_django_test.app.models import Item @@ -8,32 +7,32 @@ class TestFixtures(TestCase): fixtures = ["items"] - def test_fixtures(self): + def test_fixtures(self) -> None: assert Item.objects.count() == 1 assert Item.objects.get().name == "Fixture item" - def test_fixtures_again(self): + def test_fixtures_again(self) -> None: """Ensure fixtures are only loaded once.""" self.test_fixtures() class TestSetup(TestCase): - def setUp(self): + def setUp(self) -> None: """setUp should be called after starting a transaction""" assert Item.objects.count() == 0 Item.objects.create(name="Some item") Item.objects.create(name="Some item again") - def test_count(self): + def test_count(self) -> None: self.assertEqual(Item.objects.count(), 2) assert Item.objects.count() == 2 Item.objects.create(name="Foo") self.assertEqual(Item.objects.count(), 3) - def test_count_again(self): + def test_count_again(self) -> None: self.test_count() - def tearDown(self): + def tearDown(self) -> None: """tearDown should be called before rolling back the database""" assert Item.objects.count() == 3 @@ -41,27 +40,28 @@ def tearDown(self): class TestFixturesWithSetup(TestCase): fixtures = ["items"] - def setUp(self): + def setUp(self) -> None: assert Item.objects.count() == 1 Item.objects.create(name="Some item") - def test_count(self): + def test_count(self) -> None: assert Item.objects.count() == 2 Item.objects.create(name="Some item again") - def test_count_again(self): + def test_count_again(self) -> None: self.test_count() - def tearDown(self): + def tearDown(self) -> None: assert Item.objects.count() == 3 -def test_sole_test(django_testdir): +def test_sole_test(django_testdir) -> None: """ - Make sure the database are configured when only Django TestCase classes + Make sure the database is configured when only Django TestCase classes are collected, without the django_db marker. - """ + Also ensures that the DB is available after a failure (#824). + """ django_testdir.create_test_module( """ import os @@ -80,18 +80,33 @@ def test_foo(self): # Make sure it is usable assert Item.objects.count() == 0 + + assert 0, "trigger_error" + + class TestBar(TestCase): + def test_bar(self): + assert Item.objects.count() == 0 """ ) result = django_testdir.runpytest_subprocess("-v") - result.stdout.fnmatch_lines(["*TestFoo*test_foo PASSED*"]) - assert result.ret == 0 + result.stdout.fnmatch_lines( + [ + "*::test_foo FAILED", + "*::test_bar PASSED", + '> assert 0, "trigger_error"', + "E AssertionError: trigger_error", + "E assert 0", + "*= 1 failed, 1 passed*", + ] + ) + assert result.ret == 1 class TestUnittestMethods: "Test that setup/teardown methods of unittests are being called." - def test_django(self, django_testdir): + def test_django(self, django_testdir) -> None: django_testdir.create_test_module( """ from django.test import TestCase @@ -128,7 +143,7 @@ def test_pass(self): ) assert result.ret == 0 - def test_setUpClass_not_being_a_classmethod(self, django_testdir): + def test_setUpClass_not_being_a_classmethod(self, django_testdir) -> None: django_testdir.create_test_module( """ from django.test import TestCase @@ -145,20 +160,12 @@ def test_pass(self): result = django_testdir.runpytest_subprocess("-v", "-s") expected_lines = [ "* ERROR at setup of TestFoo.test_pass *", + "E * TypeError: *", ] - if parse_version(pytest.__version__) < parse_version("4.2"): - expected_lines += [ - "E *Failed: .setUpClass should be a classmethod", # noqa:E501 - ] - else: - expected_lines += [ - "E * TypeError: *", - ] - result.stdout.fnmatch_lines(expected_lines) assert result.ret == 1 - def test_setUpClass_multiple_subclasses(self, django_testdir): + def test_setUpClass_multiple_subclasses(self, django_testdir) -> None: django_testdir.create_test_module( """ from django.test import TestCase @@ -196,12 +203,12 @@ def test_bar21(self): ) assert result.ret == 0 - def test_setUpClass_mixin(self, django_testdir): + def test_setUpClass_mixin(self, django_testdir) -> None: django_testdir.create_test_module( """ from django.test import TestCase - class TheMixin(object): + class TheMixin: @classmethod def setUpClass(cls): super(TheMixin, cls).setUpClass() @@ -224,7 +231,7 @@ def test_bar(self): ) assert result.ret == 0 - def test_setUpClass_skip(self, django_testdir): + def test_setUpClass_skip(self, django_testdir) -> None: django_testdir.create_test_module( """ from django.test import TestCase @@ -265,7 +272,7 @@ def test_bar21(self): ) assert result.ret == 0 - def test_multi_inheritance_setUpClass(self, django_testdir): + def test_multi_inheritance_setUpClass(self, django_testdir) -> None: django_testdir.create_test_module( """ from django.test import TestCase @@ -273,7 +280,7 @@ def test_multi_inheritance_setUpClass(self, django_testdir): # Using a mixin is a regression test, see #280 for more details: # https://github.com/pytest-dev/pytest-django/issues/280 - class SomeMixin(object): + class SomeMixin: pass class TestA(SomeMixin, TestCase): @@ -331,7 +338,7 @@ def test_c(self): assert result.parseoutcomes()["passed"] == 6 assert result.ret == 0 - def test_unittest(self, django_testdir): + def test_unittest(self, django_testdir) -> None: django_testdir.create_test_module( """ from unittest import TestCase @@ -368,7 +375,7 @@ def test_pass(self): ) assert result.ret == 0 - def test_setUpClass_leaf_but_not_in_dunder_dict(self, django_testdir): + def test_setUpClass_leaf_but_not_in_dunder_dict(self, django_testdir) -> None: django_testdir.create_test_module( """ from django.test import testcases @@ -392,7 +399,7 @@ def test_noop(self): result = django_testdir.runpytest_subprocess("-q", "-s") result.stdout.fnmatch_lines( - ["*FooBarTestCase.setUpClass*", "*test_noop*", "1 passed in*"] + ["*FooBarTestCase.setUpClass*", "*test_noop*", "1 passed*"] ) assert result.ret == 0 @@ -400,7 +407,7 @@ def test_noop(self): class TestCaseWithDbFixture(TestCase): pytestmark = pytest.mark.usefixtures("db") - def test_simple(self): + def test_simple(self) -> None: # We only want to check setup/teardown does not conflict assert 1 @@ -408,12 +415,12 @@ def test_simple(self): class TestCaseWithTrDbFixture(TestCase): pytestmark = pytest.mark.usefixtures("transactional_db") - def test_simple(self): + def test_simple(self) -> None: # We only want to check setup/teardown does not conflict assert 1 -def test_pdb_enabled(django_testdir): +def test_pdb_enabled(django_testdir) -> None: """ Make sure the database is flushed and tests are isolated when using the --pdb option. @@ -458,7 +465,7 @@ def tearDown(self): assert result.ret == 0 -def test_debug_not_used(django_testdir): +def test_debug_not_used(django_testdir) -> None: django_testdir.create_test_module( """ from django.test import TestCase @@ -477,5 +484,5 @@ def test_method(self): ) result = django_testdir.runpytest_subprocess("--pdb") - result.stdout.fnmatch_lines(["*= 1 passed in *"]) + result.stdout.fnmatch_lines(["*= 1 passed*"]) assert result.ret == 0 diff --git a/tests/test_urls.py b/tests/test_urls.py index 9001fddce..31cc0f6a2 100644 --- a/tests/test_urls.py +++ b/tests/test_urls.py @@ -1,42 +1,36 @@ import pytest from django.conf import settings +from django.urls import is_valid_path from django.utils.encoding import force_str @pytest.mark.urls("pytest_django_test.urls_overridden") -def test_urls(): - try: - from django.urls import is_valid_path - except ImportError: - from django.core.urlresolvers import is_valid_path +def test_urls() -> None: assert settings.ROOT_URLCONF == "pytest_django_test.urls_overridden" assert is_valid_path("/overridden_url/") @pytest.mark.urls("pytest_django_test.urls_overridden") -def test_urls_client(client): +def test_urls_client(client) -> None: response = client.get("/overridden_url/") assert force_str(response.content) == "Overridden urlconf works!" -def test_urls_cache_is_cleared(testdir): +def test_urls_cache_is_cleared(testdir) -> None: testdir.makepyfile( myurls=""" - from django.conf.urls import url + from django.urls import path def fake_view(request): pass - urlpatterns = [url(r'first/$', fake_view, name='first')] + urlpatterns = [path('first', fake_view, name='first')] """ ) testdir.makepyfile( """ - try: - from django.urls import reverse, NoReverseMatch - except ImportError: # Django < 2.0 - from django.core.urlresolvers import reverse, NoReverseMatch + from django.urls import reverse, NoReverseMatch import pytest @pytest.mark.urls('myurls') @@ -55,35 +49,32 @@ def test_something_else(): assert result.ret == 0 -def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(testdir): +def test_urls_cache_is_cleared_and_new_urls_can_be_assigned(testdir) -> None: testdir.makepyfile( myurls=""" - from django.conf.urls import url + from django.urls import path def fake_view(request): pass - urlpatterns = [url(r'first/$', fake_view, name='first')] + urlpatterns = [path('first', fake_view, name='first')] """ ) testdir.makepyfile( myurls2=""" - from django.conf.urls import url + from django.urls import path def fake_view(request): pass - urlpatterns = [url(r'second/$', fake_view, name='second')] + urlpatterns = [path('second', fake_view, name='second')] """ ) testdir.makepyfile( """ - try: - from django.urls import reverse, NoReverseMatch - except ImportError: # Django < 2.0 - from django.core.urlresolvers import reverse, NoReverseMatch + from django.urls import reverse, NoReverseMatch import pytest @pytest.mark.urls('myurls') diff --git a/tests/test_without_django_loaded.py b/tests/test_without_django_loaded.py index eb6409947..1a7333daa 100644 --- a/tests/test_without_django_loaded.py +++ b/tests/test_without_django_loaded.py @@ -2,7 +2,7 @@ @pytest.fixture -def no_ds(monkeypatch): +def no_ds(monkeypatch) -> None: """Ensure DJANGO_SETTINGS_MODULE is unset""" monkeypatch.delenv("DJANGO_SETTINGS_MODULE") @@ -10,7 +10,7 @@ def no_ds(monkeypatch): pytestmark = pytest.mark.usefixtures("no_ds") -def test_no_ds(testdir): +def test_no_ds(testdir) -> None: testdir.makepyfile( """ import os @@ -26,7 +26,7 @@ def test_cfg(pytestconfig): assert r.ret == 0 -def test_database(testdir): +def test_database(testdir) -> None: testdir.makepyfile( """ import pytest @@ -51,7 +51,7 @@ def test_transactional_db(transactional_db): r.stdout.fnmatch_lines(["*4 skipped*"]) -def test_client(testdir): +def test_client(testdir) -> None: testdir.makepyfile( """ def test_client(client): @@ -66,7 +66,7 @@ def test_admin_client(admin_client): r.stdout.fnmatch_lines(["*2 skipped*"]) -def test_rf(testdir): +def test_rf(testdir) -> None: testdir.makepyfile( """ def test_rf(rf): @@ -78,7 +78,7 @@ def test_rf(rf): r.stdout.fnmatch_lines(["*1 skipped*"]) -def test_settings(testdir): +def test_settings(testdir) -> None: testdir.makepyfile( """ def test_settings(settings): @@ -90,7 +90,7 @@ def test_settings(settings): r.stdout.fnmatch_lines(["*1 skipped*"]) -def test_live_server(testdir): +def test_live_server(testdir) -> None: testdir.makepyfile( """ def test_live_server(live_server): @@ -102,7 +102,7 @@ def test_live_server(live_server): r.stdout.fnmatch_lines(["*1 skipped*"]) -def test_urls_mark(testdir): +def test_urls_mark(testdir) -> None: testdir.makepyfile( """ import pytest diff --git a/tox.ini b/tox.ini index 84bbb9859..923556471 100644 --- a/tox.ini +++ b/tox.ini @@ -1,34 +1,29 @@ [tox] envlist = - py37-dj{30,22,21,20,111}-postgres - py36-dj{30,22,21,20,111,110,19,18}-postgres - py35-dj{22,21,20,111,110,19,18}-postgres - py34-dj{20,111,110}-postgres - py27-dj{111,110}-{mysql_innodb,mysql_myisam,postgres} - py27-dj{111,110,19,18}-postgres - checkqa + py310-dj{main,40,32}-postgres + py39-dj{main,40,32,22}-postgres + py38-dj{main,40,32,22}-postgres + py37-dj{32,22}-postgres + py36-dj{32,22}-postgres + py35-dj{22}-postgres + linting [testenv] extras = testing deps = - djmaster: https://github.com/django/django/archive/master.tar.gz - dj30: Django>=3.0a1,<3.1 - dj22: Django>=2.2a1,<2.3 - dj21: Django>=2.1,<2.2 - dj20: Django>=2.0,<2.1 - dj111: Django>=1.11,<1.12 - dj110: Django>=1.10,<1.11 - dj19: Django>=1.9,<1.10 - dj18: Django>=1.8,<1.9 + djmain: https://github.com/django/django/archive/main.tar.gz + dj40: Django>=4.0,<4.1 + dj32: Django>=3.2,<4.0 + dj22: Django>=2.2,<2.3 - mysql_myisam: mysqlclient==1.4.2.post1 - mysql_innodb: mysqlclient==1.4.2.post1 + mysql_myisam: mysqlclient==2.1.0 + mysql_innodb: mysqlclient==2.1.0 - postgres: psycopg2-binary + !pypy3-postgres: psycopg2-binary + pypy3-postgres: psycopg2cffi coverage: coverage-enable-subprocess - pytest41: pytest>=4.1,<4.2 - pytest41: attrs==17.4.0 + pytest54: pytest>=5.4,<5.5 xdist: pytest-xdist>=1.15 setenv = @@ -45,7 +40,7 @@ setenv = coverage: COVERAGE_FILE={toxinidir}/.coverage coverage: PYTESTDJANGO_COVERAGE_SRC={toxinidir}/ -passenv = PYTEST_ADDOPTS TERM +passenv = PYTEST_ADDOPTS TERM TEST_DB_USER TEST_DB_PASSWORD TEST_DB_HOST usedevelop = True commands = coverage: coverage erase @@ -54,15 +49,21 @@ commands = coverage: coverage report coverage: coverage xml -[testenv:checkqa] +[testenv:linting] +extras = deps = flake8 + mypy==0.940 + isort commands = flake8 --version flake8 --statistics {posargs:pytest_django pytest_django_test tests} + mypy {posargs:pytest_django pytest_django_test tests} + isort --check-only --diff pytest_django pytest_django_test tests [testenv:doc8] -basepython = python3.6 +extras = +basepython = python3.8 skip_install = true deps = sphinx @@ -76,7 +77,8 @@ extras = docs commands = sphinx-build -n -W -b html -d docs/_build/doctrees docs docs/_build/html [testenv:readme] -basepython = python3.5 +extras = +basepython = python3.8 deps = readme_renderer commands =