Python unittest test cases third group methods














































Python unittest test cases third group methods



TEST CASES

Third group of methods are :


fail(msg = none)

It signals a test failure unconditionally


failureException

This class attribute gives the exception raised by the test method. 


longMessage

This class attribute determines what happens when a custom failure message is passed as the msg argument to an assertXYY call that fails.


maxDiff

This attribute controls the maximum length of diffs output by assert methods that report diffs on failure. Testing frameworks can use the following

methods to collect information on the test:


countTestCases()

Return the number of tests represented by this test object


defaultTestResult()

Return an instance of the test result class that should be used for this test case class (if no other result instance is provided to the run() method).


Id()

Return a string identifying the specific test case. This is usually the full name of the test method, including the module and class name.


shortDescription()

Returns a description of the test, or None if no description has been provided.


addCleanup(function,/,*args,**kwargs)

Add a function to be called after tearDown() to cleanup resources used during the test


doCleanups()

This method is called unconditionally after tearDown(), or after setUp() if setUp() raises an exception.


Classmethod addClassCleanup(function,/,*args,**kwargs)

Add a function to be called after tearDownClass() to cleanup resources used during the testclass.


Classmethod doClassCleanups()

This method is called unconditionally after tearDownClass(), or
after 
setUpClass() if setUpClass() raises an exception


Class unittest.IsolatedAsynciotestCase(methodName
= runTest)

This class provides an API similar to TestCase and also accepts coroutines as test functions.


run()

Sets up a new event loop to run the test, collecting the result into the TestResult object passed as result.


Code:



import unittest

from unittest import IsolatedAsyncioTestCase

events = []

class Test(IsolatedAsyncioTestCase): 

        def setUp(self):     

              events.append("setUp")

              async def asyncSetUp(self):

                        self._async_connection = await AsyncConnection()

                        events.append("asyncSetUp")

        async def test_response(self):

                       events.append("test_response")

                       response = await self._async_connection.get("https://google.com")

                       self.assertEqual(response.status_code, 200)

                       self.addAsyncCleanup(self.on_cleanup)

         def tearDown(self):

                events.append("tearDown")

         async def asyncTearDown(self):

                   await self._async_connection.close()

                   events.append("asyncTearDown")

   

        async def on_cleanup(self):

                  events.append("cleanup")


if __name__ == "__main__":

unittest.main()



 Next Article : https://cppsecrets.com/users/1380797110117112971091151051101031044948555664103109971051084699111109/Python-Unittest-Loading-and-Running-Tests.php


Comments