python模拟补丁原始函数仍然被调用

时间:2021-04-22 15:47:34

标签: python-unittest.mock

对 python unittest 模拟有点新。我有以下代码来模拟许多函数,然后测试并调用 main()。模拟项是从 main 调用的。然而,即使 unitests 成功运行,原始函数也会被调用。为什么会发生这种情况?

    @patch('utilities.etl.load')
    @patch('utilities.etl.get_joined_data')
    @patch('utilities.etl.transform')
    @patch('utilities.etl.extract_salary')
    @patch('utilities.etl.extract')
    @patch('utilities.helper.get_spark_session')
    def test_main(self, mock_get_sparksession, mock_extract,
                  mock_salary, mock_transform,
                  mock_join, mock_load):
        mock_get_sparksession.return_value = self.spark
        mock_extract.return_value = self.test_extract
        mock_salary.return_value = self.test_salary
        mock_transform.return_value = self.test_transform
        mock_join.return_value = self.test_join
        mock_load.return_value = None

        main.main()

0 个答案:

没有答案