I need to test a python script script1.py
in which it imports my another class
from lib.utils.ClassA import ClassA
lib = ClassA(xxxxxx)
lib.main()
Inside ClassA
(ClassA.py) it imports python package pyspark.sql.SparkSession
from pyspark.sql import SparkSession
def main(self):
self.create_table(xxx)
Here is my test:
@patch("lib.utils.LibSparkSession.SparkSession")
def test_self(self, mock_spark):
# arrange
mock_spark.side_effect = mock_spark_session
lib = ClassA("ref")
lib.create_mapping_table = MagicMock()
# action
lib.main()
# assert
xxxxxxxx
For this line:
mock_spark.side_effect = mock_spark_session
I tried to replace it with this:
mock_spark = mock_spark_session
It also works.
Could someone explain why it works in both ways and which one is preferred?