I am new to pytest testing and trying to test a python module main.py which includes classes and functions, and which also reads a static config.json file. When I import the main module for my pytest and run my test, I get an error that the json file cannot be found. I am looking to run a simple test again the classes I defined in main.py and do not need the config.json data.
I've tried placing my pytest in different parts of the project directory, but it does not help. I have also tried creating a package from my program, including a reference to the config.json file in a MANIFEST.in file, and my config.json file was installed with my package, but I still got the same problem where the config file could not be found.
My directory
Project
src
code
main.py
config.json
tests
test_main.py
my pytest test_main.py:
import sys, os
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, myPath + '/../')
import pytest
from main import Reservation
@pytest.mark.runme
def test_reservation():
starttime = "2019-05-03 12:30"
booktime = "2019-05-02 8:00"
newreservation = Reservation("002", "manicure", "normal", starttime, booktime, 1, 30)
assert newreservation.get_serv_name() == "manicure"
from main.py at line 436:
with open('config.json', 'rt') as fin:
config = json.load(fin)
business_name = config['business_name']
I expect to be able to import the Reservation class from main.py and run a test against it, but I get an error the config.json file cannot be found:
pytest error:
==================================== ERRORS ====================================
___________ ERROR collecting src/MiYE/tests/test_u01_load_clients.py ___________
test_main.py:6: in <module>
from main import Reservation
../main.py:436: in <module>
with open('config.json', 'rt') as fin:
E FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
=============================== warnings summary ===============================
btw, I can access the file config.json successfully when I run main.py as a regular python file (not an imported module). Also, the problem persists when the test is moved from /test to the code directory.