Unit Testing with Nose
-
Upload
cesar-cardenas-desales -
Category
Technology
-
view
31 -
download
0
Transcript of Unit Testing with Nose
![Page 1: Unit Testing with Nose](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/1.jpg)
UNIT TESTING WITH NOSECésar Cárdenas Desales
![Page 2: Unit Testing with Nose](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/3.jpg)
Step 1: Freedom, no unittest boilerplate
$ nosetests $ nosetests basic_tests.py$ nosetests basic_tests
![Page 4: Unit Testing with Nose](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/7.jpg)
Step 5: Config files
$ nosetests -v -c config.ini
![Page 8: Unit Testing with Nose](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/8.jpg)
Step 6: Fixtures
=> @with_setup(setup_function, teardown_function)
$ nosetests –v
![Page 9: Unit Testing with Nose](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/10.jpg)
Step 8: Exceptions
$ nosetests –v
![Page 11: Unit Testing with Nose](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/14.jpg)
Exercise• Test whether the following code correctly calculates in
which quarter falls the provided datetime.date object
![Page 15: Unit Testing with Nose](https://reader035.fdocuments.us/reader035/viewer/2022070602/5875bc611a28ab33128b496f/html5/thumbnails/15.jpg)
¿?• ¡ !