在 python 中使用鼻子进行并行化测试

发布于 2024-09-06 14:13:01 字数 373 浏览 8 评论 0原文

我有一个包含大量 .py 文件的目录(例如 test_1.py、test_2.py 等),每个文件都经过正确编写,可以与鼻子一起使用。因此,当我运行 notests 脚本时,它会找到所有 .py 文件中的所有测试并执行它们。

我现在想要并行化它们,以便所有 .py 文件中的所有测试都被视为可并行化并委托给工作进程。

看来默认情况下,做 :

nosetests --processes=2 

根本不引入并行性,并且所有 .py 文件的所有测试仍然在一个进程中运行

我尝试在每个 .py 文件中放入 _multiprocess_can_split_ = True ,但这没有什么区别

感谢您的任何输入!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-09-13 14:13:01

看起来nose,实际上是多进程插件,将使测试并行运行。需要注意的是,按照它的工作方式,您最终可能无法在多个进程上执行测试。该插件创建一个测试队列,生成多个进程,然后每个进程同时使用该队列。每个进程都没有测试调度,因此如果您的测试执行得非常快,它们最终可能会在同一个进程中执行。

以下示例显示了此行为:

文件 test1.py

import os
import unittest

class testProcess2(unittest.TestCase):

    def test_Dummy2(self):
        self.assertEqual(0, os.getpid())

文件 test2.py

import os
import unittest

class testProcess2(unittest.TestCase):

    def test_Dummy2(self):
        self.assertEqual(0, os.getpid())

运行 notests --processes=2 输出(注意相同的进程 id)

FF
======================================================================
FAIL: test_Dummy2 (test1.testProcess2)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\temp\test1.py", line 7, in test_Dummy2
    self.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_Dummy1
    self.assertEqual(0, os.getpid())
AssertionError: 0 != 94048

----------------------------------------------------------------------
Ran 2 tests in 0.579s

FAILED (failures=2)

现在,如果我们在其中一个测试中添加睡眠,

import os
import unittest
import time

class testProcess2(unittest.TestCase):

    def test_Dummy2(self):
        time.sleep(1)
        self.assertEqual(0, os.getpid())

我们会得到(注意不同的进程 id)

FF
======================================================================
FAIL: test_Dummy1 (test2.testProcess1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\temp\test2.py", line 8, in test_Dummy1
    self.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_Dummy2
    self.assertEqual(0, os.getpid())
AssertionError: 0 != 92744

----------------------------------------------------------------------
Ran 2 tests in 1.422s

FAILED (failures=2)

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 unittest

class testProcess2(unittest.TestCase):

    def test_Dummy2(self):
        self.assertEqual(0, os.getpid())

File test2.py

import os
import unittest

class 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_Dummy2
    self.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_Dummy1
    self.assertEqual(0, os.getpid())
AssertionError: 0 != 94048

----------------------------------------------------------------------
Ran 2 tests in 0.579s

FAILED (failures=2)

Now if we add a sleep in one of the test

import os
import unittest
import time

class 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_Dummy1
    self.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_Dummy2
    self.assertEqual(0, os.getpid())
AssertionError: 0 != 92744

----------------------------------------------------------------------
Ran 2 tests in 1.422s

FAILED (failures=2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文