@vinaymavi
Unit Testing?
Local Modules
Components of Unit Testing
Test Fixture
Test Case
Test case is logical program to test a particular unit
Text Assertion
Test Assertion is a statement that decide the result of a particular test.
Test Suite
A collection of Test Cases is called Test Suite.
Test Runner
Test Runner is a program or framework that helps to execute the Test Suites.
unittest framework
import unittest
class <test_sute>(unittest.TestCase):
def test_<test_case>(self):
# Assertion
self.assertEqual('foo'.upper(), 'FOO')
unittest framework
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'TOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
unittest framework
import unittest
class Person(object):
def set_name(self,name):
self.name = name
class TestCaseFixture(unittest.TestCase):
person = None
def setUp(self):
print "SETUP CALLING..."
TestCaseFixture.person = Person()
TestCaseFixture.person.set_name("Alivenet")
def test_name(self):
self.assertEqual(TestCaseFixture.person.name, 'Alivenet')
def tearDown(self):
print "TEAR DOWN CALLIN...."
if __name__ == '__main__':
unittest.main()
unittest framework