2017-06-20 59 views
0

我有一个项目,我们说叫'my_project'结构如下:包含在config.jsontests.py蟒蛇TOX - 没有这样的文件或目录错误

my_project 
| 
tox.ini 
setup.py 
src 
| 
    client.py 
    server.py 
    config.json 
    # other files 
    tests.py 

在文件client.pyserver.py利用信息定义的类实现py.test测试文件夹src中的每个文件。客户端/服务器文件里面我读的配置文件如下:

with open('./config.json') as infile: 
    self.parameters = json.load(infile) 

接下来,我创建了一个tox.ini文件,如下所示:

[tox] 
envlist = py27,cov 

[testenv] 
#commands = py.test --cov=src/client.py src/tests.py -v 
commands = py.test -sv --doctest-modules src/__init__.py src/tests.py 


[testenv:py27] 
commands = coverage erase 
      coverage run --source=src -m pytest 
      coverage report -m 
deps = pytest 

但是当我运行tox命令我得到一个错误:"No such file or directory: 'config.json'"。我如何设置正确的路径以确保tox可以找到所有必需的文件?

回答

2

试图打开'./config.json'将始终使Python在进程的当前目录中查找;这就是文件路径的工作方式。如果你要打开的文件config.json在同一目录.py文件正在做开,你需要包括含有.py文件的目录的路径:

with open(os.path.join(os.path.dirname(__file__), 'config.json')) as infile: 
相关问题