UNIT TESTING WITH
NOSE
César Cárdenas Desales
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
Step 1: Freedom, no unittest boilerplate
$ nosetests
$ nosetests basic_tests.py
$ nosetests basic_tests
Step 2: Test functions
$ nosetests
$ nosetests basic_tests
$ nosetests basic_tests:test_sum
$ nosetests basic_tests:test_failing_sum
Step 3: Test classes
$ nosetests basic_tests:TestSuite
$ nosetests basic_tests:TestSuite.test_mult
$ nosetests basic_tests:TestSuite.ignored
Step 4: Specify tests
$ nosetests --collect-only -v
$ nosetests --collect-only -v -s
$ nosetests -v -s
$ nosetests -v --nocapture
Step 5: Config files
$ nosetests -v -c config.ini
Step 6: Fixtures
=> @with_setup(setup_function, teardown_function)
$ nosetests –v
Step 7: Test generators & parallel testing
1 2 3 4 5
OK OK OK FAIL OK
$ nosetests -v
$ nosetests --processes=6
Step 8: Exceptions
$ nosetests –v
Step 9: Timeouts
$ nosetests -v basic_tests.py
$ nosetests -v --processes=6 --process-timeout=1 basic_tests2
Plugins! => --processes --no-capture –collect-only
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
...
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
Exercise
• Test whether the following code correctly calculates in
which quarter falls the provided datetime.date object
¿?
• ¡ !

Unit Testing with Nose

  • 1.
  • 2.
    Installing nose • Installpip • 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
  • 3.
    Step 1: Freedom,no unittest boilerplate $ nosetests $ nosetests basic_tests.py $ nosetests basic_tests
  • 4.
    Step 2: Testfunctions $ nosetests $ nosetests basic_tests $ nosetests basic_tests:test_sum $ nosetests basic_tests:test_failing_sum
  • 5.
    Step 3: Testclasses $ nosetests basic_tests:TestSuite $ nosetests basic_tests:TestSuite.test_mult $ nosetests basic_tests:TestSuite.ignored
  • 6.
    Step 4: Specifytests $ nosetests --collect-only -v $ nosetests --collect-only -v -s $ nosetests -v -s $ nosetests -v --nocapture
  • 7.
    Step 5: Configfiles $ nosetests -v -c config.ini
  • 8.
    Step 6: Fixtures =>@with_setup(setup_function, teardown_function) $ nosetests –v
  • 9.
    Step 7: Testgenerators & parallel testing 1 2 3 4 5 OK OK OK FAIL OK $ nosetests -v $ nosetests --processes=6
  • 10.
    Step 8: Exceptions $nosetests –v
  • 11.
    Step 9: Timeouts $nosetests -v basic_tests.py $ nosetests -v --processes=6 --process-timeout=1 basic_tests2 Plugins! => --processes --no-capture –collect-only
  • 12.
    Py.test anyone? $ pipinstall pytest $ py.test step1/basic_tests.py $ py.test step2/basic_tests.py $ py.test -v step4/basic_tests.py ...
  • 13.
    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
  • 14.
    Exercise • Test whetherthe following code correctly calculates in which quarter falls the provided datetime.date object
  • 15.