faq.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. FAQ
  2. ===
  3. .. _faq-import-error:
  4. I see an error saying "could not import myproject.settings"
  5. -----------------------------------------------------------
  6. pytest-django tries to automatically add your project to the Python path by
  7. looking for a ``manage.py`` file and adding its path to the Python path.
  8. If this for some reason fails for you, you have to manage your Python paths
  9. explicitly. See the documentation on :ref:`managing_the_python_path_explicitly`
  10. for more information.
  11. How can I make sure that all my tests run with a specific locale?
  12. -----------------------------------------------------------------
  13. Create a `pytest fixture <https://pytest.org/en/latest/fixture.html>`_ that is
  14. automatically run before each test case. To run all tests with the english
  15. locale, put the following code in your project's `conftest.py`_ file:
  16. .. code-block:: python
  17. from django.utils.translation import activate
  18. @pytest.fixture(autouse=True)
  19. def set_default_language():
  20. activate('en')
  21. .. _conftest.py: http://docs.pytest.org/en/latest/plugins.html
  22. .. _faq-tests-not-being-picked-up:
  23. My tests are not being found. Why?
  24. ----------------------------------
  25. By default, pytest looks for tests in files named ``test_*.py`` (note that
  26. this is not the same as ``test*.py``) and ``*_test.py``. If you have your
  27. tests in files with other names, they will not be collected. Note that
  28. Django's ``startapp`` manage command creates an ``app_dir/tests.py`` file.
  29. Also, it is common to put tests under ``app_dir/tests/views.py``, etc.
  30. To find those tests, create a ``pytest.ini`` file in your project root and add
  31. an appropriate ``python_files`` line to it:
  32. .. code-block:: ini
  33. [pytest]
  34. python_files = tests.py test_*.py *_tests.py
  35. See the `related pytest docs`_ for more details.
  36. When debugging test collection problems, the ``--collectonly`` flag and
  37. ``-rs`` (report skipped tests) can be helpful.
  38. .. _related pytest docs:
  39. http://docs.pytest.org/en/latest/example/pythoncollection.html#changing-naming-conventions
  40. Does pytest-django work with the pytest-xdist plugin?
  41. -----------------------------------------------------
  42. Yes. pytest-django supports running tests in parallel with pytest-xdist. Each
  43. process created by xdist gets its own separate database that is used for the
  44. tests. This ensures that each test can run independently, regardless of whether
  45. transactions are tested or not.
  46. .. _faq-getting-help:
  47. How can I use ``manage.py test`` with pytest-django?
  48. ----------------------------------------------------
  49. pytest-django is designed to work with the ``pytest`` command, but if you
  50. really need integration with ``manage.py test``, you can create a simple
  51. test runner like this:
  52. .. code-block:: python
  53. class PytestTestRunner(object):
  54. """Runs pytest to discover and run tests."""
  55. def __init__(self, verbosity=1, failfast=False, keepdb=False, **kwargs):
  56. self.verbosity = verbosity
  57. self.failfast = failfast
  58. self.keepdb = keepdb
  59. def run_tests(self, test_labels):
  60. """Run pytest and return the exitcode.
  61. It translates some of Django's test command option to pytest's.
  62. """
  63. import pytest
  64. argv = []
  65. if self.verbosity == 0:
  66. argv.append('--quiet')
  67. if self.verbosity == 2:
  68. argv.append('--verbose')
  69. if self.verbosity == 3:
  70. argv.append('-vv')
  71. if self.failfast:
  72. argv.append('--exitfirst')
  73. if self.keepdb:
  74. argv.append('--reuse-db')
  75. argv.extend(test_labels)
  76. return pytest.main(argv)
  77. Add the path to this class in your Django settings:
  78. .. code-block:: python
  79. TEST_RUNNER = 'my_project.runner.PytestTestRunner'
  80. Usage:
  81. .. code-block:: bash
  82. ./manage.py test <django args> -- <pytest args>
  83. **Note**: the pytest-django command line options ``--ds`` and ``--dc`` are not
  84. compatible with this approach, you need to use the standard Django methods of
  85. setting the ``DJANGO_SETTINGS_MODULE``/``DJANGO_CONFIGURATION`` environmental
  86. variables or the ``--settings`` command line option.
  87. How can I give database access to all my tests without the `django_db` marker?
  88. ------------------------------------------------------------------------------
  89. Create an autouse fixture and put it in ``conftest.py`` in your project root:
  90. .. code-block:: python
  91. @pytest.fixture(autouse=True)
  92. def enable_db_access_for_all_tests(db):
  93. pass
  94. How/where can I get help with pytest/pytest-django?
  95. ---------------------------------------------------
  96. Usage questions can be asked on StackOverflow with the `pytest tag`_.
  97. If you think you've found a bug or something that is wrong in the
  98. documentation, feel free to `open an issue on the GitHub project`_ for
  99. pytest-django.
  100. Direct help can be found in the #pylib IRC channel on irc.freenode.org.
  101. .. _pytest tag: http://stackoverflow.com/search?q=pytest
  102. .. _open an issue on the GitHub project:
  103. https://github.com/pytest-dev/pytest-django/issues/