python - Pytest - 如何在不设置测试的情况下从命令行传入文件以进行初步设置
问题描述
我在 pycharm 中有一个测试脚本设置,我想导入一个文件来做一些初步工作。虽然此代码在 python 中有效,但我在 pytest 中遇到错误。
parser = argparse.ArgumentParser()
parser.add_argument('file', nargs='+', help='properties file')
args_namespace = parser.parse_args()
args = vars(args_namespace)['file']
thisdir = os.path.dirname(os.path.abspath(__file__))
config = configparser.ConfigParser()
config.read(os.path.join(thisdir, args[0]))
但是,当我使用 pytest 运行它时,错误是:
错误:未找到:/path/to/file/properties(在任何 [] 中都没有名称“/path/to/file/properties.ini”)
我相信正在发生的事情是 pytest 试图将其解释为它自己的命令,而不是我传递的东西。有没有办法我可以引入文件而无需设置特定的 pytest 方法并使用常规的 python argsparse图书馆?
解决方案
推荐阅读
- javascript - ReactJs 中的 MegaMenu 使用 useState 钩子
- java - 我应该如何在 android studio 中获取颜色并比较它们?
- javascript - 异步 IIFE 后变量未定义
- html - 为什么我的 .scss“代码”在我的 html 页面中不起作用
- c++ - 防止 CMake 将库导出目录添加到可执行 RPath
- svg - bootstrap 5 svg 字体图标无法正常工作
- postgresql - Google Data Studio 不允许我使用度量字段计算唯一的布尔值
- vue.js - 从 vue.js 路由器链接中删除尾随空格
- c++ - 显示所有内容,包括来自用户输入的所述数组的修改结果
- mysql - 这是我面临的这个问题,传递给 Illuminate\Database\Connection::prepareBindings() 的参数 1 必须是数组类型,给定 null