Python unittesting method call issue

Milson Munakami milsonmun at gmail.com
Thu Oct 2 17:30:39 CEST 2014


On Saturday, September 27, 2014 5:26:00 PM UTC-6, Milson Munakami wrote:
> I am trying to set the precondition for the test first prior to test other test cases. But as you can see in my code the precondition print command is not fired! Can you explain the cause and solution how to fire the code with in that method i.e. SetPreConditionFirewall() with setup variable self.<variablename> in this fucntion. Here is my code:
> 
> 
> 
> import json
> 
> import urllib
> 
> #import time
> 
> #from util import *
> 
> import httplib
> 
> 
> 
> #import sys
> 
> #from scapy.all import *
> 
> 
> 
> import unittest
> 
> 
> 
> import os, sys, socket, struct, select, time 
> 
> from threading import Thread
> 
> 
> 
> import logging
> 
> import traceback
> 
> 
> 
> class testFirewallS1( unittest.TestCase ):
> 
>     def setUp(self):
> 
>         self.controllerIp="127.0.0.1"
> 
>     def tearDown(self):
> 
>         if self.failed:
> 
>             return
> 
>         duration = time.time() - self.startTime_
> 
>         self.cleanup(True)
> 
>         if self.reportStatus_:
> 
>             self.log.info("=== Test %s completed normally (%d sec)", self.name_, duration)
> 
> 
> 
>     def cleanup(self, success):
> 
>         sys.excepthook = sys.__excepthook__
> 
>         try:
> 
>             return
> 
>         except NameError:
> 
>             self.log.error("Exception hit during cleanup, bypassing:\n%s\n\n" % traceback.format_exc())
> 
>             pass
> 
>         else:
> 
> 
> 
>                 fail("Expected a NameError")    
> 
> 
> 
>     def SetPreConditionFirewall(self):
> 
>         command = "http://%s:8080/wm/firewall/module/enable/json" % self.controllerIp
> 
>         urllib.urlopen(command).read()
> 
>         print self.controllerIp
> 
>         print "Test Pre-condition setting here"
> 
> 
> 
>     #Precondition Test
> 
>     def testPreConditionFirewall(self):
> 
>         print "Test pass"   
> 
> 
> 
> def suite():
> 
> 
> 
>         suite = unittest.TestSuite()
> 
> 
> 
>         suite.addTest(unittest.makeSuite(testFirewallS1))
> 
> 
> 
>         return suite
> 
> 
> 
> 
> 
> if __name__ == '__main__':
> 
> 
> 
>         suiteFew = unittest.TestSuite()
> 
> 
> 
>         testFirewallS1("SetPreConditionFirewall")
> 
> 
> 
>         suiteFew.addTest(testFirewallS1("testPreConditionFirewall"))
> 
> 
> 
> 
> 
>         #Testing the Test Cases Begins Here
> 
>         unittest.TextTestRunner(verbosity=2).run(suiteFew)
> 
> 
> 
> How can I do access that method on __main__ and how can that method can access the setup variable.
> 
> 
> 
> Thanks

Thanks :)



More information about the Python-list mailing list