Reputation: 11
I am currently writing a pytest file and need some help regarding it.
In my conftest.py
I am initialising the spark session in a function let's session
def session():
spark - Spark.Session ...
My test file looks like this:
import file_to_be_tested as t
def test_func(spark):
df = spark.read
t.func1(df)
The issue is that func1
is calling another function which is also using my spark session, so how do I share my spark function with the module so that it can use it and not the spark function in the main file which is not initialised?
I tried using mock functions but wasn't able to get definitive answer on how to use it.
Upvotes: -1
Views: 43
Reputation: 608
A method to achieve this is to inject file_to_be_tested
's dependency on spark, rather than have it hidden in the implementation.
You could either inject spark dependency into the method(s) as an extra argument
import file_to_be_tested as t
....
t.func1(df, spark_session)
Or you could create a class to manage the spark session
import file_to_be_tested as t
....
spark_managing_class = t.SparkManagingClass(spark_session)
spark_managing_class.func1(df)
And then you don't need to import spark directly in file_to_be_tested
, instead always injecting the spark session.
While this sort of dependency injection is more often spoken about in statically typed languages, it is still of great use in python.
Upvotes: 0