简体   繁体   English

模拟python库或包装器方法

[英]mocking python libraries or wrapper method

In my code release_bundler.py file 在我的代码中release_bundler.py文件

class MavenDependenciesPathsBuilderStrategy(PathsBuilderStrategy):

  def build_bundler_copy_paths(self, mapping_values):
    source = join(getcwd(),'target','dependency',mapping_values[0])
    destination = join(getcwd(),'target','generated-resources',mapping_values[1],mapping_values[0])
    return [source, destination]

class NestedModulePathsFilterStrategy(FilterStrategy):

  def filter_changeset_paths(self, changeset_paths, bundling_map, paths_builder_strategy):
    for mapping_key in bundling_map.keys():
      if(mapping_key in changeset_paths):
        mapping_values = bundling_map.get(mapping_key).values()
        copy_paths = paths_builder_strategy.build_bundler_copy_paths(mapping_values)
        return copy_paths

If I want to test the filter_changeset_paths method, I'll have to mock both getcwd method inside the build_bundler_copy_paths method or mocking only the latter will do? 如果我想测试filter_changeset_paths方法,我将不得不在build_bundler_copy_paths方法内部模拟两个getcwd方法,或者仅模拟后者使用吗?

I tried mocking the method in my tests release_bundler_test.py , importing classed like this: 我尝试在测试版本release_bundler_test.py该方法,导入如下所示的类:

from release_bundler import NestedModulePathsFilterStrategy, MavenDependenciesPathsBuilderStrategy

then patch the MavenDependenciesPathsBuilderStrategy class 然后修补MavenDependenciesPathsBuilderStrategy

def mock_build_bundler_copy_paths(self, mapping_values):
  return ['/cwd/foo','/cwd/bar']

@mock.patch('release_bundler.MavenDependenciesPathsBuilderStrategy', 'build_bundler_copy_paths', mock_build_bundler_copy_paths)
  def test_nested_module_filter_changeset_paths(self):
    pc_maps = {'ProcessingComponents/ProcessContainer':['ProcessContainerInstaller-bin.zip','SERVICES/Installer'],'ProcessingComponents/DataGrid':['ProcessContainerInstaller-bin.zip','SERVICES/Installer']}
    changed_paths =  ['ProcessingComponents/ProcessContainer/ProcessContainerRuntime/main/java/com/suntecgroup/tbms/container/ContainerException.java']
    filter_test = NestedModulePathsFilterStrategy()
    result = filter_test.filter_changeset_paths(changed_paths,pc_maps, MavenDependenciesPathsBuilderStrategy())
    self.assertIsNotNone(result)
    self.assertEquals(result[0], '/cwd/foo')
    self.assertEquals(result[0], '/cwd/bar')

but I don't think this mock works because self.assertIsNotNone(result) fails 但我认为此模拟无效,因为self.assertIsNotNone(result)失败

So the questions are: 所以问题是:

  1. Am I mocking the right way? 我在嘲笑正确的方法吗? can't get my head over it 无法阻止我
  2. will mocking on the MavenDependenciesPathsBuilderStrategy method do or I have to mock the os.getcwd inside it's method also? 会在MavenDependenciesPathsBuilderStrategy方法上进行模拟吗,还是我也必须在方法内部模拟os.getcwd

My bad, my newbie python brains took a while to get a hang of things: 我的坏人,我的新手python大脑花了一段时间才掌握了很多东西:

this is how it works well 这是如何运作良好

  @mock.patch('release_bundler.MavenDependenciesPathsBuilderStrategy.build_bundler_copy_paths')
  def test_filters_pc_from_changeset_paths(self, mock_build_bundler_copy_paths):
    pc_maps = {'ProcessingComponents/ProcessContainer':['ProcessContainerInstaller-bin.zip','SERVICES/Installer']}
    changed_paths =  ['ProcessingComponents/ProcessContainer/ProcessContainerRuntime/main/java/com/suntecgroup/tbms/container/ContainerException.java']
    with mock.patch('release_bundler.NestedModulePathsFilterStrategy.do_copy') as mock_do_copy: 
      mock_do_copy.return_value = ''
      filter_test = NestedModulePathsFilterStrategy()
      filter_test.filter_changeset_paths(changed_paths,pc_maps, MavenDependenciesPathsBuilderStrategy())
      mock_build_bundler_copy_paths.assert_called_with(['ProcessContainerInstaller-bin.zip','SERVICES/Installer'])
      mock_do_copy.assert_called()

I might be wrong but I think we cant apply multiple decorators to a method? 我可能是错的,但我认为我们不能将多个装饰器应用于一个方法?

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

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