regen and extend examples a bit with regendoc.py

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-10-11 10:07:14 +02:00
parent aa70d9073c
commit 431a582132
4 changed files with 69 additions and 36 deletions

View File

@ -21,3 +21,34 @@ putting them into a conftest.py file like this::
option_doctestmodules = True option_doctestmodules = True
option_doctestglob = "*.rst" option_doctestglob = "*.rst"
If you then have a text file like this::
# content of example.rst
hello this is a doctest
>>> x = 3
>>> x
3
and another like this::
# content of mymodule.py
def something():
""" a doctest in a docstring
>>> something()
42
"""
return 42
then you can just invoke ``py.test`` without command line options::
$ py.test
============================= test session starts ==============================
platform linux2 -- Python 2.6.5 -- pytest-2.0.0dev0
test path 1: /tmp/doc-exec-109
conftest.py .
example.rst .
mymodule.py .
=========================== 3 passed in 0.01 seconds ===========================

View File

@ -31,15 +31,14 @@ into a test module::
Running the test looks like this:: Running the test looks like this::
$ py.test test_simplefactory.py $ py.test test_simplefactory.py
============================= test session starts ==============================
=========================== test session starts ============================ platform linux2 -- Python 2.6.5 -- pytest-2.0.0dev0
python: platform linux2 -- Python 2.6.2 test path 1: test_simplefactory.py
test object 1: /home/hpk/hg/py/trunk/example/funcarg/test_simplefactory.py
test_simplefactory.py F test_simplefactory.py F
================================ FAILURES ================================== =================================== FAILURES ===================================
______________________________ test_function _______________________________ ________________________________ test_function _________________________________
myfuncarg = 42 myfuncarg = 42
@ -47,9 +46,8 @@ Running the test looks like this::
> assert myfuncarg == 17 > assert myfuncarg == 17
E assert 42 == 17 E assert 42 == 17
test_simplefactory.py:6: AssertionError test_simplefactory.py:5: AssertionError
======================== 1 failed in 0.11 seconds ========================== =========================== 1 failed in 0.02 seconds ===========================
This means that the test function was called with a ``myfuncarg`` value This means that the test function was called with a ``myfuncarg`` value
of ``42`` and the assert fails accordingly. Here is how py.test of ``42`` and the assert fails accordingly. Here is how py.test
@ -143,14 +141,14 @@ Let's consider this test module::
Running this:: Running this::
$ py.test test_example.py $ py.test test_example.py
============================= test session starts ========================== ============================= test session starts ==============================
python: platform linux2 -- Python 2.6.2 platform linux2 -- Python 2.6.5 -- pytest-2.0.0dev0
test object 1: /home/hpk/hg/py/trunk/test_example.py test path 1: test_example.py
test_example.py .........F test_example.py .........F
================================ FAILURES ================================== =================================== FAILURES ===================================
__________________________ test_func.test_func[9] __________________________ _________________________________ test_func[9] _________________________________
numiter = 9 numiter = 9
@ -158,8 +156,8 @@ Running this::
> assert numiter < 9 > assert numiter < 9
E assert 9 < 9 E assert 9 < 9
/home/hpk/hg/py/trunk/test_example.py:10: AssertionError test_example.py:7: AssertionError
====================== 1 failed, 9 passed in 0.04 seconds ======================
Here is what happens in detail: Here is what happens in detail:

View File

@ -27,18 +27,22 @@ patch this function before calling into a function which uses it::
return os.path.join(os.expanduser("~admin"), '.ssh') return os.path.join(os.expanduser("~admin"), '.ssh')
def test_mytest(monkeypatch): def test_mytest(monkeypatch):
monkeypatch.setattr(os.path, 'expanduser', lambda x: '/tmp/xyz') def mockreturn(path):
return '/abc'
monkeypatch.setattr(os.path, 'expanduser', mockreturn)
x = getssh() x = getssh()
assert x == '/tmp/xyz/.ssh' assert x == '/abc'
After the test function finishes the ``os.path.expanduser`` modification After the test function finishes the ``os.path.expanduser`` modification
will be undone. will be undone.
Running the above example:: .. background check:
$ py.test
$ py.test ============================= test session starts ==============================
PASS platform linux2 -- Python 2.6.5 -- pytest-2.0.0dev0
test path 1: /tmp/doc-exec-117
=============================== in 0.00 seconds ===============================
Method reference of the monkeypatch function argument Method reference of the monkeypatch function argument
----------------------------------------------------- -----------------------------------------------------

View File

@ -5,9 +5,9 @@ creating JUnitXML format files
---------------------------------------------------- ----------------------------------------------------
To create result files which can be read by Hudson_ or other Continous To create result files which can be read by Hudson_ or other Continous
integration servers, use this:: integration servers, use this invocation::
$ py.test --junitxml=path py.test --junitxml=path
to create an XML file at ``path``. to create an XML file at ``path``.
@ -16,7 +16,7 @@ creating resultlog format files
To create plain-text machine-readable result files you can issue:: To create plain-text machine-readable result files you can issue::
$ py.test --resultlog=path py.test --resultlog=path
and look at the content at the ``path`` location. Such files are used e.g. and look at the content at the ``path`` location. Such files are used e.g.
by the `PyPy-test`_ web page to show test results over several revisions. by the `PyPy-test`_ web page to show test results over several revisions.