Unit Testing with Nose

15
UNIT TESTING WITH NOSE César Cárdenas Desales

Transcript of Unit Testing with Nose

Page 1: Unit Testing with Nose

UNIT TESTING WITH NOSECésar Cárdenas Desales

Page 2: Unit Testing with Nose

Installing nose• Install pip• Install virtualenv• Install pip on your virtual environment

• http://bit.ly/1kCeZv8

$ wget https://bootstrap.pypa.io/get-pip.py$ python get-pip.py $ pip install virtualenv$ virtualenv testingenv$ source testingenv/bin/activate$ python get-pip.py $ pip install nose

Page 3: Unit Testing with Nose

Step 1: Freedom, no unittest boilerplate

$ nosetests $ nosetests basic_tests.py$ nosetests basic_tests

Page 4: Unit Testing with Nose

Step 2: Test functions

$ nosetests $ nosetests basic_tests$ nosetests basic_tests:test_sum$ nosetests basic_tests:test_failing_sum

Page 5: Unit Testing with Nose

Step 3: Test classes

$ nosetests basic_tests:TestSuite$ nosetests basic_tests:TestSuite.test_mult$ nosetests basic_tests:TestSuite.ignored

Page 6: Unit Testing with Nose

Step 4: Specify tests

$ nosetests --collect-only -v$ nosetests --collect-only -v -s $ nosetests -v -s$ nosetests -v --nocapture

Page 7: Unit Testing with Nose

Step 5: Config files

$ nosetests -v -c config.ini

Page 8: Unit Testing with Nose

Step 6: Fixtures

=> @with_setup(setup_function, teardown_function)

$ nosetests –v

Page 9: Unit Testing with Nose

Step 7: Test generators & parallel testing

1 2 3 4 5

OK OK OK FAIL OK

$ nosetests -v $ nosetests --processes=6

Page 10: Unit Testing with Nose

Step 8: Exceptions

$ nosetests –v

Page 11: Unit Testing with Nose

Step 9: Timeouts

$ nosetests -v basic_tests.py$ nosetests -v --processes=6 --process-timeout=1 basic_tests2

Plugins! => --processes --no-capture –collect-only

Page 12: Unit Testing with Nose

Py.test anyone?$ pip install pytest$ py.test step1/basic_tests.py$ py.test step2/basic_tests.py$ py.test -v step4/basic_tests.py...

Page 13: Unit Testing with Nose

Is TDD dead?• No need to be purist and orthodox

• “I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence”

• – Kent Beck

• Test are code to maintain• Test core algorithms, data structures, logic with Biz. Value• Tests can become assertions • Test give you confidence to do changes

Page 14: Unit Testing with Nose

Exercise• Test whether the following code correctly calculates in

which quarter falls the provided datetime.date object

Page 15: Unit Testing with Nose

¿?• ¡ !