setUpClass and multiple processes not working

405 views
Skip to first unread message

denarced

unread,
Aug 3, 2010, 6:16:30 AM8/3/10
to nose-users
I noticed that when using setUpClass, nose forgets about running
several processes.
Here's my piece of code:

----------------- code begin -----------------
#!/usr/bin/python

import unittest
import time

class my_tester(unittest.TestCase):
@classmethod
def setUpClass(cls):
print "class setup"

def setUp(self):
print "setting it up"

def test_1(self):
for ind in range(1,5):
print "hau",1
time.sleep(1)

def test_2(self):
for ind in range(1,5):
print "hau",2
time.sleep(1)

def test_3(self):
for ind in range(1,5):
print "hau",3
time.sleep(1)

def test_4(self):
for ind in range(1,5):
print "hau",4
time.sleep(1)

def test_5(self):
for ind in range(1,5):
print "hau",5
time.sleep(1)
----------------- code end -----------------

I ran this with the following command:
nosetests --processes=2 -s
The -s option is just so it doesn't suppress the prints in the tests.
Here's the output:

----------------- output begin -----------------
class setup
setting it up
hau 1
hau 1
hau 1
hau 1
setting it up
hau 2
hau 2
hau 2
hau 2
setting it up
hau 3
hau 3
hau 3
hau 3
setting it up
hau 4
hau 4
hau 4
hau 4
setting it up
hau 5
hau 5
hau 5
hau 5
----------------- output end -----------------

You can clearly see that it's running just one process.
If I remove the setUpClass, it's acting like it should..

Anyone know why ?

Nat Williams

unread,
Aug 3, 2010, 9:48:05 AM8/3/10
to nose-...@googlegroups.com
On Tue, Aug 3, 2010 at 5:16 AM, denarced <dena...@gmail.com> wrote:

You can clearly see that it's running just one process.
If I remove the setUpClass, it's acting like it should..

Anyone know why ?


Nose doesn't know if the setup fixture on my_tester can be safely re-run in every process or not, so it lumps the whole context together in one process.  In this case, since setUpClass is safe to re-run (or "re-entrant", as it is called), you can tell nose that by setting _multiprocess_can_split_ = True on the class like so:


class my_tester(unittest.TestCase):
   _multiprocess_can_split_ = True
   @classmethod
   def setUpClass(cls):
       print "class setup"

The docstring for nose.plugins.multiprocess might explain it better if that's not clear.

Nat

denarced

unread,
Aug 4, 2010, 8:34:51 AM8/4/10
to nose-users
This did the trick .. the wrong trick :)
But it was enough to google and I found what I was looking for:
_multiprocess_shared_ = True
In my case the setUpClass sets it up for everyone and it needs to be
run just once for everyone.
Thanks!

On Aug 3, 4:48 pm, Nat Williams <nat.willi...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages