我试图模拟parquet并断言它是用正确的路径调用的,但是在正确地模拟它时遇到了问题。如何模拟option函数以返回模拟的parquet
测试中的代码
def read_from_s3(spark, path):
return spark.read.option('mergeSchema', 'true').parquet(path)测试
import unittest
import mock
from src.read_from_s3 import read_from_s3
class TestReadFromS3(unittest.TestCase):
def test_read_from_s3__called_with_correct_params(self):
spark = mock.MagicMock()
spark.read.option = mock.MagicMock()
spark.read.option.parquet = mock.MagicMock()
path = 'my_path'
read_from_s3(spark, path)
spark.read.option.assert_called_with('mergeSchema', 'true') # this passes
spark.read.option.parquet.assert_called_with(path) # this fails测试失败
AssertionError: Expected 'parquet' to have been called once. Called 0 times.发布于 2019-10-03 14:08:26
parquet不是spark.read.option的属性;它是option返回值的属性。此外,您不需要显式地创建模拟;在模拟上的属性查找也会返回模拟。
def test_read_from_s3__called_with_correct_params(self):
spark = mock.MagicMock()
read_from_s3(spark, path)
spark.read.option.assert_called_with('mergeSchema', 'true')
spark.read.option.return_value.parquet.assert_called_with(path)https://stackoverflow.com/questions/58220655
复制相似问题