unittest.main() python如何执行_Python:运行unittest.TestCase而不调用unittest.main()?

本文介绍了如何在Python的unittest框架中编写测试套件,并使用xmlrunner库将测试结果输出为JUnit格式的XML报告。通过命令行参数`--junit`可以控制是否生成JUnit报告,但遇到了在运行脚本时该选项不被识别的问题。解决方案是正确地处理命令行参数并根据参数决定是否调用xmlrunner.
摘要由CSDN通过智能技术生成

我在Python的unittest中编写了一个小测试套件:

class TestRepos(unittest.TestCase):

@classmethod

def setUpClass(cls):

"""Get repo lists from the svn server."""

...

def test_repo_list_not_empty(self):

"""Assert the the repo list is not empty"""

self.assertTrue(len(TestRepoLists.all_repos)>0)

def test_include_list_not_empty(self):

"""Assert the the include list is not empty"""

self.assertTrue(len(TestRepoLists.svn_dirs)>0)

...

if __name__ == '__main__':

unittest.main(testRunner=xmlrunner.XMLTestRunner(output='tests',

descriptions=True))

使用xmlrunner pacakge将输出格式化为Junit测试.

我添加了一个命令行参数来切换JUnit输出:

if __name__ == '__main__':

parser = argparse.ArgumentParser(description='Validate repo lists.')

parser.add_argument('--junit', action='store_true')

args=parser.parse_args()

print args

if (args.junit):

unittest.main(testRunner=xmlrunner.XMLTestRunner(output='tests',

descriptions=True))

else:

unittest.main(TestRepoLists)

问题是运行脚本没有--junit工作,但使用--junit与unittest参数的冲突调用它:

option --junit not recognized

Usage: test_lists_of_repos_to_branch.py [options] [test] [...]

Options:

-h, --help Show this message

-v, --verbose Verbose output

...

如何在不调用unittest.main()的情况下运行unittest.TestCase?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值