Dynamically passing variables to unittest
Hi Folks, Newbie question here. I'm trying to set up some unit testing for a database abstraction class, and the first thing I want to test is the connection parameters. So, my question is, how do I dynamically pass the variables from a list, for example to the unittest module so I can maintain the list of test cases more easily: - import DB import unittest class ConnectString(unittest.TestCase): InvalidStrings=(['pg','test','localhost','5432','test','test'] ,['pg','test','local',5432,'test','test']) def testInvalidStrings(self): """Check that invalid connect parameters raise InvalidConnectString error""" for i in InvalidStrings: self.assertRaises(DB.InvalidConnectString, DB.DB,",".join(i)) My problem is, this passes one string containing "'pg','test','localhost','5432','test','test'" rather than each one of those as variables. Any help appreciated. Thanks, Tom -- http://mail.python.org/mailman/listinfo/python-list
RE: Dynamically passing variables to unittest
Hi Peter, Yeah, you're right, the term "ConnectString" is a little confusing. Perhaps I should change that. Here's a valid call to DB: conn=DB.DB('pg','test','localhost',5432,'test','test') In the context of this unittest, a valid syntax would be (except that this unittest would fail, as this is a "good" connection: self.assertRaises(DB.InvalidConnectString, DB.DB,'pg','test','localhost',5432,'test','test') Thanks, Tom -Original Message- From: Peter Hansen [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 15, 2004 7:37 AM To: [EMAIL PROTECTED] Subject: Re: Dynamically passing variables to unittest Tom Haddon wrote: > So, my question is, how do I dynamically > pass the variables from a list, for example to the unittest module so I > can maintain the list of test cases more easily: > > - > import DB > import unittest > > class ConnectString(unittest.TestCase): > InvalidStrings=(['pg','test','localhost','5432','test','test'] > ,['pg','test','local',5432,'test','test']) > > def testInvalidStrings(self): > for i in InvalidStrings: > self.assertRaises(DB.InvalidConnectString, DB.DB,",".join(i)) > > > My problem is, this passes one string containing > "'pg','test','localhost','5432','test','test'" rather than each > one of those as variables. "As variables"? What does that mean? Can you give an example of precisely what a valid DB.DB call would look like in the real code, rather than showing an example of something that *doesn't* do what you want it to? I thought connect strings looked like "host=localhost;port=5432" and so on... equal signs and semicolons or something. -Peter -- http://mail.python.org/mailman/listinfo/python-list
RE: Dynamically passing variables to unittest
Great, works a treat. Thanks -Original Message- From: Jim Sizelove [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 15, 2004 11:28 AM To: [EMAIL PROTECTED] Subject: Re: Dynamically passing variables to unittest Tom Haddon wrote: > Hi Peter, > > Yeah, you're right, the term "ConnectString" is a little confusing. Perhaps I > should change that. > > Here's a valid call to DB: > > conn=DB.DB('pg','test','localhost',5432,'test','test') > > In the context of this unittest, a valid syntax would be (except that this > unittest would fail, as this is a "good" connection: > > self.assertRaises(DB.InvalidConnectString, > DB.DB,'pg','test','localhost',5432,'test','test') > You can try something like this (not tested): >>> InvalidStrings=(['pg','test','localhost','5432','test','test'], ['pg','test','local',5432,'test','test']) >>> for S in InvalidStrings: ... self.assertRaises(DB.InvalidConnectString, DB.DB, *S) ... Here the * operator unpacks a sequence when passed in to a function that is expecting positional arguments. See the Python Tutorial, 4.7.4 Unpacking Argument Lists for a better explanation. http://docs.python.org/tut/node6.html hth, Jim -- http://mail.python.org/mailman/listinfo/python-list
Decimal Places Incorrect
Hi Folks, When I run: print "%0.2f" % ((16160698368/1024/1024/1024),) I get 15.00 I should be getting 15.05. Can anyone tell me why I'm not? Thanks, Tom -- http://mail.python.org/mailman/listinfo/python-list