Pages

Tuesday, October 23, 2012

Unit testing - python

Unit testing in python

You'll have more information from this site.

There were some choices for unit test external libraries, but now unittest is a part of
import random
import unittest

from Hello import *

class TestHello(unittest.TestCase):
    def setUp(self):
        self.hello = Hello()

    def test_add(self):
        self.assertEqual(self.hello.add(2,12), 14)

    def test_add2(self):
        self.assertEqual(self.hello.add(2,3), 5)


class TestHello2(unittest.TestCase):
    def setUp(self):
        self.hello = Hello()

    def test_add(self):
        self.assertEqual(self.hello.subtract(2,12), -10)

    def test_add2(self):
        self.assertEqual(self.hello.subtract(2,13), -11)

if __name__ == '__main__':
    unittest.main()
In order to execute the code, you just need to run the unit test code.
python UNIT_TEST_CODE.PY

Things to consider

Normally, unit testing starts with a TestCase per class under the test. And for multiple TestCases, one can bundle the tests into TestSuite as you do in java.

However, with python (it seems like that) the unittest.main() searches into classes that inherit from unittest.TestCase to execute all the tests.

You see that in this example, 4 tests are executed automatically.
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

Unit testing using pycharm

PyCharm is a python development environment implemented in Java programming language from Jet Brains. You will find a Python plugin for IntellJ, but they are two different products.

PyCharm doesn't have automatic unit test generator, but it seems like that the company doesn't have a plan to support it.

PyCharm is smart enough that when you click the run button, it's for executing unit test.

Doctest

Doctest provides easy way to test. This example shows the usage of it.
"""
This is the "example" module.

The example module supplies one function, factorial().  For example,

>>> factorial(5)
120
"""

def factorial(n):
    """Return the factorial of n, an exact integer >= 0.

    If the result is small enough to fit in an int, return an int.
    Else return a long.

    >>> [factorial(n) for n in range(6)]
    [1, 1, 2, 6, 24, 120]

    Factorials of floats are OK, but the float must be an exact integer:
    >>> factorial(30.1)
    Traceback (most recent call last):
        ...
    ValueError: n must be exact integer
    """

    import math
    if not n >= 0:
        raise ValueError("n must be >= 0")
    if math.floor(n) != n:
        raise ValueError("n must be exact integer")
    if n+1 == n:  # catch a value like 1e300
        raise OverflowError("n too large")
    result = 1
    factor = 2
    while factor <= n:
        result *= factor
        factor += 1
    return result

if __name__ == "__main__":
    import doctest
    doctest.testmod()
The format is just use >>> to indicate the code to execute, and write down the expected result including the error message.

When you want to execute it as a command line, nothing will happen as a default. You can use -v option to see the testing result.
smcho@prosseek Desktop> python example.py -v
Trying:
    factorial(5)
Expecting:
    120
ok
Trying:
    [factorial(n) for n in range(6)]
Expecting:
    [1, 1, 2, 6, 24, 120]
ok
Trying:
    factorial(30.1)
Expecting:
    Traceback (most recent call last):
        ...
    ValueError: n must be exact integer
ok
2 items passed all tests:
   1 tests in __main__
   2 tests in __main__.factorial
3 tests in 2 items.
3 passed and 0 failed.
Test passed.

Reference

  1. doctest

No comments:

Post a Comment