Python Tutorial

Showing posts with label unittest. Show all posts
Showing posts with label unittest. Show all posts

Friday, May 13, 2016

Selenium unittest example


import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class JPythonSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
     URL = "http://jpython.blogspot.com"
        driver = self.driver
        driver.get(URL)
        self.assertIn("Life is very easy with Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("BFS")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source


    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

Output:

.
----------------------------------------------------------------------
Ran 1 test in 25.592s

OK
[Finished in 25.7s]

Tuesday, March 31, 2015

Nose unittest OOP example


Here we will test helloWorld.py using nose
All source code are available on github
# testHelloWorldOOP.py
__author__ = 'AbuZahedJony'

from helloWorld import add_two_num
from helloWorld import multi_two_num

class TestHelloWorld:
    def __init__(self):
        pass

    # This function will run before any test case (only once)
    @classmethod
    def setup_class(cls):
        print "Main Setup"

    # This function will run after all test case (only once)
    @classmethod
    def teardown_class(cls):
        print "Main Teardown"

    # This function will call per test case (before)
    def setup(self):
        print "SETUP"

    # This function will call per test case (after)
    def teardown(self):
        print "TEAR-DOWN"

    def test_add_num(self):
        print 10*"*"+" Test add num"
        assert add_two_num(2, 3) == 5
        assert add_two_num(-2, 3) == 1

    def test_multi_num(self):
        print 10*"*"+" Test multi num"
        assert multi_two_num(2, 3) == 6
        assert multi_two_num(-2, 3) == -6
  

RUN: nosetests -s testHelloWorldOOP.py

Output:
Main Setup
SETUP
********** Test add num
TEAR-DOWN
.SETUP
********** Test multi num
TEAR-DOWN
.Main Teardown

----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK

Nose python unittest framework


Nose official doc

Setup

pip install nose

Please install pip if required

Saturday, May 10, 2014

Python unittest examle



All source code available on github
Python unittest example. Here I am testing my insertion sort code.
import random
import unittest


def insert(A,i):
    value = A[i]
    j = i
    while j != 0 and A[j-1]>value:
        A[j] = A[j-1]
        j = j - 1
    A[j] = value


def insertion_sort(A):
     for i in range(len(A)):
         insert(A, i)

class TestInsertionSort(unittest.TestCase):

    def setUp(self):
        print "Setup ..."

    def testSortRange(self):
        print "TestSortRange .... \n"
        n = 10
        a = range(n)
        random.shuffle(a)
        insertion_sort(a)
        self.assertEqual(a, range(n))

        n = 1
        a = range(n)
        random.shuffle(a)
        insertion_sort(a)
        self.assertEqual(a, range(n))

        n = 77
        a = range(n)
        random.shuffle(a)
        insertion_sort(a)
        self.assertEqual(a, range(n))

    def testSortData(self):
        print "TestSortData .... \n"
        a = []
        r = []
        insertion_sort(a)
        self.assertEqual(a, r)

        a = [3, 1, 2]
        r = [1, 2, 3]
        insertion_sort(a)
        self.assertEqual(a, r)


if __name__=="__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(TestInsertionSort)
    unittest.TextTestRunner(verbosity=2).run(suite)


Output:
testSortData (__main__.TestInsertionSortSetup ..) ... ok
.
TestSortData .... 

Setup ...
TestSortRange .... 

testSortRange (__main__.TestInsertionSort) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
[Finished in 0.1s]

Thursday, April 25, 2013

Doctest in python

The doctest module searches for pieces of text that look like interactive Python sessions in docstrings, and then executes those sessions to verify that they work exactly as shown. If test failed it will shows proper message.

def myfun(a, b):
    """
    >>> myfun(2,3)
    5
    >>> myfun(4,3)
    7
    """
    return a+b


if __name__ == '__main__':
    import doctest
    doctest.testmod()

Python unittest

Unittest is included in the Python standard library. It is very easy to use. Here I write two function named myfun, reverse and then unsing MyTest function I tested those two functions. Let's check.

import unittest

def myfun(a, b):
    return a+b

def reverse(A):
    i = 0
    j = len(A)-1
    while i<j:
        A[i], A[j] = A[j], A[i]
        i = i + 1
        j = j -1

class MyTest(unittest.TestCase):
    def test(self):
        self.assertEqual(myfun(2, 3), 5)
        self.assertEqual(myfun(4, 3), 7)

        # test reverse(A)
        A = [1, 2, 3]
        expected = [3, 2, 1]
        reverse(A)
        self.assertEqual(A, expected)
        

if __name__ == '__main__':
    unittest.main(exit=False)