[英]Unittest: mock a python class with init method
I am trying to test a run method in my class which has init method and takes object as parameter from another class:我正在尝试在我的 class 中测试运行方法,该方法具有 init 方法并将 object 作为另一个 class 的参数:
class ServePlate(FreeSurferStep):
process_name = "FreeSurfer"
step_name = "ServePlate"
step_cli = "serve"
cpu = 1
mem = 1024
def __init__(self, project, code, args):
super(Stage, self).__init__(project, code, args)
self.next_step = Autorecon1
@classmethod
def get_queue(cls, project_name):
plog = ProcessingLog()
available = plog.get_project_images(project_name, "T1")
attempted = plog.get_step_attempted(project_name, cls.process_name, cls.step_name)
attempted_codes = [row.Code for row in attempted]
todo = [{'ProjectName': project_name, 'Code': row.Code} for row in available if row.Code not in attempted_codes]
return todo
def run(self): #<<<<-- This method is to be tested
source = None
image = ProcessingLog().get_project_image(self.project, self.code)
if image.ImageStore == "Dicom":
dcmtmp = tempfile.mkdtemp()
DicomRepository().fetch_dicoms(self.code, dcmtmp)
first_t1 = os.path.join(dcmtmp, os.listdir(dcmtmp)[0])
niitmp = os.path.join(tempfile.mkdtemp(), 'raw.nii')
cmd = 'dcm2niix -b n -z n -g i -o {} -f raw {}'.format(os.path.dirname(niitmp), first_t1)
self._run_fs_cmd(cmd)
source = niitmp
elif image.ImageStore == "Pre-Processed":
source = [PreProcessedImageRepository().get_image(self.code), ]
if source is None:
raise ProcessingError("Could not find staging data.")
first_t1 = self._copy_files(source)
cmd = 'recon-all -s %(code)s -i %(image)s' % {
"code": self.code,
"image": first_t1
}
self._run_fs_cmd(cmd). #<<<-- I am trying to check value of cmd variable
Here is my test, i am patching first the init method and second _run_fs_cmd frm another class.这是我的测试,我首先修补init方法,然后修补另一个class的_run_fs_cmd。
class Testfs(unittest.TestCase):
@patch.object(fs.FreeSurferStep, '_run_fs_cmd', spec=True)
# @patch.object(fs.FreeSurferStep, '__init__')
def test_serve(mock_serve):
"""
Serve step test
"""
mock_serve.project = 'TEST_FS'
mock_serve.code = 'Test9001-1a5'
mock_serve.args = ''
mock_stage.return_value = None
FsObj = FreeSurferStep('serve')
stage_obj = Stage(FsObj)
FsObj.run()
#
# stage_obj.run(self)
#
# self.assertEqual(self.cmd, '')
# fs.FreeSurferStep._run_fs_cmd = Mock()
this gives me error.这给了我错误。 Here even though i am passing no arguments to the run method, it keeps on complaining about more argument being passed.
在这里,即使我没有将 arguments 传递给 run 方法,它仍然抱怨传递了更多参数。 Also patching a class object to be passed to ServePlate method and patching run_fsmethod where the cmd is passed to doesn't seem to work.
还修补 class object 以传递给 ServePlate 方法并修补run_fsmethod ,其中 cmd 传递给似乎不起作用。 Do i need to compulsorily mock all other methods being called?
我是否需要强制模拟所有其他被调用的方法?
TypeError: test_serve() takes 1 positional argument but 2 were given
TypeError: run() takes 1 positional argument but 3 were given
TypeError: test_serve() takes 1 positional argument but 2 were given
TypeError: run() takes 1 positional argument but 3 were given
i got the test working with initializing correctly:我得到了正确初始化的测试:
class Testfs(unittest.TestCase):
project = 'TEST'
code = '9001-1a5'
args = 'nogrid'
@patch.object(fs.FreeSurferStep, '_run_fs_cmd', 'put_object', spec=True)
@patch.object(fs.FreeSurferStep, '__init__')
def test_serve(self, mock_test_serve):
"""
Stage step test
"""
mock_test_stage.return_value = None
project = 'TEST'
code = '9001-1a5'
args = 'nogrid'
self.logger = logging.getLogger(__name__)
FsObj = fs.FreeSurferStep('Stage')
stage_obj = fs.Stage(FsObj, code, args)
stage_obj.project = 'Test'
stage_obj.code = '9001-1a5'
stage_obj.run()
however havent got a way to check value passed to `_run_fs_cmd` method
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.