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

2024-01-22

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

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

似乎默认情况下,执行以下操作:

nosetests --processes=2 

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

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

感谢您的任何意见!


看起来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())

运行nosetests --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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 python 中使用鼻子进行并行化测试 的相关文章

随机推荐