Unit Testing in Python

Here are some unit tests for using pyunit for the balanced brackets function that was posted earlier. The idea is simply to demonstrate how unit testing for python works and in no way claims to be best coding practices or without errors.  Its simply a learning process at this point.

#!/usr/bin/env/python
import unittest
import BalancedBrackets
class SimpleTestCases(unittest.TestCase):
“””All basic test cases”””
def emptyString(self):
assert BalancedBrackets.isBalanced(“”) == False, ‘SimpleTestCase failed’
def stringWithNoBrackets(self):
assert BalancedBrackets.isBalanced(“there are no brackets in this string”), ‘SimpleTestCase failed’
def balancedSingleRoundBrackets(self):
assert BalancedBrackets.isBalanced(“(Test)”), ‘SimpleTestCase failed’
def unbalancedSingleRoundBrackets(self):
assert BalancedBrackets.isBalanced(“(Test”) == False, ‘SimpleTestCase failed’
def balancedSingleSquareBrackets(self):
assert BalancedBrackets.isBalanced(“[Test]”), ‘SimpleTestCase failed’
def unbalancedSingleSquareBrackets(self):
assert BalancedBrackets.isBalanced(“[Test”) == False, ‘SimpleTestCase failed’
def balancedSingleCurlyBrackets(self):
assert BalancedBrackets.isBalanced(“{Test}”), ‘SimpleTestCase failed’
def unbalancedSingleCurlyBrackets(self):
assert BalancedBrackets.isBalanced(“{Test”) == False, ‘SimpleTestCase failed’
class ComplicatedTestCases(unittest.TestCase):
“””A bit more complicated test cases”””
def balancedMultipleRoundBrackets(self):
assert BalancedBrackets.isBalanced(“(((Test)))”), ‘Multiple Round Brakets Case failed’
def unbalancedMultipleRoundBrackets(self):
assert BalancedBrackets.isBalanced(“(((Test))”) == False, ‘SimpleTestCase failed’
def balancedMultipleBrackets(self):
assert BalancedBrackets.isBalanced(“({[Test]})”), ‘SimpleTestCase failed’
def unbalancedMultipleBrackets(self):
assert BalancedBrackets.isBalanced(“({[Test]}”) == False, ‘SimpleTestCase failed’
def wrongOrderMultipleBrackets(self):
assert BalancedBrackets.isBalanced(“({[Test])}”) == False, ‘SimpleTestCase failed’
def simpleTestSuite():
suite = unittest.TestSuite()
suite.addTest(SimpleTestCases(“emptyString”))
suite.addTest(SimpleTestCases(“stringWithNoBrackets”))
suite.addTest(SimpleTestCases(“balancedSingleRoundBrackets”))
suite.addTest(SimpleTestCases(“unbalancedSingleRoundBrackets”))
suite.addTest(SimpleTestCases(“balancedSingleSquareBrackets”))
suite.addTest(SimpleTestCases(“unbalancedSingleSquareBrackets”))
suite.addTest(SimpleTestCases(“balancedSingleCurlyBrackets”))
suite.addTest(SimpleTestCases(“unbalancedSingleCurlyBrackets”))
return suite
def complicatedTestSuite():
suite = unittest.TestSuite()
suite.addTest(ComplicatedTestCases(“balancedMultipleRoundBrackets”))
suite.addTest(ComplicatedTestCases(“unbalancedMultipleRoundBrackets”))
suite.addTest(ComplicatedTestCases(“balancedMultipleBrackets”))
suite.addTest(ComplicatedTestCases(“unbalancedMultipleBrackets”))
suite.addTest(ComplicatedTestCases(“wrongOrderMultipleBrackets”))
return suite
simpleTestSuite = simpleTestSuite()
complicatedTestSuite = complicatedTestSuite()
runner = unittest.TextTestRunner()
runner.run(simpleTestSuite)
runner.run(complicatedTestSuite)
#!/usr/bin/env/python

import unittest

import BalancedBrackets

 

class SimpleTestCases(unittest.TestCase):

    """All basic test cases"""

    def emptyString(self):

        assert BalancedBrackets.isBalanced("") == False, 'SimpleTestCase failed'

    def stringWithNoBrackets(self):

        assert BalancedBrackets.isBalanced("there are no brackets in this string"), 'SimpleTestCase failed'

    def balancedSingleRoundBrackets(self):

        assert BalancedBrackets.isBalanced("(Test)"), 'SimpleTestCase failed'

    def unbalancedSingleRoundBrackets(self):

        assert BalancedBrackets.isBalanced("(Test") == False, 'SimpleTestCase failed'

    def balancedSingleSquareBrackets(self):

        assert BalancedBrackets.isBalanced("[Test]"), 'SimpleTestCase failed'

    def unbalancedSingleSquareBrackets(self):

        assert BalancedBrackets.isBalanced("[Test") == False, 'SimpleTestCase failed'

    def balancedSingleCurlyBrackets(self):

        assert BalancedBrackets.isBalanced("{Test}"), 'SimpleTestCase failed'

    def unbalancedSingleCurlyBrackets(self):

        assert BalancedBrackets.isBalanced("{Test") == False, 'SimpleTestCase failed'

 

class ComplicatedTestCases(unittest.TestCase):

    """A bit more complicated test cases"""

    def balancedMultipleRoundBrackets(self):

        assert BalancedBrackets.isBalanced("(((Test)))"), 'Multiple Round Brakets Case failed'

    def unbalancedMultipleRoundBrackets(self):

        assert BalancedBrackets.isBalanced("(((Test))") == False, 'SimpleTestCase failed'

    def balancedMultipleBrackets(self):

        assert BalancedBrackets.isBalanced("({[Test]})"), 'SimpleTestCase failed'

    def unbalancedMultipleBrackets(self):

        assert BalancedBrackets.isBalanced("({[Test]}") == False, 'SimpleTestCase failed'

    def wrongOrderMultipleBrackets(self):

        assert BalancedBrackets.isBalanced("({[Test])}") == False, 'SimpleTestCase failed'

def simpleTestSuite():

    suite = unittest.TestSuite()suit

    suite.addTest(SimpleTestCases("emptyString"))

    suite.addTest(SimpleTestCases("stringWithNoBrackets"))

    suite.addTest(SimpleTestCases("balancedSingleRoundBrackets"))

    suite.addTest(SimpleTestCases("unbalancedSingleRoundBrackets"))

    suite.addTest(SimpleTestCases("balancedSingleSquareBrackets"))

    suite.addTest(SimpleTestCases("unbalancedSingleSquareBrackets"))

    suite.addTest(SimpleTestCases("balancedSingleCurlyBrackets"))

    suite.addTest(SimpleTestCases("unbalancedSingleCurlyBrackets"))

    return suite

def complicatedTestSuite():

    suite = unittest.TestSuite()

    suite.addTest(ComplicatedTestCases("balancedMultipleRoundBrackets"))

    suite.addTest(ComplicatedTestCases("unbalancedMultipleRoundBrackets"))

    suite.addTest(ComplicatedTestCases("balancedMultipleBrackets"))

    suite.addTest(ComplicatedTestCases("unbalancedMultipleBrackets"))

    suite.addTest(ComplicatedTestCases("wrongOrderMultipleBrackets"))

    return suite

 

simpleTestSuite = simpleTestSuite()

complicatedTestSuite = complicatedTestSuite()

runner = unittest.TextTestRunner()

runner.run(simpleTestSuite)

runner.run(complicatedTestSuite)
Advertisements
This entry was posted in Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s