27

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?

example:

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())

Tests are executed using Selenium-RC framework.

falek.marcin
  • 9,679
  • 9
  • 28
  • 33

2 Answers2

70

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.

Hopefully one of those is useful for your needs.

Victor Häggqvist
  • 4,484
  • 3
  • 27
  • 35
Cody Hess
  • 1,777
  • 15
  • 19
19

unittest.TestCase._testMethodName

Example code:

import unittest


class BasicTests(unittest.TestCase):

    def test_print(self):
        print(self._testMethodName)
pbaranski
  • 22,778
  • 19
  • 100
  • 117
  • 2
    If you are in the test you know the name of the current function. It is when you are NOT in the function that is harder to deal with. You could use inspect.stack()[0][3]. This would work for the current function but the position in the stack can be dynamic and the function that you want may be above the current call by some dynamic count. For unittest the test_xxx() could be 1 to 10 levels higher. – Keith May 07 '19 at 21:29
  • 3
    This is works inside `setUp` and `tearDown`... which is what I was looking for. – cheshirekow Jan 16 '20 at 06:31
  • 2
    It is not generally recommended to access private property directly. Use it via `unittest.TestCase.id()`. For example: `self.id().split('.')[-1]` – nggit Jan 08 '23 at 03:10
  • `_testMethodName` is undocumented. I don't think it's a good idea to use it. – kamae Jun 10 '23 at 03:59