python - 有没有一种方法可以将我自己的 CLI 开关传递给单元测试文件?

标签 python python-3.x python-2.7 python-unittest

如果您使用 python filename.py -d abc 调用下面的程序,您将获得单元测试帮助。如果你调用 filename.py/d abc 你会得到:

AttributeError: 'module' 对象没有属性 '/d'

我想创建自己的 CLI 开关。指定配置文件或某些此类 CLI 开关。我也尝试过 --d 。是否有单元测试接受允许其他开关的方法?

import unittest

class SomeTests(unittest.TestCase):
    def test_one(self):
        theTest( 'keith' )

    def test_two(self):
        otherTest( 'keith')

if __name__ == '__main__':
    unittest.main( argv=sys.argv, testRunner = unittest.TextTestRunner(verbosity=2))

最佳答案

使用 sysgetopt 模块解析命令行选项和参数,允许您使用短选项和长选项(例如 -x 和 --long-option) .

关于python - 有没有一种方法可以将我自己的 CLI 开关传递给单元测试文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56031735/

相关文章:

python - 杀死进程不杀死子进程并且不关闭终端窗口

python - 双轴上的刻度和标签

python - 如何在 Python 3 类 __init__ 函数中使用 **kwargs?

python - 我得到一个 "TypeError: ' int' 对象没有属性 '_getitem',我不知道为什么

python - 当一条线上有多个停止点时,python何时在一条线上停止?

python - 如何按排序顺序遍历字典键

python - 将第二数据源条目与主要源进行匹配

python - 使用 homebrew 安装 pyinstaller

python - 将文件中的行拆分为字符串和十六进制并对十六进制值进行操作

python - Django ManyToMany 字段从没有 'for' 的对象中获取所有值