首页 > 解决方案 > 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图书馆?

标签: pythonpytest

解决方案


推荐阅读