[英]Mocking NamedTemporaryFile in Python Unnittest
I have a function like below:我有一个 function 如下所示:
def my_funct(configuration, **context):
my_file_path = os.path.join(FILE_PATH, configuration["id"])
f = NamedTemporaryFile("w", delete=False, dir=my_file_path)
formatted_data_output_file = f.name
f.close()
task_instance.xcom_push(key="formatted_data_output_file",value=formatted_data_output_file)
I am trying to test the xcom_push key/value but I can't figure out how to mock the NamedTemporaryFile我正在尝试测试 xcom_push 键/值,但我不知道如何模拟 NamedTemporaryFile
I have the following test:我有以下测试:
@patch("my_script.NamedTemporaryFile")
def test_xcom_push_vlaue(self, mock_tmp_file):
dag = DAG(dag_id="mock_dag", start_date=now())
task = DummyOperator(
dag=dag,
task_id="test_task",
)
mock_task_instance = TaskInstance(
task,
execution_date=now(),
)
context = {
"dag": dag,
"task_instance": mock_task_instance,
}
mock_tmp_file.return_value.__enter__.return_value.name = "name"
with patch.object(
mock_task_instance, "xcom_pull", side_effect=[self.configuration]
):
with patch.object(mock_task_instance, "xcom_push") as mock_xcom_push:
my_funct(configuration = {}, **context)
mock_xcom_push.assert_called_once_with(
key="formatted_data_output_file",
value="/my/file/path/%s" % self.configuration["id"] + "/name"
)
I get the following error when running the test: TypeError: expected str, bytes or os.PathLike object, not MagicMock
I am new to mocking an having trouble figuring it out, any help would be appriciated.运行测试时出现以下错误: TypeError: expected str, bytes or os.PathLike object, not MagicMock
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
p = <MagicMock name='NamedTemporaryFile().name'id='139929782915024'>
def dirname(p):"""Returns the directory component of a pathname"""
> p = os.fspath(p)
E TypeError: expected str, bytes or os.PathLike object, not MagicMock
/usr/local/lib/python3.7/posixpath.py:156: TypeError
-------------- generated xml file: /opt/airflow/test_results.xml ---------------
Why are you patching the __enter__
?你为什么要修补__enter__
? You're not using a with
statement.您没有使用with
语句。
Try replacing this line:尝试替换此行:
mock_tmp_file.return_value.__enter__.return_value.name = "name"
With this:有了这个:
mock_tmp_file.return_value.name = "name"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.