python-nose: a unittest-based testing framework for python that makes writing and running tests...
DESCRIPTION
Barcelona Python MeetupTRANSCRIPT
Python-nose: A unittest-based testing framework
for Python that makes writing and running tests
easier
Timo Stollenwerk
April 27th, 2009
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Introduction
Usually: Write unit tests with boilerplate code
Automated test discovery and running process for unittest
Intended to mimic the behavior of py.test
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Installation
$ e a s y_ i n s t a l l nose
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Egg Installation
setup.py
. . .s e tup (
. . .t e s t_ s u i t e =' nose . c o l l e c t o r ' ,t e s t_ r e q u i r e s =[ ' nose ' ] ,i n s t a l l _ r e q u i r e s =[
. . .' nose ' ,
] ,. . .
)
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Basic Usage
nosetests [options] [(optional) test �les or directories]
Options
Test selection (path, package, function, etc.)
Run only tests with certain attributes
PDB
Plugins
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Basic Usage: Con�guration
/.noserc
[ n o s e t e s t s ]v e r b o s i t y=3with−do c t e s t=1doc t e s t−e x t e n s i o n =. t x t
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Writing Unit Tests
Simplest possible failing test:
d e f t e s t ( ) :a s s e r t F a l s e
And the simplest passing test:
d e f t e s t ( ) :pa s s
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Example Unit Tests
from l xm l impor t o b j e c t i f y
c l a s s TestTrans form ( ) :
d e f setUp ( s e l f ) :from html2docbook impor t Html2DocBookh2d = Html2DocBook ( )s e l f . h2d = h2d
de f t e s t_pa rag raph s ( s e l f ) :html = '<p>lorem</p><p>ipsum</p>'expec t = '< s e c t i o n ><para>lorem</para><para>ipsum</para></s e c t i o n >'xml = s e l f . h2d . t r an s f o rm ( html )a s s e r t xml == expec t
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Automate your Unit tests with Nose
nosy.py
Every time you change any .py �le, it runs tests
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Automate your Unit tests with Nose and Eclipse
Go to the project properties
De�ne a new "program"builder
Set the location to the nosetest script
Set the working directory to the project location${workspace_loc:/html2docbook}
Optional: append any command line options you want
Go to the "build options"tag and tick the "launch inbackgroundänd "during auto builds".
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Conclusion
Nose makes it easier to write and run useful tests.
Useful tests make it easier to write less code and better code.
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier
Futher Information
Nose: http://code.google.com/p/python-nose/
nosy.py: http://jeffwinkler.net/2006/04/27/keeping-your-nose-green/
Automated python testing with nose and eclipse:http://www.machine-envy.com/blog/2006/07/29/
automated-python-testing-with-nose-and-eclipse/
Pycon 2008 Talk: Testing for the lazy coderhttp://us.pycon.org/common/2008/talkdata/
PyCon2008/079/nose_pycon08.pdf
Timo Stollenwerk Python-nose: A unittest-based testing framework for Python that makes writing and running tests easier