Monday, January 26, 2015

Python unittest with two different testcases

Python unittest with two different testcases:

Main file contains:

import unittest
from my_math import product
from dbtest import Dbquery

class Test(unittest.TestCase):
    """Tests for Product"""

    def test_product(self):
        try:
            self.failUnlessEqual(product(2,3),6,"Product Testcase")
        except AssertionError:
            print "Assertion Error, Check the Values"

    def test_mysql(self):
        try:
            check = str("(1, u'Vallinayagam.K', u'Welcome to DB World')")
            self.failUnlessAlmostEqual(str(Dbquery("SELECT * FROM name")),check)
        except AssertionError:
            print "Assertion Error"


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


my_math file contains:

def product(a,b):
    return a*b

dbtest file contains:

import mysql.connector

def Dbquery(query):
    try:
        conn = mysql.connector.connect(user="root", password="admin",host="127.0.0.1",database="test")
        cursor = conn.cursor()
        cursor.execute(query)
        for (id,username,tweet) in cursor:
            return id,username,tweet
        cursor.close()
        conn.close()
    except mysql.connector.Error as err:
        print "Error " + format(err)



Output:





No comments:

Post a Comment