python unittest add message for all passed case also and print it in nose html report

2.5k Views Asked by At

I run the following code with command "nosetests --with-html test_rest_api.py"

class Test_rest_api(unittest.TestCase):

    def test_create_task_group(self):
        data = {"name":"group2"}
        response = ib_api.rest_request('POST', object_type="create_Test")
        msg = json.loads(response.read())
        self.assertTrue(response.status >= 200 and response.status < 300,msg)

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

If the case is failed i get the string which is in variable "msg", but if it the case is passed i don't get the message

Tried the solution as below,

self.assertTrue(response.status == 200 , msg)
print msg

This works but the issue here is if the case gets failed the message appears 2 times in the html report

Please suggest any good solution to handle the above case

1

There are 1 best solutions below

0
kiminoa On

I wound up customizing unittest's assert functions to get PASS messages in my logging.

Like this:

class TestBase(unittest.TestCase):
    def _assert_logs(self, expr, msg=None):                                                         
        """                                                                                         
        Helper function: Auto-logs PASS/FAIL for all overriden asserts                                 

        Keyword arguments:                                                                             
            expr -- Conditional expression appearing in an assert, ex. "x is None"                     
            msg -- General message to log (default = None)                                             
        """                                                                                         
        if expr:                                                                                       
            self.log.debug("PASS {0}".format(msg))                                                     
        else:                                                                                          
            self.log.error("FAIL {0}".format(msg)) 

    def assertIn(self, member, container, msg=None, expected=None,                                     
                 actual=None):                                                                         
        """                                                                                            
        Prepend unittest.TestCase's assertIn function for logging                                      

        Arguments:                                                                                     
            first -- first variable of conditional being evaluated                                     
            second -- second variable of conditional being evaluated                                   

        Keyword arguments:                                                                             
            msg -- msg that identifies the test assertion                                              
            expected -- msg indicating expected result                                                 
            actual -- msg indicating actual result                                                     
        """                                                                                            
        msg = self._format_message(msg, expected, actual)                                              
        self._assert_logs(member in container, msg)                                                    
        super(TestBase, self).assertIn(member, container, msg)

Then have your test cases inherit from TestBase instead of unittest.TestCase. Hope it helps!