如何从单元测试中的测试套件获取当前正在运行的测试用例名称 [英] How to get currently running testcase name from testsuite in unittest

查看:32
本文介绍了如何从单元测试中的测试套件获取当前正在运行的测试用例名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取当前正在运行的测试用例名称,而在测试套件集合中有 16 个测试用例.测试按顺序执行(按照将测试添加到 testSuite 集合的顺序).当我将所有测试添加到 testSuite 集合时,我可以预览这个对象,但是如何在测试运行时获取当前正在执行的测试.也许某个变量保存了这些信息?

How can I get currently running testcase name, while in the testsuite collection there are 16 testcases. Tests are executed sequentially (in the order of adding test to the testSuite collection). When I add all tests to testSuite collection I can preview this object but how can I get currently executing test while tests are running. Maybe some variable holds this information?

示例:

def suite():
    testSuite= unittest.TestSuite()
    testSuite.addTest(FlightsTestCases('test_sel__reservation_one_way_wizzair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_wizzair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_round_wizzair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_tair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_round_tair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_wizzair_credit_card'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_tair_credit_card'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_round_wizzair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_wizzair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_easyjet_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_ryanair_transfer'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_round_ryanair_credit_card'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_tair_duplicated'))
    testSuite.addTest(FlightsTestCases('test_reservation_wrong_card_lowcost'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_tair_credit_card'))
    testSuite.addTest(FlightsTestCases('test_sel_reservation_one_way_tair_wrong_credit_card'))

    return testSuite

if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

使用 Selenium-RC 框架执行测试.

Tests are executed using Selenium-RC framework.

推荐答案

unittest.TestCase.shortDescription()

返回测试的描述,如果没有提供描述,则返回 None.此方法的默认实现返回测试方法文档字符串的第一行(如果可用)或 None.

Returns a description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the test method’s docstring, if available, or None.

unittest.TestCase.id()

返回标识特定测试用例的字符串.这通常是测试方法的全名,包括模块和类名.

Return a string identifying the specific test case. This is usually the full name of the test method, including the module and class name.

希望其中之一对您的需求有用.

Hopefully one of those is useful for your needs.

这篇关于如何从单元测试中的测试套件获取当前正在运行的测试用例名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆