如何修补未定义的变量/函数-单元测试



我目前正在编写一些数据块代码;神奇地";从其他文件导入,但是,当使用常规python进行本地测试时,将要导入的函数/变量不存在。

我已经将代码抽象为核心问题,没有数据块之类的东西。

如何修补未在文件中定义也没有导入的函数。

演示.py

def saySomething():
return sayHello()
saySomething() # test works without this line
# continue with further code execution
# ...

test_demo.py

from unittest.mock import patch
import demo
def test_fs():
with patch.object(demo, 'sayHello', return_value="hello", create=True):
assert demo.saySomething() == "hello"

每当我运行任何测试时,代码都会失败,因为导入是在测试开始之前,因此会立即评估文件,并可以看到sayHello函数没有定义。

名称错误:名称"sayHello"未定义

有什么方法可以做到这一点吗?

我们不直接接近。但作为变通方法,为了从Databricks中的不同笔记本中获得DataFrame的结果,我们可以如下所述。

记事本1

def func1(arg(:

df=df.transfomationlogic
return df

记事本2

%运行notebook1 的路径

df=func1(**dfinput**)
Here the dfinput is a df you are passing and you will get the transformed df back from func1.

最新更新