python - 在 python 中模拟另一个模块中间函数的问题
问题描述
我的项目文件夹结构:
myproject
utilities--encrypt_decrypt_util -- encrypt()
src --sample.py
tests --sample_test2.py
src/sample.py:
from utilities.encrypt_decrypt_util import encrypt
def m1():
print('from m1 method')
encr_pwd=encrypt('SGu2s2RFT')
print(encr_pwd)
测试/sample_test2.py:
from src.sample import m1
from mock import patch
def encrypt(password):
print('its mock...')
return 'encryptedpassword'
@patch('utilities.encrypt_decrypt_util.encrypt')
def test_mytst2(mock_m2):
print('From test_mytst2 method')
mock_m2.return_value= encrypt('password')
print(m1())
实用程序/encrypt_decrypt_util.py
def encrypt(plain_text_pwd: str) -> bytes:
#some code here
return encrypted_password
当我执行pytest test_mytst2
时,该encrypt
方法应被模拟,但utils
会调用原始方法 from 。您能否帮助了解问题所在。
解决方案
推荐阅读
- python - Django 的外键错误
- string - Excel公式比较两个字符串
- amazon-web-services - EC2:是否可以运行自定义 JIRA 容器?
- javascript - 从 Firebase 网络获取数据
- java - 斯坦福 CoreNLP 服务器 - 如何从 Java 访问结果
- php - PHP未定义变量ldap
- typescript - Angular 5 HTTP 请求间隔使用 Observables,而值可能会改变
- python - ImageField 表单在 django 中不起作用
- ada - Ada:“类型访问”是什么意思?
- powershell - 查找所有包含字符串的路径