Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78305 > unrolled thread

Syntax error in python unittest script

Started byMilson Munakami <milsonmun@gmail.com>
First post2014-09-24 12:33 -0700
Last post2014-09-24 17:39 -0400
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Syntax error in python unittest script Milson Munakami <milsonmun@gmail.com> - 2014-09-24 12:33 -0700
    Re: Syntax error in python unittest script Emile van Sebille <emile@fenx.com> - 2014-09-24 12:40 -0700
    Re: Syntax error in python unittest script Peter Otten <__peter__@web.de> - 2014-09-24 21:41 +0200
    Re: Syntax error in python unittest script Milson Munakami <milsonmun@gmail.com> - 2014-09-24 13:06 -0700
      Re: Syntax error in python unittest script Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-24 21:28 +0100
    Re: Syntax error in python unittest script Terry Reedy <tjreedy@udel.edu> - 2014-09-24 17:39 -0400

#78305 — Syntax error in python unittest script

FromMilson Munakami <milsonmun@gmail.com>
Date2014-09-24 12:33 -0700
SubjectSyntax error in python unittest script
Message-ID<2c76f6c8-bb13-44e9-8da8-62a08e067b3a@googlegroups.com>
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

[toc] | [next] | [standalone]


#78307

FromEmile van Sebille <emile@fenx.com>
Date2014-09-24 12:40 -0700
Message-ID<mailman.14328.1411587685.18130.python-list@python.org>
In reply to#78305
On 9/24/2014 12:33 PM, Milson Munakami wrote:
> 	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


The method above doesn't end cleanly (you need to add a close paren to 
the last line at least)

Emile


> 	def cleanup(self, success):

[toc] | [prev] | [next] | [standalone]


#78308

FromPeter Otten <__peter__@web.de>
Date2014-09-24 21:41 +0200
Message-ID<mailman.14329.1411587688.18130.python-list@python.org>
In reply to#78305
Milson Munakami wrote:

>                 if self.reportStatus_:
>                         self.log.info("=== Test %s completed normally (%d sec)", self.name_, duration

The info() call is missing the closing parenthesis
 
>         def cleanup(self, success):
>                 sys.excepthook = sys.__excepthook__
> 

> It gives me errror   
> 
> File "TestTest.py", line 44
>     def cleanup(self, success):
>       ^
> SyntaxError: invalid syntax
 
To find a syntax error it is often a good idea to look into the code that 
precedes the line that the compiler complains about.

[toc] | [prev] | [next] | [standalone]


#78310

FromMilson Munakami <milsonmun@gmail.com>
Date2014-09-24 13:06 -0700
Message-ID<7807b4fc-fde2-41ea-a1d8-23bb93a50b8c@googlegroups.com>
In reply to#78305
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 <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

[toc] | [prev] | [next] | [standalone]


#78311

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-09-24 21:28 +0100
Message-ID<mailman.14331.1411590551.18130.python-list@python.org>
In reply to#78310
On 24/09/2014 21:06, Milson Munakami wrote:

[snipped all the usual garbage]

Would you please access this list via 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#78313

FromTerry Reedy <tjreedy@udel.edu>
Date2014-09-24 17:39 -0400
Message-ID<mailman.14333.1411594822.18130.python-list@python.org>
In reply to#78305
On 9/24/2014 3:33 PM, Milson Munakami wrote:

> I am learning to use unittest with python

[way too long example]

> File "TestTest.py", line 44
>      def cleanup(self, success):
>        ^
> SyntaxError: invalid syntax

A common recommendation is to find the *minimal* example that exhibits 
the problem.  (Start with commenting out everything after the code in 
the traceback, then at least half the code before it. Etc.) That means 
that removing or commenting out a single statememt and the problem 
disappears.  In compound statements, you may need to insert 'pass' to 
not create a new problem.

If you had done that, you would have reduced your code to something like

class testFirewall( unittest.TestCase ):
	def tearDown(self):
		if self.reportStatus_:
			self.log.info("=== Test %s completed normally (%d sec)", self.name_, 
duration
	def cleanup(self, success):
		sys.excepthook = sys.__excepthook__

At that point, replacing "self.log.info(..." with "pass" would have made 
it clear that the problem was with the replaced statement.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web