Pyunit

Embed Size (px)

Citation preview

  1. 1. PyUnit Framework Ikuru K
  2. 2. index unittest how to with python Generic Unit test loader & Runner Coverage.py
  3. 3. Import unittest module No external dependencies: its packaged by default.
  4. 4. Creating a unit test
  5. 5. Points Test case is a class that inherits from TestCase class.
  6. 6. Parameters Need to have self as parameter all the time; same as defining any new class in python. (Is this the case?)
  7. 7. Class Name Class name needs to start with Test Self awareness begins from a class named a test in unittest realm.
  8. 8. Method (=unit test case) name needs to start with test If not, the method will not be part of the test report.
  9. 9. Number of test methods Can have multiple test methods.
  10. 10. File name File containing the test can be named anything, but for making detection/loading/running easier it is a good idea to give it a consistent naming (i.e. name it as *test.py)
  11. 11. __name__? The last if __name__ part enables the Test case class to be inferred as a test case when running it from command line However, does not get executed when running from a test runner which will be described later.
  12. 12. Output looks like: usr:~/Machine-Translation-JP-EN/tests$ python random_splitter_test.py ... --------------------------------------------------------------------- - Ran 3 tests in 0.001s OK
  13. 13. Loading and running test cases Short answer: Place all test case files in a single directory and run this script to show an aggregated result of all rests being run:
  14. 14. Anatomy Loader identifies unittests by file name pattern Test runner runs all of the identified test methods and test classes to run them in bulk. Please ignore irrelevant imports and comment; that was my own setup.
  15. 15. Bonus: coverage package Use coverage package to get a concise report (text and html) of how well your code is tested. Very easy to use Tells you which parts were missed Can handle C1 coverage as well Run pip install coverage
  16. 16. Sample output ikuru@debian:~/Machine-Translation-JP-EN/tests$ coverage report -m Name Stmts Miss Cover Missing --------------------------------------------------------------------------------------------- /home/ikuru/Machine-Translation-JP-EN/src/mac_tran_core_logic 18 0 100% /home/ikuru/Machine-Translation-JP-EN/src/mac_tran_dao 234 34 85% 80-91, 103-104, 150-161, 186-195 /home/ikuru/Machine-Translation-JP-EN/src/mac_tran_utils 23 2 91% 22-23 /home/ikuru/Machine-Translation-JP-EN/src/mac_tran_vo 13 4 69% 24, 27, 30, 33 PyUnit_All 15 0 100% build_map_test 34 1 97% 44 chunk_search_test 46 1 98% 59 mac_tran_vo_test 15 1 93% 21 random_splitter_test 28 1 96% 52 retrieve_words_in_sentence_test 70 1 99% 99 templetize_test 27 1 96% 35 translate_word_test 35 1 97% 41 --------------------------------------------------------------------------------------------- TOTAL 558 47 92%
  17. 17. Thank you!