7. nose
for sanity’s sake
adam@rye:~/projects/pdxpython$ nosetests -v
Simple addition must be correct. ... ok
----------------------------------------------------------------------
Ran 1 test in 0.010 seconds
OK
(?:^|[b_.-])[Tt]est
8. py.test
worth considering
(pdxpython)adam@rye:~/projects/pdxpython$ py.test -v test-examples.py
============================= test session starts ==============================
python: platform darwin -- Python 2.5.2 -- /Users/adam/projects/pdxpython/bin/
python2.5 -- pytest-1.0.0
test object 1: /Users/adam/projects/pdxpython/test-examples.py
test-examples.py:6: TestMath.test_addition PASS
=========================== 1 passed in 0.03 seconds ===========================
9. import nose
def test_add():
assert 2 + 2 == 3
def test_add2():
nose.tools.assert_equals(2 + 2, 3)
(pdxpython)adam@rye:~/projects/pdxpython$ nosetests -v nose-examples.py
nose-examples.test_add ... FAIL
nose-examples.test_add2 ... FAIL
======================================================================
FAIL: nose-examples.test_add
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/adam/projects/pdxpython/lib/python2.5/site-packages/nose-0.11.1-py2.5.egg/nose/case.py",
line 183, in runTest
self.test(*self.arg)
File "/Users/adam/projects/pdxpython/nose-examples.py", line 5, in test_add
assert 2 + 2 == 3
AssertionError
======================================================================
FAIL: nose-examples.test_add2
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/adam/projects/pdxpython/lib/python2.5/site-packages/nose-0.11.1-py2.5.egg/nose/case.py",
line 183, in runTest
self.test(*self.arg)
File "/Users/adam/projects/pdxpython/nose-examples.py", line 9, in test_add2
nose.tools.assert_equals(2 + 2, 3)
AssertionError: 4 != 3
----------------------------------------------------------------------
Ran 2 tests in 0.005 seconds
FAILED (failures=2)
11. Doctest
class Outer(object):
"""Outside worker
>>> outer = Outer()
>>> outer.outer_work()
True
"""
def __init__(self):
self.inner = Inner()
def outer_work(self):
try:
return self.inner.inner_work()
except Exception:
return False
(pdxpython)adam@rye:~/projects/pdxpython$ nosetests -v --with-doctest worker.py
Doctest: worker.Outer ... ok
----------------------------------------------------------------------
Ran 1 test in 0.006 seconds
OK
12. Mocks and Stubs
http://martinfowler.com/articles/mocksArentStubs.html
Sakurako Kitsa Andrei Z
http://www.flickr.com/photos/kitsa_sakurako/1117063708/ http://www.flickr.com/photos/andreiz/330632238/
19. This is your application
AriCee: http://www.flickr.com/photos/aricee/64358127/
20. class User(object):
"""User object, connected to the user_table
>>> user = User(password='password')
>>> db.session.flush()
>>> user # doctest: +ELLIPSIS
<User ...>
>>> user.is_anonymous()
False
>>> User.query.filter_by(id=user.id).one() is user
True
>>> user.id
1
"""
def __init__(self, **kwargs):
21. Solution: Custom Nose
Plugin
from nose.plugins import Plugin
class RollbackPlugin(Plugin):
"""Rollback transaction after each test"""
name = 'rollback'
def beforeTest(self, test):
from myapp import test_utils
test_utils.set_up_app()
def afterTest(self, test):
from myapp import db
db.session.rollback()
db.session.close()
22. setup.py
try:
import ez_setup
ez_setup.use_setuptools()
except ImportError:
pass
from setuptools import setup
setup(
name='RollbackPlugin',
description='Nose plugin to rollback transaction after each test',
py_modules=['rollback'],
version='0.1',
zip_safe=False,
entry_points={
'nose.plugins': [
'rollback = rollback:RollbackPlugin'
]
}
)
nosetests -v --with-doctest --with-rollback