如果您使用 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))
最佳答案
使用 sys
和 getopt
模块解析命令行选项和参数,允许您使用短选项和长选项(例如 -x 和 --long-option) .
关于python - 有没有一种方法可以将我自己的 CLI 开关传递给单元测试文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56031735/