简体   繁体   English

Python Unittest:确保已调用修补的类

[英]Python Unittest: make sure patched class is called

I have the following code 我有以下代码

@ddt
@patch('mymodule.myclass', MagicMock)
class MyTest(): 
    @data([val1, val2])
    @unpack 
    def test_run(self, val1, val2):
         ClassA().run(val1, val2)  

How Do I assert some methods inside mymodule.myclass are called? 我如何断言mymodule.myclass内部的某些方法被调用?

When you patch it at the class level you get a reference to the mock as an argument in your test methods, for instance: 在类级别对其进行修补时,您会在测试方法中获得对模拟的引用作为参数,例如:

@ddt
@patch('mymodule.myclass', MagicMock)
class MyTest(): 
    @data([val1, val2])
    @unpack 
    def test_run(self, val1, val2, my_class_mock):
         # use my_class_mock   

You can use this mock for many purposes but in this case you'd still need to patch your class inside the test method to make your assertions. 您可以将此模拟程序用于许多目的,但在这种情况下,您仍然需要在测试方法中修补类以进行断言。

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

相关问题 Python unittest.mock:调用了已修补的类方法,但断言失败 - Python unittest.mock: patched class method called but assertion fails 在类方法中传递的Python unittest测试用 - Python unittest test passed in class method is called with Python 单元测试:模拟外部库 function 从 class ZA8CFDE6331BD1C49EB2AC96F8691 调用 - Python unittest: Mock an external library function called from a class object 用Python修补的SIP包装的C ++类 - C++ class wrapped in SIP patched in Python 猴子修补的python类中的自引用 - self reference in monkey patched python class 修补程序-如何检查修补的类的实例(称为类函数)? - Patch - How to check that a patched class's instance called a class function? assertRaises 在 python 单元测试 class - assertRaises in python unittest class Python Linter 表示修补后的单元测试中缺少方法调用中的值,但测试运行正常。 为什么? - Python Linter says value in method call is missing in patched unittest but test runs properly. Why? 如何确保在第一次调用另一个类函数之前不会调用类函数? - How to make sure a class function wont be called until another class function has been called first? 使用Python单元测试库(unittest,mock),如何断言是否在类A的方法内调用了类B的方法? - Using Python unittesting libraries (unittest, mock), how to assert if a method of class B was called within a method of class A?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM