Syntax error in python unittest script

Milson Munakami milsonmun at gmail.com
Wed Sep 24 15:33:05 EDT 2014


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 <FileName>.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




More information about the Python-list mailing list