看起来鼻子,实际上是多进程插件,将使测试并行运行。需要注意的是,它的工作方式最终可能不会在多个进程上执行测试。该插件创建一个测试队列,生成多个进程,然后每个进程同时使用该队列。每个进程都没有测试分派,因此如果您的测试执行得非常快,它们最终可能会在同一个进程中执行。
以下示例显示了这种行为:
文件 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)