在 Python 单元测试中模拟 NamedTemporaryFile

Mocking NamedTemporaryFile in Python Unnittest

我有如下功能:

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)

我正在尝试测试 xcom_push key/value 但我不知道如何模拟 NamedTemporaryFile

我有以下测试:

@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"
    )

我在 运行 测试时出现以下错误: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 ---------------

为什么要修补 __enter__?您没有使用 with 语句。

尝试替换此行:

mock_tmp_file.return_value.__enter__.return_value.name = "name"

有了这个:

mock_tmp_file.return_value.name = "name"