473,569 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass variable to test class

Hi,

Newbie question about unittest. I am having trouble passing a variable
to a test class object.

MyCase class will potentially have many test functions.

Any help would be much appreciated.

Thanks,
P

# File MyCase.py
import unittest

class MyCase(unittest .TestCase):
def __init__(self, value):
super(MyCase, self).__init__( )
self.value = value
def test1(self):
print self.value
def test2(self):
print 'world'

if __name__ == '__main__':
msg = 'Hello'
myCase = MyCase(msg)
suite = unittest.TestSu ite()
suite.addTest(m yCase)
unittest.TextTe stRunner(verbos ity=2).run(suit e)
D:\MyWorks>MyCa se.py
Traceback (most recent call last):
File "D:\MyWorks\MyC ase.py", line 14, in ?
myCase = MyCase(msg)
File "D:\MyWorks\MyC ase.py", line 5, in __init__
super(MyCase, self).__init__( )
File "C:\Python24\li b\unittest.py", line 208, in __init__
raise ValueError, "no such test method in %s: %s" % \
ValueError: no such test method in <class '__main__.MyCas e'>: runTest

Apr 16 '06 #1
2 3846
Podi wrote:
Newbie question about unittest. I am having trouble passing a variable
to a test class object.

MyCase class will potentially have many test functions.


By default a unittest.TestCa se has only one test function called "runTest".
Therefore you have to add multiple instances of your TestCase subclass to
the suite and to pass the test function's name to the initializer
explicitly:

import unittest

class MyTestCase(unit test.TestCase):
def __init__(self, testname, value):
super(MyTestCas e, self).__init__( testname)
self.value = value
def test1(self):
pass
def test2(self):
pass

if __name__ == "__main__":
value = 42

suite = unittest.TestSu ite()
suite.addTest(M yTestCase("test 1", value))
suite.addTest(M yTestCase("test 2", value))

unittest.TextTe stRunner(verbos ity=2).run(suit e)

However, the standard place for common setup is in the setUp() method.

Peter

Apr 17 '06 #2
Thanks for replying.

I need to pass some external values to the test cases because I want to
run the same tests in different environments such as lab/instrument
setup.

Regards,
Podi

Apr 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6622
by: Harri Pesonen | last post by:
How do I pass the calling C++ class reference (or anything) to a callback? My code is: static PyObject* emb_Set(PyObject *self, PyObject *args) { char *key, *value; if(!PyArg_ParseTuple(args, "ss", &key, &value)) return NULL; // do something with the C++ class here
10
2054
by: Doug Jordan | last post by:
I am fairly new to Python. This should be an easy answer but I cannot get this to work. The code is listed below. I know how to do this in C, Fortran, and VB but it doesn't seem to work the same way here. I would appreciate any help. #try this to pass a list to a function and have the function return #a variable #this works list= def...
21
3280
by: vmsgman | last post by:
Here is a code sample ... int blah = ReadFile( defArray, defFileName, w, h); // Read File Contents into memory array and return for processing public int ReadFile( ref ushort nArray, string sFname, int w, int h) { FileStream fs = new FileStream(sFname, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); // Read data
2
19841
by: John Kelsey | last post by:
I am an old, longtime C programmer surprised and confused by an error message I'm getting from my VS2005 compiler... "Cannot pass 'Item' as a ref or out argument because it is a 'foreach iteration variable'" is the error message I'm getting for code that I think should work. I'm sure that there is some sublety I'm missing... maybe with the...
3
1128
by: lazzypink | last post by:
hi mate! i'm showing just a part of my source code. i need to pass the 'GetSeconds' variable to my SqlDB. can somebody show me how to pass it? what should i need to add in? thanks for the kindly help.. namespace test { public partial class test : Form { private long GetSeconds(int hour, int minutes) { ...
2
1877
by: lazzypink | last post by:
hi mate! i'm showing just a part of my source code. i need to pass the 'GetSeconds' variable to my SqlDB. can somebody show me how to pass it? what should i need to add in? thanks for the kindly help.. namespace test { public partial class test : Form { private long GetSeconds(int hour, int minutes) { ...
12
3000
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables....
12
11039
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed!...
5
13815
by: James | last post by:
Hello, I am a beginner in C# programming and I just want to know how we can pass a datetime variable in a method. can anyone please help me regarding this. Thanks, James.
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.