700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python单元测试mock_Python单元测试mock 获取mocked函数的输入参数

python单元测试mock_Python单元测试mock 获取mocked函数的输入参数

时间:2023-09-15 19:01:49

相关推荐

python单元测试mock_Python单元测试mock 获取mocked函数的输入参数

您可以使用patch decorator,然后对模拟对象调用assert_called_with,如下所示:

如果您有这种结构:example.py

tests.py

lib/__init__.py

lib/event.py

而example.py的内容是:import lib

METADATA = 'metadata_example'

class Monolith(object):

def foo(self, raw_event):

action = 'action_example' # ... Parse Event

# Middle of function

lib.event.Event(METADATA, action)

# Continue on to use the build event.

而lib/event.py的内容是:class Event(object):

def __init__(self, metadata, action):

pass

tests.py的代码应该如下:import mock

import unittest

from lib.event import Event

from example import Monolith

class TestExample(unittest.TestCase):

@mock.patch('lib.event.Event')

def test_example1(self, event_mocked):

# Setup

m = Monolith()

# Exercise

m.foo('raw_event')

# Verify

event_mocked.assert_called_with('metadata_example', 'action_example')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。