Py学习  »  Python

如何断言一个方法是从Python中的另一个复杂方法调用的?

James Lin • 2 年前 • 775 次点击  

正如标题所示,我正在向现有的不太适合测试的代码添加一些测试,我需要测试复杂的方法是否真的调用了另一个方法,例如。

class SomeView(...):
    def verify_permission(self, ...):
        # some logic to verify permission
        ...

    def get(self, ...):
        # some codes here I am not interested in this test case
        ...
        
        if some condition:
            self.verify_permission(...)
        
        # some other codes here I am not interested in this test case
        ...        

我需要写一些测试用例来验证 self.verify_permission 在满足条件时调用。

我需要一直嘲笑到 self.verify_permission 被执行?或者我需要重构 def get() 函数来抽象出代码以变得更便于测试?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/161231
 
775 次点击  
文章 [ 1 ]  |  最新文章 2 年前