Re: [unittest] Run setUp only once

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jean-Paul Calderone

    Re: [unittest] Run setUp only once

    On Tue, 29 Jul 2008 16:35:55 +0200, Nikolaus Rath <nikolaus@rath. orgwrote:
    >Hello,
    >
    >I have a number of conceptually separate tests that nevertheless need
    >a common, complicated and expensive setup.
    >
    >Unfortunatel y, unittest runs the setUp method once for each defined
    >test, even if they're part of the same class as in
    >
    >class TwoTests(unitte st.TestCase):
    def setUp(self):
    # do something very time consuming
    >
    def testOneThing(se lf):
    >
    >
    def testADifferentT hing(self):
    >
    >
    >which would call setUp twice.
    >
    >
    >Is there any way to avoid this, without packing all the unrelated
    >tests into one big function?
    >
    class TwoTests(unitte st.TestCase):
    setUpResult = None

    def setUp(self):
    if self.setUpResul t is None:
    self.setUpResul t = computeIt()

    ...

    There are plenty of variations on this pattern.

    Jean-Paul

    >
    >Best,
    >
    -Nikolaus
    >
    >--
    »It is not worth an intelligent man's time to be in the majority.
    By definition, there are already enough people to do that.«
    -J.H. Hardy
    >
    PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
    >
    >--
    >http://mail.python.org/mailman/listinfo/python-list
    >
  • Nikolaus Rath

    #2
    Re: [unittest] Run setUp only once

    Jean-Paul Calderone <exarkun@divmod .comwrites:
    On Tue, 29 Jul 2008 16:35:55 +0200, Nikolaus Rath <nikolaus@rath. orgwrote:
    >>Hello,
    >>
    >>I have a number of conceptually separate tests that nevertheless need
    >>a common, complicated and expensive setup.
    >>
    >>Unfortunately , unittest runs the setUp method once for each defined
    >>test, even if they're part of the same class as in
    >>
    >>class TwoTests(unitte st.TestCase):
    > def setUp(self):
    > # do something very time consuming
    >>
    > def testOneThing(se lf):
    >>
    >>
    > def testADifferentT hing(self):
    >>
    >>
    >>which would call setUp twice.
    >>
    >>
    >>Is there any way to avoid this, without packing all the unrelated
    >>tests into one big function?
    >>
    >
    class TwoTests(unitte st.TestCase):
    setUpResult = None
    >
    def setUp(self):
    if self.setUpResul t is None:
    self.setUpResul t = computeIt()
    >
    ...
    >
    There are plenty of variations on this pattern.

    But at least this variation doesn't work, because unittest apparently
    also creates two separate TwoTests instances for the two tests. Isn't
    there some way to convince unittest to reuse the same instance instead
    of trying to solve the problem in the test code itself?


    Best,

    -Nikolaus

    --
    »It is not worth an intelligent man's time to be in the majority.
    By definition, there are already enough people to do that.«
    -J.H. Hardy

    PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

    Comment

    Working...