python - pytest 命令行参数:默认值取决于操作系统?
问题描述
Apytest
接受一个命令行参数,其默认值取决于操作系统。
这仅适用于值为 的 OS X file.dylib
:
def pytest_addoption(parser):
parser.addoption('--filename', type=str, default='file.dylib')
在 Windows 上,这个值应该是file.dll
,在 Linux 上libfile.so
。
有没有办法让默认值适用于所有操作系统?
解决方案
这样做:
def get_lib_name():
libnames = {'Windows': 'file.dll', 'Darwin': 'file.dylib', 'Linux': 'file.so'}
osname = platform.system()
if osname in libnames:
return libnames[osname]
else:
raise OSError('OS not supported.')
get_lib_name()
从解析器调用:
parser.addoption('--filename', type=str, default=get_lib_name() )
推荐阅读
- reactjs - 自定义钩子能否确定调用它的组件?
- angular - 如何使用角度从html中的数组中提取前两个字符?
- python - 如何每 x 秒重复调用 API 来更新坐标?
- javascript - 文件夹结构中的JS导入
- mysql - 如何在没有唯一值的两个表之间设置外键?
- reactjs - 仅当未使用 @azure/msal-react 进行身份验证时才重定向 onLoad
- javascript - 除非我刷新页面,否则 Vue data() 对象输入 v-model 不起作用
- postgresql - checkpoint_completion_target 被忽略
- python - 将两个不相等的列表压缩到字典中。如果 key 没有值,则不给出。忽略没有键的值
- vb.net - SQL Server 通配符 LIKE 搜索