Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get currently running testcase name from testsuite in unittest

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.

like image 224
falek.marcin Avatar asked Oct 17 '11 17:10

falek.marcin


People also ask

What does Unittest main () do?

Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.

Is Unittest a test runner?

unittest contains both a testing framework and a test runner. unittest has some important requirements for writing and executing tests.

Which function in Unittest will run all of your tests?

TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.

How do you run a test case in python?

From the context menu, select the corresponding run command. If the directory contains tests that belong to the different testing frameworks, select the configuration to be used. For example, select Run 'All tests in: <directory name>' Run pytest in <directory name>'.


2 Answers

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.

like image 160
Cody Hess Avatar answered Sep 29 '22 16:09

Cody Hess


unittest.TestCase._testMethodName

Example code:

import unittest   class BasicTests(unittest.TestCase):      def test_print(self):         print(self._testMethodName) 
like image 41
pbaranski Avatar answered Sep 29 '22 16:09

pbaranski