Python unit test that uses an external data file

前端 未结 4 1375
执念已碎
执念已碎 2021-02-06 22:29

I have a Python project that I\'m working on in Eclipse and I have the following file structure:

/Project
    /projectname
        module1.py
        module2.py          


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 23:09

    Usually what I do is define

    THIS_DIR = os.path.dirname(os.path.abspath(__file__))

    at the top of each test module. Then it doesn't matter what working directory you're in - the file path is always the same relative to the where the test module sits.

    Then I use something like this is in my test (or test setup):

    my_data_path = os.path.join(THIS_DIR, os.pardir, 'data_folder/data.csv')
    

    Or in your case, since the data source is in the test directory:

    my_data_path = os.path.join(THIS_DIR, 'testdata.csv')
    

提交回复
热议问题