2010-10-25 03:55:27 +08:00
|
|
|
Getting Started
|
2010-10-14 07:25:09 +08:00
|
|
|
===================================
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2010-10-14 07:25:09 +08:00
|
|
|
.. _`easy_install`:
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2010-10-14 07:25:09 +08:00
|
|
|
**Compatibility**: Python 2.4-3.2, Jython, PyPy on Unix/Posix and Windows
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
Installation
|
|
|
|
----------------------------------------
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
Installation options::
|
2010-10-14 07:25:09 +08:00
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
easy_install -U pytest # or
|
|
|
|
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
|
|
|
|
|
|
|
$ py.test --version
|
|
|
|
|
|
|
|
If you get an error, checkout :ref:`installation issues`.
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
Writing a simple test function with an assertion
|
|
|
|
----------------------------------------------------------
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
Let's create a small file with a test function testing a function
|
|
|
|
computes a certain value::
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
# content of test_sample.py
|
|
|
|
def func(x):
|
|
|
|
return x + 1
|
|
|
|
def test_answer():
|
|
|
|
assert func(3) == 5
|
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
Now you can execute the test function::
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
$ py.test test_sample.py
|
2010-10-25 03:55:27 +08:00
|
|
|
========================= test session starts ==========================
|
|
|
|
platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev4
|
2010-10-14 01:30:00 +08:00
|
|
|
test path 1: test_sample.py
|
|
|
|
|
|
|
|
test_sample.py F
|
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
=============================== FAILURES ===============================
|
|
|
|
_____________________________ test_answer ______________________________
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
def test_answer():
|
|
|
|
> assert func(3) == 5
|
|
|
|
E assert 4 == 5
|
|
|
|
E + where 4 = func(3)
|
|
|
|
|
|
|
|
test_sample.py:4: AssertionError
|
2010-10-25 03:55:27 +08:00
|
|
|
======================= 1 failed in 0.02 seconds =======================
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
We got a failure because our little ``func(3)`` call did not return ``5``.
|
|
|
|
A few notes on this little test invocation:
|
|
|
|
|
|
|
|
* ``test_answer`` was identified as a test function because of the
|
|
|
|
``test_`` prefix,
|
|
|
|
|
|
|
|
* we conveniently used the standard `assert statement`_ and the failure
|
|
|
|
report shows us the intermediate values.
|
|
|
|
|
|
|
|
.. _`assert statement`: http://docs.python.org/reference/simple_stmts.html#the-assert-statement
|
|
|
|
|
2010-10-25 03:55:27 +08:00
|
|
|
Asserting that a certain exception is raised
|
|
|
|
--------------------------------------------------------------
|
|
|
|
|
|
|
|
If you want to assert a test raises a certain exception you can
|
|
|
|
use the ``raises`` helper::
|
|
|
|
|
|
|
|
# content of test_sysexit.py
|
|
|
|
import py
|
|
|
|
def f():
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
def test_mytest():
|
|
|
|
with py.test.raises(SystemExit):
|
|
|
|
f()
|
|
|
|
|
|
|
|
Running it with::
|
|
|
|
|
|
|
|
$ py.test test_sysexit.py
|
|
|
|
========================= test session starts ==========================
|
|
|
|
platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev4
|
|
|
|
test path 1: test_sysexit.py
|
|
|
|
|
|
|
|
test_sysexit.py .
|
|
|
|
|
|
|
|
======================= 1 passed in 0.01 seconds =======================
|
|
|
|
|
|
|
|
.. For further ways to assert exceptions see the :pyfunc:`raises`
|
2010-10-14 07:25:09 +08:00
|
|
|
|
|
|
|
where to go from here
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
Here are a few suggestions where to go next:
|
|
|
|
|
|
|
|
* :ref:`cmdline` for command line invocation examples
|
|
|
|
* :ref:`good practises` for virtualenv, test layout, genscript support
|
|
|
|
* :ref:`apiref` for documentation and examples on writing Python tests
|
|
|
|
* :ref:`examples` for more complex examples
|
|
|
|
|
2010-10-22 16:09:26 +08:00
|
|
|
.. include:: links.inc
|