首页 > 解决方案 > pytest 命令行参数:默认值取决于操作系统?

问题描述

Apytest接受一个命令行参数,其默认值取决于操作系统。

这仅适用于值为 的 OS X file.dylib

def pytest_addoption(parser):
    parser.addoption('--filename', type=str, default='file.dylib')

在 Windows 上,这个值应该是file.dll,在 Linux 上libfile.so

有没有办法让默认值适用于所有操作系统?

标签: pythonpytest

解决方案


这样做:

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() )

推荐阅读