admin管理员组文章数量:1129124
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.
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.
Share Improve this question edited Jan 8 at 11:46 khelwood 58.9k14 gold badges88 silver badges115 bronze badges asked Jan 8 at 11:27 u09ju09j 111 silver badge2 bronze badges 1 |1 Answer
Reset to default 0A 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.
本文标签:
版权声明:本文标题:python - How do I mock the function in a module which is being called inside it from another function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736734797a1950195.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
from unittest.mock import patch
– Steven Commented Jan 8 at 13:31