Our development team uses a PEP8 linter which requires a maximum line length of 80 characters.
When I\'m writing unit tests in python, I like to have
We can applying decorator to the class instead of method since unittest
get methods name from dir(class)
.
The decorator decorate_method
will go through class methods and rename method's name based on func_mapping
dictionary.
Thought of this after seeing decorator answer from @Sean Vieira , +1 from me
import unittest, inspect
# dictionary map short to long function names
func_mapping = {}
func_mapping['test_client'] = ("test_that_client_event_listener_receives_"
"connection_refused_error_without_server")
# continue added more funtion name mapping to the dict
def decorate_method(func_map, prefix='test_'):
def decorate_class(cls):
for (name, m) in inspect.getmembers(cls, inspect.ismethod):
if name in func_map and name.startswith(prefix):
setattr(cls, func_map.get(name), m) # set func name with new name from mapping dict
delattr(cls, name) # delete the original short name class attribute
return cls
return decorate_class
@decorate_method(func_mapping)
class ClientConnectionTest(unittest.TestCase):
def test_client(self):
# dummy print for testing
print('i am test_client')
# self.given_server_is_offline()
# self.given_client_connection()
# self.when_client_connection_starts()
# self.then_client_receives_connection_refused_error()
test run with unittest
as below did show the full long descriptive function name, thinks it might works for your case though it may not sounds so elegant
and readable from the implementation
>>> unittest.main(verbosity=2)
test_that_client_event_listener_receives_connection_refused_error_without_server (__main__.ClientConnectionTest) ... i am client_test
ok