'Pytest mock pandas read_excel inside the Class Construction
I am doing some Integration tests using Pytest. I am reading my excel sheet input from the class constructor. I don't know how to mock the read_excel function of pandas. My actual code,
import pandas as pd
class Sample:
def __init__(self, input_file_path):
self.df_input = pd.read_excel(
input_file_path, sheet_name="Input sheet", header=[0]
)
def calculation(self):
pass
def choose_template(self):
pass
sample_obj = Sample('./test.xlsx')
sample_obj.choose_template()
test_one.py,
@mock.patch.dict(os.environ, {"DB_CONNECTION_STRING": "Mock_Connection_String",
"DB_DATABASE": "Mock_Db",
"AZURE_STORAGE_CONNECTION_STRING": "Mock_Azure_connection_String",
},clear=True)
def test_choose_template_valid():
expected = "fizz"
sample_obj = Sample('./test.xlsx')
sample_obj.choose_template()
actual = "fizz"
assert actual == expected
I am getting the following error. I understand why this is happening. But I need a solution on how to mockup the pd.read_excel call.
=============================================================== short test summary info ================================================================
FAILED test_report.py::test_choose_template_valid - FileNotFoundError: [Errno 2] No such file or directory: './test.xlsx'
================================================================== 1 failed in 0.96s ===================================================================
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
