473,503 Members | 1,656 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.TestSuite()
suite.addTest(myCase)
unittest.TextTestRunner(verbosity=2).run(suite)
D:\MyWorks>MyCase.py
Traceback (most recent call last):
File "D:\MyWorks\MyCase.py", line 14, in ?
myCase = MyCase(msg)
File "D:\MyWorks\MyCase.py", line 5, in __init__
super(MyCase, self).__init__()
File "C:\Python24\lib\unittest.py", line 208, in __init__
raise ValueError, "no such test method in %s: %s" % \
ValueError: no such test method in <class '__main__.MyCase'>: runTest

Apr 16 '06 #1
2 3836
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.TestCase 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(unittest.TestCase):
def __init__(self, testname, value):
super(MyTestCase, self).__init__(testname)
self.value = value
def test1(self):
pass
def test2(self):
pass

if __name__ == "__main__":
value = 42

suite = unittest.TestSuite()
suite.addTest(MyTestCase("test1", value))
suite.addTest(MyTestCase("test2", value))

unittest.TextTestRunner(verbosity=2).run(suite)

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
6618
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,...
10
2048
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...
21
3255
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...
2
19814
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...
3
1126
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...
2
1873
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...
12
2994
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....
12
11005
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....
5
13791
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
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
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...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4998
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...
0
3160
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...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
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...

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.