Python 单元测试不接受传递的参数
Python unittest does not take passed parameters
我有以下代码:
class WebuiSeleniumTest(unittest.TestCase):
def setup_parser(self):
parser = argparse.ArgumentParser(description='Automation Testing!')
parser.add_argument('-p', '--platform', help='Platform for desired_caps', default='Mac OS X 10.9')
parser.add_argument('-b', '--browser-name', help='Browser Name for desired_caps', default='chrome')
parser.add_argument('-v', '--version', default='')
return parser.parse_args()
def test_parser(self):
args = self.setup_parser()
print args
if __name__ == "__main__":
unittest.main()
当我尝试在终端中使用命令 "python myfile.py -b firefox" 运行 时,我得到 AttributeError: 'module' object has no attribute 'firefox'
并生成了帮助输出。
当我隔离它并且 运行 它没有 if __name__ == "__main__"
它工作正常。为什么它会尝试在 unittest 上应用我传递的参数?我的代码中需要它作为字符串。
用 python myfile.py -b firefox
调用你的脚本确实会进入单元测试,而不是你的参数解析器。
Unittest 尝试解析您给出的参数,例如如果您这样调用脚本:
python myfile.py --help
您看到有效选项:
Usage: myfile.py [options] [test] [...]
Options:
-h, --help Show this message
-v, --verbose Verbose output
-q, --quiet Minimal output
-f, --failfast Stop on first failure
-c, --catch Catch control-C and display results
-b, --buffer Buffer stdout and stderr during test runs
Examples:
parse.py - run default set of tests
parse.py MyTestSuite - run suite 'MyTestSuite'
parse.py MyTestCase.testSomething - run MyTestCase.testSomething
parse.py MyTestCase - run all 'test*' test methods
in MyTestCase
查看帮助输出 -b
会缓冲(我想抑制?)stdout/stderr。参数 firefox
被用作模块中 运行 的测试名称。并且没有函数 method
存在,它输出这个错误:
AttributeError: 'module' object has no attribute 'firefox'
现在,您可能想要做的是调用 test_parser
,如果您使用 python myfile.py WebuiSeleniumTest.test_parser
调用,则您无法传递任何附加参数。这可能是你最后的问题。 this question 给出了一些可能的解决方案,用于测试 argparse 作为单元测试。
我有以下代码:
class WebuiSeleniumTest(unittest.TestCase):
def setup_parser(self):
parser = argparse.ArgumentParser(description='Automation Testing!')
parser.add_argument('-p', '--platform', help='Platform for desired_caps', default='Mac OS X 10.9')
parser.add_argument('-b', '--browser-name', help='Browser Name for desired_caps', default='chrome')
parser.add_argument('-v', '--version', default='')
return parser.parse_args()
def test_parser(self):
args = self.setup_parser()
print args
if __name__ == "__main__":
unittest.main()
当我尝试在终端中使用命令 "python myfile.py -b firefox" 运行 时,我得到 AttributeError: 'module' object has no attribute 'firefox'
并生成了帮助输出。
当我隔离它并且 运行 它没有 if __name__ == "__main__"
它工作正常。为什么它会尝试在 unittest 上应用我传递的参数?我的代码中需要它作为字符串。
用 python myfile.py -b firefox
调用你的脚本确实会进入单元测试,而不是你的参数解析器。
Unittest 尝试解析您给出的参数,例如如果您这样调用脚本:
python myfile.py --help
您看到有效选项:
Usage: myfile.py [options] [test] [...]
Options:
-h, --help Show this message
-v, --verbose Verbose output
-q, --quiet Minimal output
-f, --failfast Stop on first failure
-c, --catch Catch control-C and display results
-b, --buffer Buffer stdout and stderr during test runs
Examples:
parse.py - run default set of tests
parse.py MyTestSuite - run suite 'MyTestSuite'
parse.py MyTestCase.testSomething - run MyTestCase.testSomething
parse.py MyTestCase - run all 'test*' test methods
in MyTestCase
查看帮助输出 -b
会缓冲(我想抑制?)stdout/stderr。参数 firefox
被用作模块中 运行 的测试名称。并且没有函数 method
存在,它输出这个错误:
AttributeError: 'module' object has no attribute 'firefox'
现在,您可能想要做的是调用 test_parser
,如果您使用 python myfile.py WebuiSeleniumTest.test_parser
调用,则您无法传递任何附加参数。这可能是你最后的问题。 this question 给出了一些可能的解决方案,用于测试 argparse 作为单元测试。