473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Re: [unittest] Run setUp only once

On Tue, 29 Jul 2008 16:35:55 +0200, Nikolaus Rath <ni******@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(unittest.TestCase):
def setUp(self):
# do something very time consuming

def testOneThing(self):
def testADifferentThing(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(unittest.TestCase):
setUpResult = None

def setUp(self):
if self.setUpResult is None:
self.setUpResult = 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
Jul 29 '08 #1
1 3788
Jean-Paul Calderone <ex*****@divmod.comwrites:
On Tue, 29 Jul 2008 16:35:55 +0200, Nikolaus Rath <ni******@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(unittest.TestCase):
def setUp(self):
# do something very time consuming

def testOneThing(self):
def testADifferentThing(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(unittest.TestCase):
setUpResult = None

def setUp(self):
if self.setUpResult is None:
self.setUpResult = 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
Jul 29 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
5
by: paul kölle | last post by:
hi all, I noticed that setUp() and tearDown() is run before and after *earch* test* method in my TestCase subclasses. I'd like to run them *once* for each TestCase subclass. How do I do that. ...
3
by: Jean-Paul Calderone | last post by:
On Tue, 29 Jul 2008 19:26:09 +0200, Nikolaus Rath <nikolaus@rath.orgwrote: Eh sorry, you're right, the above is broken. `setUpResult` should be a class attribute instead of an instance...
0
by: Jean-Paul Calderone | last post by:
On Tue, 29 Jul 2008 20:12:14 +0200, Nikolaus Rath <nikolaus@rath.orgwrote: How is it a nasty hack? It's a straight-forward implementation of shared state, which is what you said you wanted. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.