Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python print name of a unit test function in setup

After reading How to get the function name as string in Python?

I wondered if it is somehow possible to put a logging statement into one of the

def setUp(self):
    ... 

def tearDown(self):
    ...

and print the name of the current test function.

like image 956
Gobliins Avatar asked Jan 19 '26 16:01

Gobliins


1 Answers

Testcases have a self.id() method that returns the current TestClassName.testMethodName.

The method uses self._testMethodName to build that string; you could use that attribute but the name of the attribute does flag it as internal. Relying on the attribute being there may not work in all Python versions.

like image 129
Martijn Pieters Avatar answered Jan 21 '26 06:01

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!