PyUnit Framework
Ikuru K
index
● unittest how to with python
● Generic Unit test loader & Runner
● Coverage.py
Import unittest module
● No external dependencies: its packaged by
default.
Creating a unit test
Points
● Test case is a class that inherits from TestCase
class.
Parameters
● Need to have self as parameter all the time;
same as defining any new class in python. (Is
this the case?)
Class Name
● Class name needs to start with “Test”
– Self awareness begins from a class named a test in
unittest realm.
● Method (=unit test case) name needs to start
with “test”
– If not, the method will not be part of the test report.
Number of test methods
● Can have multiple test methods.
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)
__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.
Output looks like:
usr:~/Machine-Translation-JP-EN/tests$ python
random_splitter_test.py
...
---------------------------------------------------------------------
-
Ran 3 tests in 0.001s
OK
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:
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.
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
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%
● Thank you!

Pyunit

  • 1.
  • 2.
    index ● unittest howto with python ● Generic Unit test loader & Runner ● Coverage.py
  • 3.
    Import unittest module ●No external dependencies: its packaged by default.
  • 4.
  • 5.
    Points ● Test caseis a class that inherits from TestCase class.
  • 6.
    Parameters ● Need tohave self as parameter all the time; same as defining any new class in python. (Is this the case?)
  • 7.
    Class Name ● Classname needs to start with “Test” – Self awareness begins from a class named a test in unittest realm.
  • 8.
    ● Method (=unittest case) name needs to start with “test” – If not, the method will not be part of the test report.
  • 9.
    Number of testmethods ● Can have multiple test methods.
  • 10.
    File name ● Filecontaining 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.
    __name__? ● The lastif __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.
    Output looks like: usr:~/Machine-Translation-JP-EN/tests$python random_splitter_test.py ... --------------------------------------------------------------------- - Ran 3 tests in 0.001s OK
  • 13.
    Loading and runningtest 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.
    Anatomy ● Loader identifiesunittests 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.
    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.
    Sample output ikuru@debian:~/Machine-Translation-JP-EN/tests$ coveragereport -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.