I have two functions in a python file. I want to do some unit tests for these functions using Mock.
def col_rename(col_name):
reps = ((' ', '_&'), ('(', '*_'), (')', '_*'), ('{', '#_'), ('}', '_#'))
new_cols = reduce(lambda a, kv: a.replace(*kv), reps, col_name)
return new_cols
def rename_characters(df):
df_cols = df.schema.names
for x in df_cols:
df = df.withColumnRenamed(x, col_rename(x))
return df
In the above function withColumnRenamed is a function in pyspark that will return a column after is renames the column name.
df is a pyspark data frame.
I am able to do unit testing to the col_rename function.
I am able to do unit testing to the rename_characters function by creating data frames manually in pyspark.
Now I want to do the unit testing using Mock in python.
I have tried something like this below. I am not sure if this is correct or What I am doing is completely wrong
import unittest
from mock import patch
class Test(unittest.TestCase):
@patch('mymodule.rename_characters')
def test_func(self, rename_characters_mock):
rename_characters_mock.return_value = 'mocked values'
self.assertEqual(return_value, 'mocked_values'))
How can I do Mocking for the unit testing as in the above scenario
from mymodule import rename_characters, r u sure we canimporta func?pycharmit gave me unused import statement error, I removed the import statementpyspark.x,self.assertEqual(return_value, 'mocked_values'))the return_value is not defined, are u try toself.assertEqual(mymodule.rename_charaters(), 'mocked_value'?self.assertEqual(mymodule.rename_charaters(), 'mocked_value'