2010-11-02 07:53:53 +08:00
Installation and Getting Started
2010-10-14 07:25:09 +08:00
===================================
2010-10-14 01:30:00 +08:00
2015-09-17 19:30:35 +08:00
**Pythons** : Python 2.6,2.7,3.3,3.4,3.5, Jython, PyPy-2.3
2011-07-09 18:02:22 +08:00
**Platforms** : Unix/Posix and Windows
**PyPI package name** : `pytest <http://pypi.python.org/pypi/pytest> `_
2010-10-14 01:30:00 +08:00
2014-05-14 15:14:40 +08:00
**dependencies** : `py <http://pypi.python.org/pypi/py> `_ ,
`colorama (Windows) <http://pypi.python.org/pypi/colorama> `_ ,
`argparse (py26) <http://pypi.python.org/pypi/argparse> `_ .
2016-07-27 07:20:22 +08:00
**documentation as PDF** : `download latest <https://media.readthedocs.org/pdf/pytest/latest/pytest.pdf> `_
2011-07-09 19:23:58 +08:00
2010-11-25 19:11:10 +08:00
.. _`getstarted`:
2012-10-12 20:52:36 +08:00
.. _installation:
2010-11-25 19:11:10 +08:00
2010-10-25 03:55:27 +08:00
Installation
----------------------------------------
2010-10-14 01:30:00 +08:00
2016-08-23 10:35:41 +08:00
Installation::
2010-10-14 07:25:09 +08:00
2016-08-23 10:35:41 +08:00
pip install -U pytest
2010-10-14 07:25:09 +08:00
2010-10-25 03:55:27 +08:00
To check your installation has installed the correct version::
2010-10-14 07:25:09 +08:00
2016-06-21 22:16:57 +08:00
$ pytest --version
2017-03-14 06:41:20 +08:00
This is pytest version 3.0.7, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py
2010-10-14 01:30:00 +08:00
2010-11-25 19:11:10 +08:00
.. _`simpletest`:
2010-11-02 07:53:53 +08:00
Our first test run
2010-10-25 03:55:27 +08:00
----------------------------------------------------------
2010-10-14 01:30:00 +08:00
2010-11-06 06:37:25 +08:00
Let's create a first test file with a simple test function::
2010-10-14 01:30:00 +08:00
# content of test_sample.py
def func(x):
return x + 1
2010-11-06 06:37:25 +08:00
2010-10-14 01:30:00 +08:00
def test_answer():
assert func(3) == 5
2010-11-06 06:37:25 +08:00
That's it. You can execute the test function now::
2010-10-14 01:30:00 +08:00
2016-06-21 22:16:57 +08:00
$ pytest
2015-06-07 05:30:49 +08:00
======= test session starts ========
2017-03-14 06:41:20 +08:00
platform linux -- Python 3.5.2, pytest-3.0.7, py-1.4.32, pluggy-0.4.0
rootdir: $REGENDOC_TMPDIR, inifile:
2012-10-07 19:06:17 +08:00
collected 1 items
2017-04-07 10:29:41 +08:00
2010-10-14 01:30:00 +08:00
test_sample.py F
2017-04-07 10:29:41 +08:00
2015-06-07 05:30:49 +08:00
======= FAILURES ========
_______ test_answer ________
2017-04-07 10:29:41 +08:00
2010-10-14 01:30:00 +08:00
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
2017-04-07 10:29:41 +08:00
2010-11-06 06:37:25 +08:00
test_sample.py:5: AssertionError
2015-06-07 05:30:49 +08:00
======= 1 failed in 0.12 seconds ========
2010-10-14 01:30:00 +08:00
2015-09-26 12:41:17 +08:00
We got a failure report because our little `` func(3) `` call did not return `` 5 `` .
2010-10-14 01:30:00 +08:00
2010-11-02 07:53:53 +08:00
.. note ::
2010-10-14 01:30:00 +08:00
2011-06-15 13:50:34 +08:00
You can simply use the `` assert `` statement for asserting test
expectations. pytest's :ref: `assert introspection` will intelligently
report intermediate values of the assert expression freeing
you from the need to learn the many names of `JUnit legacy methods`_ .
2010-11-02 07:53:53 +08:00
2011-07-07 03:47:33 +08:00
.. _`JUnit legacy methods`: http://docs.python.org/library/unittest.html#test-cases
2010-10-14 01:30:00 +08:00
.. _`assert statement`: http://docs.python.org/reference/simple_stmts.html#the-assert-statement
2015-09-26 12:41:17 +08:00
Running multiple tests
----------------------------------------------------------
`` pytest `` will run all files in the current directory and its subdirectories of the form test_*.py or \* _test.py. More generally, it follows :ref: `standard test discovery rules <test discovery>` .
2011-03-04 06:40:38 +08:00
Asserting that a certain exception is raised
2010-10-25 03:55:27 +08:00
--------------------------------------------------------------
2011-03-04 06:40:38 +08:00
If you want to assert that some code raises an exception you can
2010-10-25 03:55:27 +08:00
use the `` raises `` helper::
# content of test_sysexit.py
2010-11-18 05:12:16 +08:00
import pytest
2010-10-25 03:55:27 +08:00
def f():
raise SystemExit(1)
def test_mytest():
2010-11-18 05:12:16 +08:00
with pytest.raises(SystemExit):
2010-10-25 03:55:27 +08:00
f()
2010-11-02 07:53:53 +08:00
Running it with, this time in "quiet" reporting mode::
2016-06-21 22:16:57 +08:00
$ pytest -q test_sysexit.py
2010-11-02 07:53:53 +08:00
.
2015-06-07 05:30:49 +08:00
1 passed in 0.12 seconds
2010-11-02 07:53:53 +08:00
Grouping multiple tests in a class
--------------------------------------------------------------
2011-03-04 06:40:38 +08:00
Once you start to have more than a few tests it often makes sense
to group tests logically, in classes and modules. Let's write a class
containing two tests::
2010-11-02 07:53:53 +08:00
# content of test_class.py
2017-02-17 02:41:51 +08:00
class TestClass(object):
2010-11-02 07:53:53 +08:00
def test_one(self):
x = "this"
assert 'h' in x
2010-10-25 03:55:27 +08:00
2010-11-02 07:53:53 +08:00
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
2010-11-06 06:37:25 +08:00
The two tests are found because of the standard :ref: `test discovery` .
There is no need to subclass anything. We can simply
run the module by passing its filename::
2010-11-02 07:53:53 +08:00
2016-06-21 22:16:57 +08:00
$ pytest -q test_class.py
2010-11-02 07:53:53 +08:00
.F
2015-06-07 05:30:49 +08:00
======= FAILURES ========
_______ TestClass.test_two ________
2017-04-07 10:29:41 +08:00
2015-09-22 22:52:35 +08:00
self = <test_class.TestClass object at 0xdeadbeef>
2017-04-07 10:29:41 +08:00
2010-11-02 07:53:53 +08:00
def test_two(self):
x = "hello"
> assert hasattr(x, 'check')
2017-03-14 06:41:20 +08:00
E AssertionError: assert False
2016-08-02 02:46:34 +08:00
E + where False = hasattr('hello', 'check')
2017-04-07 10:29:41 +08:00
2010-11-02 07:53:53 +08:00
test_class.py:8: AssertionError
2015-06-07 05:30:49 +08:00
1 failed, 1 passed in 0.12 seconds
2010-10-14 07:25:09 +08:00
2010-11-06 06:37:25 +08:00
The first test passed, the second failed. Again we can easily see
the intermediate values used in the assertion, helping us to
understand the reason for the failure.
Going functional: requesting a unique temporary directory
--------------------------------------------------------------
For functional tests one often needs to create some files
2012-10-07 19:06:17 +08:00
and pass them to application objects. pytest provides
2014-01-18 19:31:33 +08:00
:ref: `builtinfixtures` which allow to request arbitrary
2012-10-07 19:06:17 +08:00
resources, for example a unique temporary directory::
2010-11-06 06:37:25 +08:00
# content of test_tmpdir.py
def test_needsfiles(tmpdir):
2014-09-02 18:20:16 +08:00
print (tmpdir)
2010-11-06 06:37:25 +08:00
assert 0
We list the name `` tmpdir `` in the test function signature and
2014-01-18 19:31:33 +08:00
`` pytest `` will lookup and call a fixture factory to create the resource
2010-11-06 06:37:25 +08:00
before performing the test function call. Let's just run it::
2016-06-21 22:16:57 +08:00
$ pytest -q test_tmpdir.py
2014-09-05 19:55:00 +08:00
F
2015-06-07 05:30:49 +08:00
======= FAILURES ========
_______ test_needsfiles ________
2017-04-07 10:29:41 +08:00
2015-09-22 20:02:11 +08:00
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
2017-04-07 10:29:41 +08:00
2014-09-05 19:55:00 +08:00
def test_needsfiles(tmpdir):
print (tmpdir)
> assert 0
E assert 0
2017-04-07 10:29:41 +08:00
2014-09-05 19:55:00 +08:00
test_tmpdir.py:3: AssertionError
2015-09-22 20:02:11 +08:00
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
2015-06-07 05:30:49 +08:00
1 failed in 0.12 seconds
2010-11-06 06:37:25 +08:00
Before the test runs, a unique-per-test-invocation temporary directory
was created. More info at :ref: `tmpdir handling` .
2012-10-07 19:06:17 +08:00
You can find out what kind of builtin :ref: `fixtures` exist by typing::
2010-11-06 06:37:25 +08:00
2016-06-21 22:16:57 +08:00
pytest --fixtures # shows builtin and custom fixtures
2010-11-06 06:37:25 +08:00
2011-09-06 17:43:42 +08:00
Where to go next
2010-10-14 07:25:09 +08:00
-------------------------------------
Here are a few suggestions where to go next:
* :ref: `cmdline` for command line invocation examples
2016-06-25 17:27:10 +08:00
* :ref: `good practices <goodpractices>` for virtualenv, test layout
2017-04-07 10:29:41 +08:00
* :ref: `existingtestsuite` for working with pre-existing tests
2012-10-19 17:12:13 +08:00
* :ref: `fixtures` for providing a functional baseline to your tests
2010-11-06 06:37:25 +08:00
* :ref: `plugins` managing and writing plugins
2010-11-02 07:53:53 +08:00
2010-10-22 16:09:26 +08:00
.. include :: links.inc