Question

I have a folder structure like this

App
--App
  --app.py       
--Docs
--Tests
  --test_app.py
  --sample.csv

In my test_app.py, I have a line to open the sample.csv file. When I run py.test command on the root App level. It throws errors saying that sample.csv does not exist. How should I configure py.test so that it can find my sample.csv file?

Edit:

import pandas as pd

class TestReturns:
    def test_annualized(self):
        pd.read_csv("sample.csv")
    def test_rebalancing(self):
        assert 1 == 1

Nothing fancy about the test. I am just trying out how to configure paths.

Était-ce utile?

La solution

If the csv file is always in the same directory as the test file you should use __file__, something like:

def test_annualized():
    fname = os.path.join(os.path.dirname(__file__), 'sample.csv')
    pd.read_csv(fname)

See the python datamodel docs for some scant notes on __file__.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top