Getting tests to parallelize using nose in python

2024/11/17 11:36:26

I have a directory with lots of .py files (say test_1.py, test_2.py and so on) Each one of them is written properly to be used with nose. So when I run nosetests script, it finds all the tests in all the .py files and executes them.

I now want to parallelize them so that all the tests in all .py files are treated as being parallelizable and delegated to worker processes.

It seems that by default, doing :

nosetests --processes=2 

introduces no parallelism at all and all tests across all .py files still run in just one process

I tried putting a _multiprocess_can_split_ = True in each of the .py files but that makes no difference

Thanks for any inputs!

Answer

It seems that nose, actually the multiprocess plugin, will make test run in parallel. The caveat is that the way it works, you can end up not executing test on multiple processes. The plugin creates a test queue, spawns multiple processes and then each process consumes the queue concurrently. There is no test dispatch for each process thus if your test are executing very fast, they could end up being executed in the same process.

The following example displays this beaviour:

File test1.py

import os
import unittestclass testProcess2(unittest.TestCase):def test_Dummy2(self):self.assertEqual(0, os.getpid())

File test2.py

import os
import unittestclass testProcess2(unittest.TestCase):def test_Dummy2(self):self.assertEqual(0, os.getpid())

Running nosetests --processes=2 outputs (notice the identical process id)

FF
======================================================================
FAIL: test_Dummy2 (test1.testProcess2)
----------------------------------------------------------------------
Traceback (most recent call last):File "C:\temp\test1.py", line 7, in test_Dummy2self.assertEqual(0, os.getpid())
AssertionError: 0 != 94048======================================================================
FAIL: test_Dummy1 (test2.testProcess1)
----------------------------------------------------------------------
Traceback (most recent call last):File "C:\temp\test2.py", line 8, in test_Dummy1self.assertEqual(0, os.getpid())
AssertionError: 0 != 94048----------------------------------------------------------------------
Ran 2 tests in 0.579sFAILED (failures=2)

Now if we add a sleep in one of the test

import os
import unittest
import timeclass testProcess2(unittest.TestCase):def test_Dummy2(self):time.sleep(1)self.assertEqual(0, os.getpid())

We get (notice the different process id)

FF
======================================================================
FAIL: test_Dummy1 (test2.testProcess1)
----------------------------------------------------------------------
Traceback (most recent call last):File "C:\temp\test2.py", line 8, in test_Dummy1self.assertEqual(0, os.getpid())
AssertionError: 0 != 80404======================================================================
FAIL: test_Dummy2 (test1.testProcess2)
----------------------------------------------------------------------
Traceback (most recent call last):File "C:\temp\test1.py", line 10, in test_Dummy2self.assertEqual(0, os.getpid())
AssertionError: 0 != 92744----------------------------------------------------------------------
Ran 2 tests in 1.422sFAILED (failures=2)
https://en.xdnf.cn/q/71222.html

Related Q&A

Python IDLE is not starting on Windows 7

I used to use Python 2.7 and then IDLE was working. I uninstalled it and installed Python 3.1. Right now Idle cannot launch. What should i do to get it running?NOTE: I tried c:\Python31\pythonw.exe c:…

SocketIO + Flask Detect Disconnect

I had a different question here, but realized it simplifies to this: How do you detect when a client disconnects (closes their page or clicks a link) from a page (in other words, the socket connection …

Numpy Array Broadcasting with different dimensions

I a little confused by the broadcasting rules of numpy. Suppose you want to perform an axis-wise scalar product of a higher dimension array to reduce the array dimension by one (basically to perform a …

xml filtering with python

I have a following xml document:<node0><node1><node2 a1="x1"> ... </node2><node2 a1="x2"> ... </node2><node2 a1="x1"> ... </no…

What it really is @client.event? discord.py

A few days ago I became interested in programming discord bots a bit. In the syntax of these programs I noticed a lot of unintelligible issues that I can not find an answer to. Thats why I am asking y…

How to customize virtualenv shell prompt

How do you define a custom prompt to use when activating a Python virtual environment?I have a bash script for activating a virtualenv I use when calling specific Fabric commands. I want the shell pro…

How to get the percent change of values in a dataframe while caring about NaN values?

I have the following DataFrame:Date A 2015-01-01 10 2015-01-02 14 2015-01-05 NaN 2015-01-06 …

Convert CSV to YAML, with Unicode?

Im trying to convert a CSV file, containing Unicode strings, to a YAML file using Python 3.4.Currently, the YAML parser escapes my Unicode text into an ASCII string. I want the YAML parser to export t…

Why is the divide and conquer method of computing factorials so fast for large ints? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Python calculate speed, distance, direction from 2 GPS coordinates

How do I calculate the speed, distance and direction (degrees) from 2 GPS coordinates in Python? Each point has lat, long, time.I found the Haversine distance calculation in this post:Calculate dista…