Sunday, April 26, 2020

UnitTest Framework - API Python


                                         




test fixture
test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.
test case
test case is the smallest unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase, which may be used to create new test cases.
test suite
test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
test runner
test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.


import unittest

class CodeVlidation(unittest.TestCase):

    def setUp(self):
        print("Setup the environment")
    def tearDown(self):
        print("cleaning the environment")

    def testcase1(self):
        print("running the test case1")
        self.assertEqual(2,2)
    def testcase2(self):
        print("running the test case2")
        self.assertEqual(2,3)

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

Setup the environment
running the test case1
cleaning the environment
.Setup the environment
running the test case2
cleaning the environment
F
======================================================================
FAIL: testcase2 (__main__.CodeVlidation)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "UnitTest.py", line 15, in testcase2
    self.assertEqual(2,3)
AssertionError: 2 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.003s

FAILED (failures=1)

(Python3.8) D:\import\UnitTestPython>


TestSuite Class

Test suites are implemented by the TestSuite class. This class allows individual tests and test suites to be aggregated; when the suite is executed, all tests added directly to the suite and in “child” test suites are run.

import unittest

class CodeVlidation(unittest.TestCase):

   
@classmethod
   
def setUpClass(cls):
       
print('Executed Before the all the method executed in class')

   
@classmethod
   
def tearDownClass(cls):
       
print('xecuted after the all the method executed in class')


   
def setUp(self):
       
print("Setup the environment")
   
def tearDown(self):
       
print("cleaning the environment")

   
def testcase1(self):
       
print("running the test case1")
       
self.assertEqual(2,2)
   
def testcase2(self):
       
print("running the test case2")
       
self.assertEqual(2,3)

def suite():
    suite = unittest.TestSuite()
       
##   suite.addTest (CodeVlidation("testcase1"))
        ##   suite.addTest (CodeVlidation("testcase2"))
   
suite.addTest(unittest.makeSuite(CodeVlidation))
   
return suite

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    test_suite = suite()
    runner.run (test_suite)
Output

D:\import\UnitTestPython>python UnitTest.py
Executed Before the all the method executed in class
Setup the environment
running the test case1
cleaning the environment
.Setup the environment
running the test case2
cleaning the environment
Fxecuted after the all the method executed in class

======================================================================
FAIL: testcase2 (__main__.CodeVlidation)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "UnitTest.py", line 24, in testcase2
    self.assertEqual(2,3)
AssertionError: 2 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.037s

FAILED (failures=1)

D:\import\UnitTestPython>


TestLoader Class


               
import unittest

  

  class CodeVlidation(unittest.TestCase):

  

    @classmethod

    def setUpClass(cls):

        print('Executed Before the all the method executed in class')

  

    @classmethod

    def tearDownClass(cls):

        print('xecuted after the all the method executed in class')

  

  

    def setUp(self):

        print("Setup the environment")

    def tearDown(self):

        print("cleaning the environment")

  

    def testcase1(self):

        print("running the test case1")

        self.assertEqual(2, 2)

    def testcase2(self):

        print("running the test case2")

        self.assertEqual(2, 3)

  

  # def suite():

#     suite = unittest.TestSuite()

#         ##   suite.addTest (CodeVlidation("testcase1"))

#         ##   suite.addTest (CodeVlidation("testcase2"))

#     suite.addTest(unittest.makeSuite(CodeVlidation))

#     return suite

#

# if __name__ == '__main__':

#     runner = unittest.TextTestRunner()

#     test_suite = suite()

#     runner.run (test_suite)

  

  if __name__ == '__main__':

  

    testList = [CodeVlidation]

    testLoad = unittest.TestLoader()

  

    TestList = []

    for testCase in testList:

        testSuite = testLoad.loadTestsFromTestCase(testCase)

        TestList.append(testSuite)

  

    newSuite = unittest.TestSuite(TestList)

    runner = unittest.TextTestRunner()

    runner.run(newSuite)
 
D:\import\UnitTestPython>python UnitTest12345.py
Executed Before the all the method executed in class
Setup the environment
running the test case1
cleaning the environment
.Setup the environment
running the test case2
cleaning the environment
Fxecuted after the all the method executed in class

======================================================================
FAIL: testcase2 (__main__.CodeVlidation)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "UnitTest12345.py", line 24, in testcase2
    self.assertEqual(2, 3)
AssertionError: 2 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.064s

FAILED (failures=1)

D:\import\UnitTestPython>




No comments:

Post a Comment