views:

416

answers:

2

hi

I want to add two variables to my subclass which is inherited from unittest.testcase

like I have:

import unittest

class mrp_repair_test_case(unittest.TestCase):

     def __init__(self, a=None, b=None, methodName=['runTest']):
             unittest.TestCase.__init__(self)
             self.a= a
              self.b = b

     def test1(self):
           ..........
           .......

def runtest()
    mrp_repair_test_case(a=10,b=20)
    suite = unittest.TestLoader().loadTestsFromTestCase(mrp_repair_test_case)
    res = unittest.TextTestRunner(stream=out,verbosity=2).run(suite)

how can I acvhieve this: I am getting this error:

ValueError: no such test method in ****<class 'mrp_repair.unit_test.test.mrp_repair_test_case'>:**** runTest

thanks

+3  A: 

At first glance, it looks like you need to create an instance of mrp_repair_test_case. Your current line:

mrp_repair_test_case(a=10,b=20)

doesn't actually do anything.

Try (not tested):

def runtest():
    m = mrp_repair_test_case(a=10, b=20)
    suite = unittest.TestLoader().loadsTestsFromTestCase(m)
    res = unittest.TextTestRunner(stream=out, verbosity=2).run(suite)

This assumes you've set up 'out' as a stream already.

Edit:

By the way, is there any reason you're not using a setUp method to set these values? That would be normal best practice. Looking at the documentation of loadTestsFromTestCase it looks like it will only accept the Class itself not an instance of it, which would mean you're rather working against the design of the unittest module.

Edit 2:

In response to your further information, I would actually set your uid and cursor values seperately at module level before calling the tests. I'm not a huge fan of globals normally, but if I'm understanding you correctly these values will be A) read-only B) always the same for the same customer which avoids most of the normal pitfalls in using them.

Edit 3:

To answer your edit, if you really want to use __init__ you probably can, but you will have to roll your own loadsTestsFromTestCase alternative, and possibly your own TestSuite (you'll have to check the internals of how it works). As I said above, you'll be working against the existing design of the module - to the extent that if you decide to do your testing that way it might be easier to roll your own solution completely from scratch than use unittest. Amend: just checked, you'd definately have to roll your own version of TestSuite, as the existing one creates a new instance of the TestCaseClass for each test.

mavnn
A: 

actually this is my code

import statements

class mrp_repair_test_case(unittest.TestCase):

def __init__(self, cursor=None, user=None, methodName='runTest'):
    unittest.TestCase.__init__(self,methodName)
    self.uid = user
    self.cr = cursor

def setUp(self):
    try:
        self.pool = pooler.get_pool(cr.dbname)
        self.repair_order = self.pool.get('mrp.repair')
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def tearDown(self):
    try:
        self.pool = None
        self.repair_order = None
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)


def test_1_Create(self):

    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def test_2_ConfirmRepair(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def test_3_RepairReady(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def test_4_RepairStart(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def test_4_RepairEnd(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def test_5_PrintProductionOrder(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def test_6_MakeInvoice(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)


def test_7_CancelProductionOrder(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def test_8_Unlink(self):
    try:
    except osv.except_osv,e:
        self.fail(e.name + e.value)
    except Exception,e:
        self.fail(e)

def runTest(cursor=None, user=None): global cr global uid cr = cursor uid = user out = StringIO() testmethods = unittest.TestLoader().getTestCaseNames(mrp_repair_test_case) for test in testmethods: mrp_repair_test_case(cursor=cursor,user=user,methodName=test) m = mrp_repair_test_case(cursor=cursor,user=user) suite = unittest.TestLoader().loadTestsFromTestCase(m) res = unittest.TextTestRunner(stream=out,verbosity=2).run(suite) if res.wasSuccessful(): return (True,out.getvalue()) print out.getvalue() return (res,out.getvalue())

this runTest() will be called by third party module and that third party will pass the cursor and user so what I want is this cursor and user in all my testcases. but I am not able to do so. same case is taht If we need a value of a variable in other testcase that is created in one test case we need to define that variable as global then and then we can use it but I dont want to use global I want to assign it in self using init

How can I . Thanks for your quick response

Edit:1 : thanks for your response but can I get by overriding unittest case init() if yes then how ?

Naresh
You probably want to move this information to the question as an edit.
mavnn