简体   繁体   English

到 function 的模拟补丁路径

[英]Mock patch path to function

Is there a more easy way to get this path when mocking functions?当 mocking 起作用时,是否有更简单的方法来获取此路径?

@mock.patch('folder1.folder2.file.class.get_some_information', side_effect=mocked_information) @mock.patch('folder1.folder2.file.class.get_some_information', side_effect=mocked_information)

I would like to have the path for the function get_some_information generated automatically.我希望自动生成 function get_some_information 的路径。 Thanks!谢谢!

Helper package to generate paths for mocking: github.com/pksol/mock_autogen#generating-the-arrange-section助手 package 为 mocking 生成路径:github.com/pksol/mock_autogen#generating-the-arrange-section

If you have the function object get_some_information , you can generate the said path by joining with a dot the object's __module__ attribute, for package name and module name, and the __qualname__ attribute, for class name and function name:如果您有 function object get_some_information ,您可以通过用点连接对象的__module__属性(package 名称和模块名称)和__qualname__属性(class 名称和 883400406:58988 名称)来生成上述路径

'.'.join(get_some_information.__module__, get_some_information.__qualname__)

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

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