【问题标题】:Can python nosetest and py.test frameworks run tests with multiprocessingpython nosetest 和 py.test 框架可以运行多处理测试吗
【发布时间】:2018-07-11 02:54:20
【问题描述】:

我的以下代码在作为 python unittest 运行时可以正常工作,但如果使用带有以下错误消息的 nosetest 或 py.test 则会失败:

鼻子测试错误: PicklingError: Can't pickle : it's not found as teamcity.nose_report.newCaptureBeforeTest

py.test 错误: PicklingError: Can't pickle : it's not found as __builtin__.module

代码:

import unittest
import multiprocessing as mp
import time

TIME_LIMIT = 1

class TestCases(unittest.TestCase):

    def setUp(self):
        self.a = 0

    def my_func(self):
        time.sleep(2)
        self.q.put(self.a + 1)

    def run_case(self, func):
        self.q = mp.Queue()

        test_process = mp.Process(target=func)

        test_process.start()
        test_process.join(TIME_LIMIT)

        self.assertFalse(test_process.is_alive(), 'timeout exceeded')

    def test_case1(self):
        self.run_case(self.my_func)
        self.assertEquals(self.a + 1, self.q.get())

【问题讨论】:

    标签: python-2.7 pytest python-multiprocessing nose


    【解决方案1】:

    如果被测函数不在同一个测试类中,则该问题似乎可以避免。不知道为什么,但我相信 py.test/nose 调用父类以分叉进程的方式陷入了无限循环。

    import unittest
    import multiprocessing as mp
    
    TIME_LIMIT = 1
    
    class Submission():
        def __init__(self):
            self.score = mp.Queue(1)
    
        def reset(self):
            while not self.score.empty():
                self.score.get()
    
        def run(self):
            self.score.put(0)
    
    class TestCases(unittest.TestCase):
    
        def setUp(self):
            self.func = Submission()
            self.a = 0
    
        def run_case(self):
            self.submission.reset()
    
            test_process = mp.Process(target=self.submission.run)
            test_process.start()
            test_process.join(TIME_LIMIT)
    
            self.assertFalse(test_process.is_alive(), 'timeout exceeded')
            self.assertTrue(True)
    
        def test_case1(self):
            self.run_case()
            score = self.submission.score.get()
            self.assertTrue(score >= self.a) 
    

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多