在方法主体python单元测试内调用skip方法

时间:2019-08-19 07:24:55

标签: python-unittest

我有一个A类,它有两个方法:


def app1():
    ----some code-----
    app2() # line 3



def app2():
    ----some code---

在为上面的类编写单元测试时,我在调用app1()方法 但我想从app1()方法中跳过调用方法app2()。

class TestController(unittest.TestCase):

    def setUp(self):
        self.app = app1() # its failing here(at line 3), because there is some DB setting inside app2() which i want to skip.

1 个答案:

答案 0 :(得分:1)

您正在谈论嘲笑

from unittest import TestCase
from unittest.mock import patch

from apps import app1


class App1Tests(TestCase):
    @patch('apps.app2')
    def test_app1(self, app2):
        app1()
        app2.assert_called_once_with()