You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python Unittest结合Selenium的测试输出名称?

Absolutely! You can customize both the console output test names and the entries in your res.xml file using Python Unittest. Here's how to do it step by step:

Customizing Console Test Output

By default, Unittest uses your test method names (like test_order_confirmation) in console output, but we can override this with a custom test runner that respects your preferred names.

First, define a custom test result and runner to handle the name display:

from unittest import TextTestRunner, TextTestResult

class CustomTestResult(TextTestResult):
    def getDescription(self, test):
        # Use the custom name if the test has one set
        if hasattr(test, 'custom_test_name'):
            return test.custom_test_name
        # Fall back to default behavior if no custom name exists
        return super().getDescription(test)

class CustomTestRunner(TextTestRunner):
    def _makeResult(self):
        return CustomTestResult(self.stream, self.descriptions, self.verbosity)

Then, update your test class to set a custom name for each test method:

class NotificationsTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome(executable_path=Data.driver)
        cls.driver.implicitly_wait(10)
        cls.driver.maximize_window()

    def test_order_confirmation(self):
        # Set your custom test name here
        self.custom_test_name = "Verify Order Confirmation Notification Flow"
        Actions.login(self, Data.email, Data.password)
        driver = self.driver
        driver.get(Data.website + "Admin/Configuration/Message")
        notify = NotificationsPage(driver)
        notify.order_confirmation()

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()
        cls.driver.quit()

Finally, run your tests with the custom runner (use verbosity=2 to see the custom names clearly):

if __name__ == '__main__':
    runner = CustomTestRunner(verbosity=2)
    unittest.main(testRunner=runner)
Customizing the res.xml Report

Assuming you're using the popular xmlrunner library (standard Unittest doesn't include an XML runner by default), you can modify the XML output to use your custom names too.

First, install xmlrunner if you haven't already:

pip install xmlrunner

Create a custom XML runner that picks up your test names:

from xmlrunner import XMLTestRunner

class CustomXMLTestRunner(XMLTestRunner):
    def _get_test_name(self, test):
        # Use the custom test name if available
        if hasattr(test, 'custom_test_name'):
            return test.custom_test_name
        # Fall back to the default method name
        return super()._get_test_name(test)

Update your main block to generate the XML file with custom names:

if __name__ == '__main__':
    with open('res.xml', 'wb') as output_file:
        runner = CustomXMLTestRunner(output=output_file)
        unittest.main(testRunner=runner)

Now your res.xml will show your custom strings in the <testcase> elements instead of the original method names.

内容的提问来源于stack exchange,提问作者lukos06

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:33:12