Running python unit-test cannot find my files in the correct import directory

This is what my code looks like in the code field below. When I run it, the results for my_dir and network_json are equal respectively:

my_dir: C: \ Users \ sepham \ My Documents \ LiClipse Workspace \ sengtool_data_funnel

network_json:

C: \ Users \ sepham \ My Documents \ LiClipse Workspace \ sengtool_data_funnel \ example_EventsOfInterestColumnAggregate.json

But my project directory structure for test_json_networks.py is as follows:

C: \ Users \ sepham \ Documents \ LiClipse Workspace \ sengtool_data_funnel \ tests \ test_networks

json_files_input = [
                'example_EventsOfInterestColumnAggregate.json',
                'example_EventsOfInterestConcatenate.json']
@pytest.mark.parametrize('network_json', json_files_input)
def test_network_jsons(network_json):

    my_dir = os.path.dirname(os.path.abspath(network_json))
    network_json = os.path.join(my_dir, network_json)

    print my_dir
    print network_json

    with open(network_json) as jdata:
        network = Network.load(json.load(jdata))

print operation results:

======================== CAPTURED OUTPUT =========================
C:\Users\sepham\My Documents\LiClipse Workspace\sengtool_data_funnel
C:\Users\sepham\My Documents\LiClipse 
Workspace\sengtool_data_funnel\example_EventsOfInterestColumnAggregate.json

The question is, which variable or environment setting do I need to configure for the Python unit test so that it can see my file in the correct directory at:

C: \ Users \ sepham \ Documents \ LiClipse Workspace \ sengtool_data_funnel \ tests \ test_networks

, IDClip LiClipse.

, python unit-test, :

E IOError: [Errno 2] No such file or directory: 'C:\\Users\\sepham\\My 
Documents\\LiClipse 
Workspace\\sengtool_data_funnel\\example_EventsOfInterestCoβ€Œβ€‹
lumnAggregate.json' 

File "C:\Users\sepham\My Documents\LiClipse 
Workspace\sengtool_data_funnel\tests\test_networks\test_jsonβ€Œβ€‹_networks.py", 
line 58 IOError

, , :

C:\Users\sepham\ \ LiClipse\sengtool_data_funnel

+4
3

, , . , IDClick.

test_name.py

β†’

(x) = ""

β†’

-3

:

:

- sengtool_data_funnel (project root)
  - tests
    - test_networks
      - test_jsonβ€Œβ€‹_networks.py
      - example_EventsOfInterestColumnAggregate.json
      - example_EventsOfInterestConcatenate.json

sengtool_data_funnel, sengtool_data_funnel/tests/test_networks, JSON. , .

, , , . Python ( , , ).

, - (, ). . , . , , sys.path.

, .

?

, , , .

sengtool_data_funnel/tests/test_networks/example_EventsOfInterestConcatenate.json example_EventsOfInterestConcatenate.json

script. Python __file__ script. .. os.path.dirname(__file__), sengtool_data_funnel/tests/test_networks. script :

my_dir = os.path.dirname(__file__)
network_json = os.path.join(my_dir, network_json)

, , ( , ).

+2

Maybe you can use the sys module in the python file test_json_networks.py at the beginning of your file

import sys
sys.path.append('C:\Users\sepham\Documents\LiClipse Workspace\sengtool_data_funnel')

This should solve your problem.

-1
source

Source: https://habr.com/ru/post/1677305/


All Articles