0

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.

lhbf
  • 21
  • 4
  • You're probably looking for [`import json`](https://docs.python.org/3/library/json.html) – m13op22 May 02 '19 at 15:08
  • You need to specify an absolute path, not a relative one. ``__file__`` is the absolute path to a module. – MisterMiyagi May 02 '19 at 15:11
  • Possible duplicate of [How to properly determine current script directory?](https://stackoverflow.com/questions/3718657/how-to-properly-determine-current-script-directory) – MisterMiyagi May 02 '19 at 15:12
  • import json does not help. – lhbf May 02 '19 at 15:15
  • @MisterMiyagi, I commented out top 3 lines from test_main.py, but I get the same error. – lhbf May 02 '19 at 15:15
  • @MisterMiyagi, I already had the absolute path in my test_main.py file. I used it successfully to import somemodule.py, where somemodule.py does not call an external file. – lhbf May 02 '19 at 15:19
  • You need the absolute path in main.py for opening config.json! – MisterMiyagi May 02 '19 at 15:56

1 Answers1

0

Assuming you have a sample data json like below:

{ "url" : "http://www.example.com",
"username": "user1"

}

you can use the below code to extract data from json file:

with open('path /example.json') as json_file:
                    data = json.load(json_file)

username = data['username']



Seema Nair
  • 155
  • 7