简体   繁体   English

是否可以在 python unittest (HtmlTestRunner) 中命名单个子测试?

[英]Is it possible to name individual subtests in python unittest (HtmlTestRunner)?

py version:Python 3.6.3 py版本:Python 3.6.3

Context: I am using HtmlTestRunner with unittest to get tidy post-deployment test reports for a REST API.上下文:我正在使用 HtmlTestRunner 和 unittest 来获得 REST API 的整洁的部署后测试报告。

Issue: When I run the tests, I do get everything separated, but the subtests inherit their name 1:1 from the test.问题:当我运行测试时,我确实将所有内容分开,但子测试从测试中继承了它们的名称 1:1。

Example view in html: Example view in html with errors html中的示例视图: html 中的示例视图有错误

Example code:示例代码:

import unittest
import HtmlTestRunner

class testBadRequest(unittest.TestCase):
    def test_serviceOne_bad_request(self):
        with self.subTest():
            x = endpointOne.getAll(acfg_url, cfg.badHeaders, acfg_host, acfg_params)
            self.assertEqual(x["status_code"], 400)
        with self.subTest():
            x = endpointOne.getByID(cfg.badID, acfg_url, acfg_headers, acfg_host, acfg_params)
            self.assertEqual(x["status_code"], 400)
            
if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(combine_reports=True)) 

(answering my own question) (回答我自己的问题)

Yes, you add the msg parameter when creating a subTest, so:是的,你在创建 subTest 时添加了 msg 参数,所以:

class testBadRequest(unittest.TestCase):
    def test_serviceOne_bad_request(self):
        with self.subTest(msg="getAll"):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM