Needed class whose instances are many test cases

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sumit

    #1

    Needed class whose instances are many test cases

    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(u nittest.TestCas e):

    def __init__(self, method = 'runTests'):
    unittest.TestCa se.__init__(sel f, 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 ?

  • Chris Smith

    #2
    Re: Needed class whose instances are many test cases

    >>>>> "Sumit" == Sumit <sumit.nanda@gm ail.com> writes:

    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(u nittest.TestCas e):

    Sumit> def __init__(self, method = 'runTests'):
    Sumit> unittest.TestCa se.__init__(sel f, 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(u nittest.TestCas e):
    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

    Comment

    • Ben Finney

      #3
      Re: Needed class whose instances are many test cases

      Sumit <sumit.nanda@gm ail.com> wrote:[color=blue]
      > 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__()[/color]

      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.TestCa se methods setUp() and tearDown(), to establish
      and clear up respectively for each test.

      --
      \ "When I wake up in the morning, I just can't get started until |
      `\ I've had that first, piping hot pot of coffee. Oh, I've tried |
      _o__) other enemas..." -- Emo Philips |
      Ben Finney

      Comment

      • Roy Smith

        #4
        Re: Needed class whose instances are many test cases

        In article <dl24to$p20$1@r ose.polar.local >,
        Ben Finney <bignose+hate s-spam@benfinney. id.au> wrote:
        [color=blue]
        > 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.TestCa se methods setUp() and tearDown(), to establish
        > and clear up respectively for each test.[/color]

        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.

        Comment

        • Ben Finney

          #5
          Re: Needed class whose instances are many test cases

          Roy Smith <roy@panix.co m> wrote:[color=blue]
          > Ben Finney <bignose+hate s-spam@benfinney. id.au> wrote:[color=green]
          > > 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.TestCa se methods setUp() and
          > > tearDown(), to establish and clear up respectively for each test.[/color]
          >
          > 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.[/color]

          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.sourcefor ge.net/>

          --
          \ "I always had a repulsive need to be something more than |
          `\ human." -- David Bowie |
          _o__) |
          Ben Finney

          Comment

          • Sumit

            #6
            Re: Needed class whose instances are many test cases

            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.testsu it is goingto help me out . There __init__() can be Run
            One time and the resource can be create one time for set of tests.

            Comment

            Working...