我试图在子类的setUporsetUpClass方法中修补一些函数unittest.TestCase。
给定一个模块 patch_me_not.py
# patch_me_not.py
def patch_me(at):
print('I am not patched at {}.'.format(at))
def patch_me_not(at):
patch_me(at)
以下脚本产生了更多我期望的输出。
# main.py
import unittest
from unittest.mock import patch
from patch_me_not import patch_me_not
@patch('patch_me_not.patch_me', lambda x: None)
class PatchMeNotTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('I am the setUpClass.')
patch_me_not('setUpClass')
def setUp(self):
print('I am the setUp.')
patch_me_not('setUp')
def test_print(self):
print('I am the test')
patch_me_not('test_print')
if __name__ == '__main__':
unittest.main()
测试脚本输出是
I am the setUpClass.
I am not patched at setUpClass.
I am the setUp.
I am not patched at setUp.
I am the test
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
如果补丁在setUp和setUpClass.
如何在这些方法中应用模拟补丁?
慕运维8079593
相关分类