Python unittest

What is unittest?

unittest is a framework that is designed for testing. A regular script can accomplish a task, say connect to a web server, download a page, and look for some text in the webpage. If this is all you want to do, then a simple python script is your option. Otherwise, if there are multiple tasks for you and each task is a test, then unittest is your solution.

You can write a set of test cases using unittest framework, and run the test cases. unittest framework is designed to pick up a test case at a time, run it, and post the result at the end of all test cases.

Sample unittest script:

So, let's dive into unittest, and write our first test suite. A test suite is a set of test cases. In our example, we will write 2 test cases, and run them.
 * 1) !/usr/bin/python

__author__ = 'rakesh.reddy'

import unittest

import string

class TestUnitTest(unittest.TestCase):

def test_000_case_conversion(self):

"""

This test case verifies if variable upper is converted to lower case

"""

upper = "PYTHON"

lower = "python"

if upper.lower == lower:

return 1

else:

return 0

def test_001_check_ascii(self):

"""

This test case verifies if the given word is ASCII

"""

myword = "Python is an interesting language. I am loving it!!!!"

for ch in myword:

if ch not in string.letters or ch not in string.punctuation:

return 0

else:

return 0

if __name__ == "__main__":

unittest.main