Python:运行unittest.TestCase不打电话unittest.main()?

2024-06-08 04:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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))

使用the xmlrunner pacakge将输出格式化为Junit test。在

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

^{pr2}$

问题是不使用--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.TestCase不打电话unittest.main()?


Tags: thetestselfmaindefnotsvnrepo
1条回答
网友
1楼 · 发布于 2024-06-08 04:24:39

您确实应该使用适当的测试运行程序(例如nose或{})。在您的特定情况下,我将使用^{}代替:

if __name__ == '__main__':
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument(' junit', action='store_true')
    options, args = parser.parse_known_args()

    testrunner = None
    if (options.junit):
        testrunner = xmlrunner.XMLTestRunner(output='tests', descriptions=True)
    unittest.main(testRunner=testrunner, argv=sys.argv[:1] + args)

请注意,我从参数解析器中删除了 help,因此 junit选项变为隐藏,但它不再干扰unittest.main。我还将其余参数传递给unittest.main()。在

相关问题 更多 >

    热门问题