简体   繁体   English

使用pytest测试python脚本-权限问题

[英]Testing a python script with pytest - permission issues

I have unit tested my modules using pytest without issue. 我已经使用pytest对模块进行了单元测试,没有任何问题。 I would also like to test my script (which imports some of the modules), but I am getting a permission error. 我也想测试我的脚本(其中导入了一些模块),但是出现权限错误。 I am new to writing larger python projects that actually need to be tested and maintained, so my directory structure may be non-optimal, so please let me know if that is the case. 我是刚开始编写实际上需要测试和维护的大型python项目的新手,因此我的目录结构可能不是最佳的,所以请告诉我是否是这种情况。 My directory structure looks like - 我的目录结构看起来像-

parent
 modules
  __init__.py
  module1.py
  ...
 test
  ...
  test_script.py
 script.py

My test code: 我的测试代码:

import os
import subprocess
import train

def test_train_dataset1():
  #run the script
  p = subprocess.Popen(['./train.py', 'test_models', 'test1', 'data/temp.json', '--isfile'])
  #more code to check output

My error: 我的错误:

PermissionError: [Errno 13] Permission denied: './train.py'

Permission denied: './train.py' is most certainly mean the file is not executable. Permission denied: './train.py'最肯定意味着该文件不可执行。 To fix: 修理:

chmod a+x ./train.py

Also verify it has a proper shebang line ( #!/usr/bin/python or such). 还要验证它是否具有正确的shebang行( #!/usr/bin/python等)。

PS. PS。 When you do import train do you import from the same train.py ? import train ,您是否从同一train.py进口? If not — you shouldn't give the script the same name as a module/package you're gonna import. 如果不是,则不应为脚本提供与要导入的模块/软件包相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM