Needed class whose instances are many test cases

C

Chris Smith

Sumit> I have scinario like I have to Create resource(in
Sumit> __init__()) Before Running a set of testcases and then In
Sumit> Testcases resources are going to used and then It will
Sumit> cleared off after Running the testcases by destructor
Sumit> __del__() import unittest import time

Sumit> class app_adminfunc(unittest.TestCase):

Sumit> def __init__(self, method = 'runTests'):
Sumit> unittest.TestCase.__init__(self, method)
Sumit> #------------Resource Creation --------

Sumit> def __del__(self): #--------------Resource
Sumit> Deletion -----------------

Sumit> def test01----- def test02----- ....... ....

Sumit> But in this above code Problem is that __init__() called at
Sumit> each time when the Testcase is run ,But i want Single time
Sumit> run of the Init Prior to run of each tests inside the class
Sumit> . Can Anybody help me on this ?

The unittest module runs a setUp and tearDown before each test case.
If that is too high-frequency, why not just do something like

class app_adminfunc(unittest.TestCase):
def setUp(self): pass
def tearDown(self): pass
def allYourTestCase(self):
def test01(): pass
def test02(): pass

since python is so mellow about nesting functions and classes and
such.

HTH,
Chris
 
S

Sumit

I have scinario like I have to Create resource(in __init__()) Before
Running a set of testcases and then In Testcases resources are going to
used and then It will cleared off after Running the testcases by
destructor __del__()
import unittest
import time

class app_adminfunc(unittest.TestCase):

def __init__(self, method = 'runTests'):
unittest.TestCase.__init__(self, method)
#------------Resource Creation --------

def __del__(self):
#--------------Resource Deletion -----------------

def test01-----
def test02-----
........
.....

But in this above code Problem is that __init__() called at each time
when the Testcase is run ,But i want Single time run of the Init Prior
to run of each tests inside the class .
Can Anybody help me on this ?
 
B

Ben Finney

Sumit said:
I have scinario like I have to Create resource(in __init__()) Before
Running a set of testcases and then In Testcases resources are going
to used and then It will cleared off after Running the testcases by
destructor __del__()

This is a poor design; your tests will each be starting in a different
state, and will likely not run the same way if run in a different
order, making them fragile.

Test cases should each run individually, from a known state, and not
depend on any other tests. You can define a fixture for several tests
in the unittest.TestCase methods setUp() and tearDown(), to establish
and clear up respectively for each test.
 
R

Roy Smith

Ben Finney said:
This is a poor design; your tests will each be starting in a different
state, and will likely not run the same way if run in a different
order, making them fragile.

Test cases should each run individually, from a known state, and not
depend on any other tests. You can define a fixture for several tests
in the unittest.TestCase methods setUp() and tearDown(), to establish
and clear up respectively for each test.

In general, I certainly agree with the above. The problem is that
sometimes setup is so expensive, it becomes impractical to do a full
setup/teardown cycle for each test.
 
B

Ben Finney

Roy Smith said:
In general, I certainly agree with the above. The problem is that
sometimes setup is so expensive, it becomes impractical to do a full
setup/teardown cycle for each test.

That's what mock objects are for. The test fixture should be minimal,
having only the interface needed to test that the production code does
what it should, with almost no functionality behind that interface.

<URL:http://pmock.sourceforge.net/>
 
S

Sumit

Thanks for comments
..setup() is going the Run Before every testcase Run. But i need to
create resource for set of testcases , it is one time only . I can not
create at every instant before testcases Run . thats why
Unittest.testsuit is goingto help me out . There __init__() can be Run
One time and the resource can be create one time for set of tests.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top