简体   繁体   English

在子进程中调用的Python模拟函数

[英]Python mock function called in subprocess

Having following file handler.py 具有以下文件handler.py

import job

def worker():
   return job()

And following test test_handler.py 然后测试test_handler.py

import subprocess

def test_worker():
     subprocess.Popen(['./handler.py'], stderr=subprocess.PIPE)        

How can I mock job function, taking into account that handler.py is called in subprocess 考虑到subprocess handler.py中调用了handler.py ,我该如何模拟job功能

This isn't the best approach to test this feature. 这不是测试此功能的最佳方法。

Since it is a unit test, you should test the worker and check it's value. 由于这是一个单元测试,因此您应该测试该工作程序并检查其值。

handler.py handler.py

import job

def worker():
    return job()

test_handler.py test_handler.py

import unittest
from handler import worker

class TestHandlerCase(unittest.TestCase):

    def test_worker(self):
        """Test call worker should succeed."""
        self.assertEqual(worker(), True)

if __name__ == '__main__':
    unittest.main()

Check if the result of method is equal to the expected. 检查方法的结果是否等于预期的结果。 And so, adjust the method/test. 因此,调整方法/测试。

Anyway, this is an example, based on you question code. 无论如何,这是一个示例,基于您的问题代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM