File: tutorial.rst

package info (click to toggle)
pytest-django 2.9.1-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 428 kB
  • sloc: python: 1,009; makefile: 148
file content (82 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Getting started with pytest and pytest-django
=============================================

Introduction
------------

pytest and pytest-django are compatible with standard Django test suites and
Nose test suites. They should be able to pick up and run existing tests without
any or little configuration. This section describes how to get started quickly.

Talks, articles and blog posts
------------------------------

 * Talk from DjangoCon Europe 2014: `pytest: helps you write better Django apps, by Andreas Pelme <https://www.youtube.com/watch?v=aaArYVh6XSM>`_

 * Talk from EuroPython 2013: `Testing Django application with py.test, by Andreas Pelme <http://www.youtube.com/watch?v=aUf8Fkb7TaY>`_

 * Three part blog post tutorial (part 3 mentions Django integration): `pytest: no-boilerplate testing, by Daniel Greenfeld <http://pydanny.com/pytest-no-boilerplate-testing.html>`_

 * Blog post: `Django Projects to Django Apps: Converting the Unit Tests, by
   John Costa
   <http://www.johnmcostaiii.net/2013/django-projects-to-django-apps-converting-the-unit-tests/>`_.

For general information and tutorials on pytest, see the `pytest tutorial page <http://pytest.org/latest/getting-started.html>`_.


Step 1: Installation
--------------------

pytest-django can be obtained directly from `PyPI
<http://pypi.python.org/pypi/pytest-django>`_, and can be installed with
``pip``::

    pip install pytest-django

Installing pytest-django will also automatically install the latest version of
pytest. ``pytest-django`` uses ``pytest``'s plugin system and can be used right away
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
runs. The easiest way to achieve this is to create a pytest configuration file with this information.

Create a filed called ``pytest.ini`` in your project root directory that contains::

    [pytest]
    DJANGO_SETTINGS_MODULE=yourproject.settings

You can also specify your Django settings by setting the
``DJANGO_SETTINGS_MODULE`` environment variable or specifying the
``--ds=yourproject.settings`` command line flag when running the tests. See the
full documentation on :ref:`configuring_django_settings`.

Step 3: Run your test suite
---------------------------

Tests are invoked directly with the ``py.test`` command, instead of ``manage.py
test``, that you might be used to::

    py.test

Do you have problems with pytest not finding your code? See the FAQ
:ref:`faq-import-error`.

Next steps
----------

The :ref:`usage` section describes more ways to interact with your test suites.

pytest-django also provides some :ref:`helpers` to make it easier to write
Django tests.

Consult the `pytest documentation <http://pytest.org/>`_ for more information
in pytest itself.

Stuck? Need help?
-----------------

No problem, see the FAQ on :ref:`faq-getting-help` for information on how to
get help.