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
  • search how to use this : from unittest.mock import patch – Steven Commented Jan 8 at 13:31
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: