python - 如何从单元测试中的测试套件获取当前正在运行的测试用例名称

标签 python unit-testing selenium

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

示例:

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 框架执行测试。

最佳答案

unittest.TestCase.shortDescription()

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.

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

关于python - 如何从单元测试中的测试套件获取当前正在运行的测试用例名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7797442/

相关文章:

python - 使用 Regex Python 提取字符串中的字符串

python - 如何为每个 CSV 文件创建单独的 Pandas DataFrame 并给它们起有意义的名字?

javascript - javascript CustomEvent 的 jasmine 单元测试

python - 无法弄清楚如何使用/selenium 抓取页面

python - 为什么 str(super(B, b)) 不等于 super(B, b).__str__()?

python - 将输入参数从 jupyter notebook 传递给脚本

android - 使用 Android volley 进行单元测试

sql-server - VBScript、ASP 代码和 SQL Server 2000 的单元测试

python - 如何等待页面在 Python Selenium 中加载

javascript - Protractor - 从 Bootstrap 下拉列表中选择