How to pass variable to test class

Discussion in 'Python' started by Podi, Apr 16, 2006.

  1. Podi

    Podi Guest

    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
     
    Podi, Apr 16, 2006
    #1
    1. Advertising

  2. Podi

    Peter Otten Guest

    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
     
    Peter Otten, Apr 17, 2006
    #2
    1. Advertising

  3. Podi

    Podi Guest

    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
     
    Podi, Apr 20, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    1
    Views:
    668
    Chris
    May 11, 2006
  2. Edvard Majakari
    Replies:
    4
    Views:
    686
    Edvard Majakari
    Feb 25, 2005
  3. Tricky
    Replies:
    0
    Views:
    580
    Tricky
    Mar 19, 2009
  4. Chris

    how to pass a variable from test.aspx.vb to test.aspx

    Chris, May 11, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    570
  5. timr
    Replies:
    2
    Views:
    165
Loading...

Share This Page