[英]Mock a shell command in a script called from Python subprocess.Popen()
[英]Python mock function called in subprocess
具有以下文件handler.py
import job
def worker():
return job()
然后測試test_handler.py
import subprocess
def test_worker():
subprocess.Popen(['./handler.py'], stderr=subprocess.PIPE)
考慮到subprocess
handler.py
中調用了handler.py
,我該如何模擬job
功能
這不是測試此功能的最佳方法。
由於這是一個單元測試,因此您應該測試該工作程序並檢查其值。
handler.py
import job
def worker():
return job()
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()
檢查方法的結果是否等於預期的結果。 因此,調整方法/測試。
無論如何,這是一個示例,基於您的問題代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.