Skip to content

Commit 7e1bf50

Browse files
bittnerpelme
authored andcommitted
Add test discovery config instruction to tutorial chapter (docs) (#446)
Addresses issue #67 Use code-block directive for code blocks
1 parent 862066b commit 7e1bf50

File tree

3 files changed

+103
-46
lines changed

3 files changed

+103
-46
lines changed

docs/faq.rst

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,44 @@ How can I make sure that all my tests run with a specific locale?
1818

1919
Create a `pytest fixture <http://pytest.org/latest/fixture.html>`_ that is
2020
automatically run before each test case. To run all tests with the english
21-
locale, put the following code in your project's `conftest.py
22-
<http://pytest.org/latest/plugins.html>`_ file::
21+
locale, put the following code in your project's `conftest.py`_ file:
22+
23+
.. code-block:: python
2324
2425
from django.utils.translation import activate
2526
2627
@pytest.fixture(autouse=True)
2728
def set_default_language():
2829
activate('en')
2930
31+
.. _conftest.py: http://docs.pytest.org/en/latest/plugins.html
32+
3033
.. _faq-tests-not-being-picked-up:
3134

32-
My tests are not being found. Why not?
33-
-------------------------------------------------------------------------------------
34-
By default, pytest looks for tests in files named ``test_*.py`` (note that
35-
this is not the same as ``test*.py``). If you have your tests in files with
36-
other names, they will not be collected. It is common to put tests under
37-
``app_directory/tests/views.py``. To find those tests, create a ``pytest.ini``
38-
file in your project root with the contents::
35+
My tests are not being found. Why?
36+
----------------------------------
37+
38+
By default, pytest looks for tests in files named ``test_*.py`` (note that
39+
this is not the same as ``test*.py``) and ``*_test.py``. If you have your
40+
tests in files with other names, they will not be collected. Note that
41+
Django's ``startapp`` manage command creates an ``app_dir/tests.py`` file.
42+
Also, it is common to put tests under ``app_dir/tests/views.py``, etc.
43+
44+
To find those tests, create a ``pytest.ini`` file in your project root and add
45+
an appropriate ``python_files`` line to it:
46+
47+
.. code-block:: ini
3948
4049
[pytest]
41-
python_files=*.py
50+
python_files = tests.py test_*.py *_tests.py
51+
52+
See the `related pytest docs`_ for more details.
4253

43-
When debugging test collection problems, the ``--collectonly`` flag and ``-rs``
44-
(report skipped tests) can be helpful.
54+
When debugging test collection problems, the ``--collectonly`` flag and
55+
``-rs`` (report skipped tests) can be helpful.
56+
57+
.. _related pytest docs:
58+
http://docs.pytest.org/en/latest/example/pythoncollection.html#changing-naming-conventions
4559

4660
Does pytest-django work with the pytest-xdist plugin?
4761
-----------------------------------------------------
@@ -58,7 +72,9 @@ How can I use ``manage.py test`` with pytest-django?
5872

5973
pytest-django is designed to work with the ``pytest`` command, but if you
6074
really need integration with ``manage.py test``, you can create a simple
61-
test runner like this::
75+
test runner like this:
76+
77+
.. code-block:: python
6278
6379
class PytestTestRunner(object):
6480
"""Runs pytest to discover and run tests."""
@@ -90,11 +106,15 @@ test runner like this::
90106
argv.extend(test_labels)
91107
return pytest.main(argv)
92108
93-
Add the path to this class in your Django settings::
109+
Add the path to this class in your Django settings:
110+
111+
.. code-block:: python
94112
95113
TEST_RUNNER = 'my_project.runner.PytestTestRunner'
96114
97-
Usage::
115+
Usage:
116+
117+
.. code-block:: bash
98118
99119
./manage.py test <django args> -- <pytest args>
100120
@@ -106,7 +126,9 @@ variables or the ``--settings`` command line option.
106126
How can I give database access to all my tests without the `django_db` marker?
107127
------------------------------------------------------------------------------
108128

109-
Create an autouse fixture and put it in `conftest.py` in your project root::
129+
Create an autouse fixture and put it in ``conftest.py`` in your project root:
130+
131+
.. code-block:: python
110132
111133
@pytest.fixture(autouse=True)
112134
def enable_db_access_for_all_tests(db):
@@ -115,11 +137,14 @@ Create an autouse fixture and put it in `conftest.py` in your project root::
115137
How/where can I get help with pytest/pytest-django?
116138
---------------------------------------------------
117139

118-
Usage questions can be asked on StackOverflow with the `pytest tag
119-
<http://stackoverflow.com/search?q=pytest>`_.
140+
Usage questions can be asked on StackOverflow with the `pytest tag`_.
120141

121142
If you think you've found a bug or something that is wrong in the
122-
documentation, feel free to `open an issue on the Github project for
123-
pytest-django <https://github.com/pytest-dev/pytest-django/issues/>`_.
143+
documentation, feel free to `open an issue on the GitHub project`_ for
144+
pytest-django.
124145

125146
Direct help can be found in the #pylib IRC channel on irc.freenode.org.
147+
148+
.. _pytest tag: http://stackoverflow.com/search?q=pytest
149+
.. _open an issue on the GitHub project:
150+
https://github.com/pytest-dev/pytest-django/issues/

docs/index.rst

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
Welcome to pytest-django's documentation!
2-
=========================================
1+
===========================
2+
pytest-django Documentation
3+
===========================
34

4-
pytest-django is a plugin for `pytest <http://pytest.org/>`_ that provides a set of useful tools for testing `Django <http://www.djangoproject.com/>`_ applications and projects.
5+
pytest-django is a plugin for `pytest`_ that provides a set of useful tools
6+
for testing `Django`_ applications and projects.
7+
8+
.. _pytest: http://pytest.org/
9+
.. _Django: https://www.djangoproject.com/
10+
11+
Quick Start
12+
===========
13+
14+
1. ``pip install pytest-django``
15+
2. Make sure ``DJANGO_SETTINGS_MODULE`` is defined and and run tests with the ``pytest`` command.
16+
3. (Optional) If you want tests of Django's default application layout be discovered (``tests.py``),
17+
if you put your tests under a ``tests/`` directory , or your files are not named ``test_FOO.py``,
18+
see the FAQ at :ref:`faq-tests-not-being-picked-up`.
19+
20+
Table of Contents
21+
=================
522

623
.. toctree::
724
:maxdepth: 3
@@ -21,25 +38,24 @@ Why would I use this instead of Django's manage.py test command?
2138

2239
Running the test suite with pytest offers some features that are not present in Django's standard test mechanism:
2340

24-
* Less boilerplate: no need to import unittest, create a subclass with methods. Just write tests as regular functions.
25-
* `Manage test dependencies with fixtures <http://pytest.org/latest/fixture.html>`_
26-
* Database re-use: no need to re-create the test database for every test run.
27-
* Run tests in multiple processes for increased speed
28-
* There are a lot of other nice plugins available for pytest.
29-
* Easy switching: Existing unittest-style tests will still work without any modifications.
41+
* Less boilerplate: no need to import unittest, create a subclass with methods. Just write tests as regular functions.
42+
* `Manage test dependencies with fixtures`_.
43+
* Database re-use: no need to re-create the test database for every test run.
44+
* Run tests in multiple processes for increased speed.
45+
* There are a lot of other nice plugins available for pytest.
46+
* Easy switching: Existing unittest-style tests will still work without any modifications.
3047

31-
See the `pytest documentation <http://pytest.org/latest/>`_ for more information on pytest.
32-
33-
Quick Start
34-
===========
35-
1. ``pip install pytest-django``
36-
2. Make sure ``DJANGO_SETTINGS_MODULE`` is defined and and run tests with the ``pytest`` command.
37-
3. (Optionally) If you put your tests under a tests directory (the standard Django application layout), and your files are not named ``test_FOO.py``, see the FAQ :ref:`faq-tests-not-being-picked-up`.
48+
See the `pytest documentation`_ for more information on pytest.
3849

50+
.. _Manage test dependencies with fixtures: http://docs.pytest.org/en/latest/fixture.html
51+
.. _pytest documentation: http://docs.pytest.org/
3952

4053
Bugs? Feature suggestions?
41-
============================
42-
Report issues and feature requests at the `github issue tracker <http://github.com/pytest-dev/pytest-django/issues>`_.
54+
==========================
55+
56+
Report issues and feature requests at the `GitHub issue tracker`_.
57+
58+
.. _GitHub issue tracker: http://github.com/pytest-dev/pytest-django/issues
4359

4460
Indices and tables
4561
==================

docs/tutorial.rst

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Step 1: Installation
2929

3030
pytest-django can be obtained directly from `PyPI
3131
<http://pypi.python.org/pypi/pytest-django>`_, and can be installed with
32-
``pip``::
32+
``pip``:
33+
34+
.. code-block:: bash
3335
3436
pip install pytest-django
3537
@@ -41,23 +43,37 @@ Step 2: Point pytest to your Django settings
4143
--------------------------------------------
4244

4345
You need to tell pytest which Django settings that should be used for test
44-
runs. The easiest way to achieve this is to create a pytest configuration file with this information.
46+
runs. The easiest way to achieve this is to create a pytest configuration file
47+
with this information.
48+
49+
Create a file called ``pytest.ini`` in your project root directory that
50+
contains:
4551

46-
Create a file called ``pytest.ini`` in your project root directory that contains::
52+
.. code-block:: ini
4753
4854
[pytest]
49-
DJANGO_SETTINGS_MODULE=yourproject.settings
55+
DJANGO_SETTINGS_MODULE = yourproject.settings
5056
5157
You can also specify your Django settings by setting the
5258
``DJANGO_SETTINGS_MODULE`` environment variable or specifying the
53-
``--ds=yourproject.settings`` command line flag when running the tests. See the
54-
full documentation on :ref:`configuring_django_settings`.
59+
``--ds=yourproject.settings`` command line flag when running the tests.
60+
See the full documentation on :ref:`configuring_django_settings`.
61+
62+
Optionally, also add the following line to the ``[pytest]`` section to
63+
instruct pytest to collect tests in Django's default app layouts, too.
64+
See the FAQ at :ref:`faq-tests-not-being-picked-up` for more infos.
65+
66+
.. code-block:: ini
67+
68+
python_files = tests.py test_*.py *_tests.py
5569
5670
Step 3: Run your test suite
5771
---------------------------
5872

5973
Tests are invoked directly with the ``pytest`` command, instead of ``manage.py
60-
test``, that you might be used to::
74+
test``, that you might be used to:
75+
76+
.. code-block:: bash
6177
6278
pytest
6379
@@ -73,7 +89,7 @@ pytest-django also provides some :ref:`helpers` to make it easier to write
7389
Django tests.
7490

7591
Consult the `pytest documentation <http://pytest.org/>`_ for more information
76-
in pytest itself.
92+
on pytest itself.
7793

7894
Stuck? Need help?
7995
-----------------

0 commit comments

Comments
 (0)