Adding support to execute with arguments (#2)
* Adding support for executing with arguments * Fixing new line at the end * Added a seperate test * updated documentation Co-authored-by: Michael Grandel <mgrandel@microsoft.com>
This commit is contained in:
Родитель
d1fd7d64f9
Коммит
960ddccc3e
|
@ -5,7 +5,10 @@ All notable changes to this project will be documented in this file.
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
## [0.2.0] - 2022-05-17
|
||||
|
||||
### Added
|
||||
- `execute` now allows passing down arguments to the executing function call: `execute(function, *args, **kwargs)`
|
||||
|
||||
## [0.1.0] - 2022-03-23
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ imocker.reset_mocks()
|
|||
This is useful when the code to execute will perform an inline `import`.
|
||||
|
||||
```py
|
||||
imocker.execute(lambda: function_that_calls_inline_import())
|
||||
imocker.execute(lambda: function_that_calls_inline_import(x, y, z=4))
|
||||
```
|
||||
|
||||
## Rationale
|
||||
|
@ -183,10 +183,11 @@ imported modules can be mocked again.
|
|||
Uses the same logic of `import_module` but receives a list of module names to
|
||||
import and returns a list with the imported modules in the same order.
|
||||
|
||||
### `execute(function)`
|
||||
### `execute(function, *args, **kwargs)`
|
||||
|
||||
Executes a function inside a context that returns the mocked modules when they
|
||||
are imported, all other imports will work normally.
|
||||
are imported, all other imports will work normally. `*args` and `**kwargs` are the
|
||||
arguments to pass down to `function`.
|
||||
|
||||
This is useful when you are testing code that has `import` statements inside
|
||||
a function, and you want to mock those imports.
|
||||
|
|
|
@ -51,7 +51,7 @@ class ImportMocker(object):
|
|||
|
||||
return imported_modules
|
||||
|
||||
def execute(self, function):
|
||||
def execute(self, function, *args, **kwargs):
|
||||
"""
|
||||
Executes a function inside a context that returns the mocked modules
|
||||
when they are imported, all other imports will work normally.
|
||||
|
@ -64,7 +64,7 @@ class ImportMocker(object):
|
|||
executing the function.
|
||||
"""
|
||||
with mock.patch("builtins.__import__", side_effect=self._import_mock):
|
||||
function()
|
||||
function(*args, **kwargs)
|
||||
|
||||
def get_mocks(self):
|
||||
"""
|
||||
|
|
|
@ -12,4 +12,8 @@ def function_a_that_calls_c():
|
|||
|
||||
def function_a_that_imports_and_calls_d():
|
||||
import module_d
|
||||
module_d.function_d()
|
||||
module_d.function_d()
|
||||
|
||||
def function_a_that_imports_and_calls_e_(x,y,z):
|
||||
import module_e
|
||||
module_e.function_e(x+y+z)
|
|
@ -4,4 +4,4 @@ def function_b():
|
|||
print("function_b from module_b was called!")
|
||||
|
||||
def function_b_that_calls_c():
|
||||
module_c.function_c()
|
||||
module_c.function_c()
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
def function_c():
|
||||
print("function_c from module_c was called!")
|
||||
print("function_c from module_c was called!")
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
def function_d():
|
||||
print("function_d from module_d was called!")
|
||||
print("function_d from module_d was called!")
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
def function_e(num):
|
||||
print(f"function_e({num}) from module_e was called!")
|
|
@ -126,13 +126,14 @@ def test_can_reset_all_mocks():
|
|||
|
||||
def test_can_execute_code_on_dynamically_imported_mock():
|
||||
# Arrange
|
||||
imocker = ImportMocker(["module_b", "module_c", "module_d"])
|
||||
imocker = ImportMocker(["module_b", "module_c", "module_d", "module_e"])
|
||||
module_a = imocker.import_module("module_a")
|
||||
|
||||
# Act
|
||||
module_a.function_a_that_calls_b()
|
||||
module_a.function_a_that_calls_c()
|
||||
imocker.execute(lambda: module_a.function_a_that_imports_and_calls_d())
|
||||
imocker.execute(lambda: module_a.function_a_that_imports_and_calls_e(1, 2, z=3))
|
||||
|
||||
# Assert
|
||||
# Verify mocks are executed correctly
|
||||
|
@ -145,3 +146,7 @@ def test_can_execute_code_on_dynamically_imported_mock():
|
|||
# The module_d mock should have been imported dynamically when using execute()
|
||||
module_d = imocker.get_mock("module_d")
|
||||
module_d.function_d.assert_called_once()
|
||||
|
||||
# The module_e mock should have been imported dynamically when using execute()
|
||||
module_e = imocker.get_mock("module_e")
|
||||
module_e.function_e.assert_called_once_with(1+2+3)
|
||||
|
|
Загрузка…
Ссылка в новой задаче