Re: How to set the global variable so that it can be accessed and released inside other methods
Hi Chris, I want to remove that CreateNet() part from the test class so that it will not create network every time in setup() because all the test cases are run and tested in same network! But the test class method also need to access the net object How can I do that? Please Help. Thanks, Milson On Thursday, October 2, 2014 9:30:21 AM UTC-6, Milson Munakami wrote: > Hi, > > > > I am newbie to Python, > > > > I am trying to use unittest and python. My python script is like this: > > > > #! /usr/bin/env python > > __author__ = 'Milson Munakami' > > __revision__ = '0.0.2' > > > > import json > > import urllib > > import httplib > > from scapy.all import * > > > > import unittest > > > > import os, sys, socket, struct, select, time > > from threading import Thread > > > > import logging > > import traceback > > > > from mininet.net import Mininet > > from mininet.node import OVSSwitch, OVSKernelSwitch, Controller, > RemoteController > > from mininet.log import setLogLevel, info > > from mininet.cli import CLI > > > > class testFirewallS1( unittest.TestCase ): > > > > #I am trying to set net variable to global > > global net > > ## > > > > def setUp(self): > > self.controllerIp="127.0.0.1" > > self.switch = "00:00:00:00:00:00:00:01" > > self.destinationIp = "10.0.0.1" > > self.startTime_ = time.time() > > self.failed = False > > self.reportStatus_ = True > > self.name_ = "Firewall" > > self.log = logging.getLogger("unittest") > > self.CreateNet() > > self.SetPrecondition() > > > > def CreateNet(self): > > "Create an empty network and add nodes to it." > > net = Mininet( controller=RemoteController ) > > > > info( '*** Adding controller\n' ) > > net.addController( 'c0' , controller=RemoteController,ip= > "127.0.0.1", port=6633) > > > > info( '*** Adding hosts\n' ) > > h1 = net.addHost( 'h1', ip='10.0.0.1' ) > > h2 = net.addHost( 'h2', ip='10.0.0.2' ) > > h3 = net.addHost( 'h3', ip='10.0.0.3' ) > > > > info( '*** Adding switch\n' ) > > s1 = net.addSwitch( 's1' ) > > > > info( '*** Creating links\n' ) > > net.addLink( h1, s1 ) > > net.addLink( h2, s1 ) > > net.addLink( h3, s1 ) > > > > info( '*** Starting network\n') > > net.start() > > > > 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__ > > self.SetFinalcondition() > > 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 StatusFirewall(self): > > command = "http://%s:8080/wm/firewall/module/status/json"; % > self.controllerIp > > x = urllib.urlopen(command).read() > > parsedResult = json.loads(x) > > return parsedResult['result'] > > > > def CountFirewallRules(self): > > command = "http://%s:8080/wm/firewall/rules/json"; % > self.controllerIp > > x = urllib.urlopen(command).read() > > return x > >
Re: Python unittesting method call issue
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. 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 :) -- https://mail.python.org/mailman/listinfo/python-list
How to set the global variable so that it can be accessed and released inside other methods
Hi, I am newbie to Python, I am trying to use unittest and python. My python script is like this: #! /usr/bin/env python __author__ = 'Milson Munakami' __revision__ = '0.0.2' import json import urllib import httplib from scapy.all import * import unittest import os, sys, socket, struct, select, time from threading import Thread import logging import traceback from mininet.net import Mininet from mininet.node import OVSSwitch, OVSKernelSwitch, Controller, RemoteController from mininet.log import setLogLevel, info from mininet.cli import CLI class testFirewallS1( unittest.TestCase ): #I am trying to set net variable to global global net ## def setUp(self): self.controllerIp="127.0.0.1" self.switch = "00:00:00:00:00:00:00:01" self.destinationIp = "10.0.0.1" self.startTime_ = time.time() self.failed = False self.reportStatus_ = True self.name_ = "Firewall" self.log = logging.getLogger("unittest") self.CreateNet() self.SetPrecondition() def CreateNet(self): "Create an empty network and add nodes to it." net = Mininet( controller=RemoteController ) info( '*** Adding controller\n' ) net.addController( 'c0' , controller=RemoteController,ip= "127.0.0.1", port=6633) info( '*** Adding hosts\n' ) h1 = net.addHost( 'h1', ip='10.0.0.1' ) h2 = net.addHost( 'h2', ip='10.0.0.2' ) h3 = net.addHost( 'h3', ip='10.0.0.3' ) info( '*** Adding switch\n' ) s1 = net.addSwitch( 's1' ) info( '*** Creating links\n' ) net.addLink( h1, s1 ) net.addLink( h2, s1 ) net.addLink( h3, s1 ) info( '*** Starting network\n') net.start() 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__ self.SetFinalcondition() 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 StatusFirewall(self): command = "http://%s:8080/wm/firewall/module/status/json"; % self.controllerIp x = urllib.urlopen(command).read() parsedResult = json.loads(x) return parsedResult['result'] def CountFirewallRules(self): command = "http://%s:8080/wm/firewall/rules/json"; % self.controllerIp x = urllib.urlopen(command).read() return x def CountFlowRules(self): command = "http://%s:8080/wm/core/switch/%s/flow/json"; % (self.controllerIp, self.switch) x = urllib.urlopen(command).read() parsedResult = json.loads(x) content = parsedResult['00:00:00:00:00:00:00:01'] if content is None: return "[]" else: return str(content) def SetPrecondition(self): command = "http://%s:8080/wm/firewall/module/enable/json"; % self.controllerIp urllib.urlopen(command).read() # cleanup all Firewall rules command = "http://%s:8080/wm/firewall/rules/json"; % self.controllerIp x = urllib.urlopen(command).read() parsedResult = json.loads(x) for i in range(len(parsedResult)): params = "{\"ruleid\":\"%s\"}" % parsedResult[i]['ruleid'] command = "/wm/firewall/rules/json" url = "%s:8080" % self.controllerIp connection = httplib.HTTPConnection(url) connection.request("DELETE", command, para
Python unittesting method call issue
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. 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Syntax error in python unittest script
On Wednesday, September 24, 2014 1:33:35 PM UTC-6, Milson Munakami wrote: > Hi, > > > > I am learning to use unittest with python and walkthrough with this example > > http://agiletesting.blogspot.com/2005/01/python-unit-testing-part-1-unittest.html > > > Thanks > so my test script is like this: > > 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 testFirewall( unittest.TestCase ): > > def setUp(self): > > """ > > > > set up data used in the tests. > > > > setUp is called before each test function execution. > > > > """ > > > > self.controllerIp="127.0.0.1" > > self.switches = ["00:00:00:00:00:00:00:01"] > > self.startTime_ = time.time() > > self.failed = False > > self.reportStatus_ = True > > self.name_ = "Firewall" > > self.log = logging.getLogger("unittest") > > > > 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 testStatusFirewall(self): > > command = "http://%s:8080/wm/firewall/module/status/json"; % > self.controllerIp > > x = urllib.urlopen(command).read() > > parsedResult = json.loads(x) > > return parsedResult['result'] > > > > > > def suite(): > > > > suite = unittest.TestSuite() > > > > suite.addTest(unittest.makeSuite(testFirewall)) > > > > return suite > > > > if __name__ == '__main__': > > logging.basicConfig(filename='/tmp/testfirewall.log', > level=logging.DEBUG, > > format='%(asctime)s %(levelname)s %(name)s %(message)s') > > logger=logging.getLogger(__name__) > > > > suiteFew = unittest.TestSuite() > > > > suiteFew.addTest(testFirewall("testStatusFirewall")) > > > > unittest.TextTestRunner(verbosity=2).run(suiteFew) > > > > #unittest.main() > > > > #unittest.TextTestRunner(verbosity=2).run(suite()) > > > > > > while running it in console using python .py > > > > It gives me errror > > > > File "TestTest.py", line 44 > > def cleanup(self, success): > > ^ > > SyntaxError: invalid syntax > > > > I guess it is due to time module but as you can see I already had import time. > > > > what can be the reason if I comment those line containing the time it works. > > > > But i need to keep track of duration > > > > Please help and suggest. > > > > Thanks, > > Milson -- https://mail.python.org/mailman/listinfo/python-list
Syntax error in python unittest script
Hi, I am learning to use unittest with python and walkthrough with this example http://agiletesting.blogspot.com/2005/01/python-unit-testing-part-1-unittest.html so my test script is like this: 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 testFirewall( unittest.TestCase ): def setUp(self): """ set up data used in the tests. setUp is called before each test function execution. """ self.controllerIp="127.0.0.1" self.switches = ["00:00:00:00:00:00:00:01"] self.startTime_ = time.time() self.failed = False self.reportStatus_ = True self.name_ = "Firewall" self.log = logging.getLogger("unittest") 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 testStatusFirewall(self): command = "http://%s:8080/wm/firewall/module/status/json"; % self.controllerIp x = urllib.urlopen(command).read() parsedResult = json.loads(x) return parsedResult['result'] def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(testFirewall)) return suite if __name__ == '__main__': logging.basicConfig(filename='/tmp/testfirewall.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s %(message)s') logger=logging.getLogger(__name__) suiteFew = unittest.TestSuite() suiteFew.addTest(testFirewall("testStatusFirewall")) unittest.TextTestRunner(verbosity=2).run(suiteFew) #unittest.main() #unittest.TextTestRunner(verbosity=2).run(suite()) while running it in console using python .py It gives me errror File "TestTest.py", line 44 def cleanup(self, success): ^ SyntaxError: invalid syntax I guess it is due to time module but as you can see I already had import time. what can be the reason if I comment those line containing the time it works. But i need to keep track of duration Please help and suggest. Thanks, Milson -- https://mail.python.org/mailman/listinfo/python-list