Is it possible to break a long function name across multiple lines?
No, this is not possible.
In most cases such a long name would be undesirable from the standpoint of readability and usability of the function, though your use case for test names seems pretty reasonable.
The lexical rules of Python do not allow a single token (in this case an identifier) to be split across multiple lines. The logical line continuation character (\
at the end of a line) can join multiple physical lines into a single logical line, but cannot join a single token across multiple lines.
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
You could also write a decorator that mutates .__name__
for the method.
def test_name(name):
def wrapper(f):
f.__name__ = name
return f
return wrapper
Then you could write:
class ClientConnectionTest(unittest.TestCase):
@test_name("test_that_client_event_listener_"
"receives_connection_refused_error_without_server")
def test_client_offline_behavior(self):
self.given_server_is_offline()
self.given_client_connection()
self.when_client_connection_starts()
self.then_client_receives_connection_refused_error()
relying on the fact that Python concatenates source-adjacent string literals.
Per the answer to this question:How to disable a pep8 error in a specific file?, use the # nopep8
or # noqa
trailing comment to disable PEP-8 for a long line. It's important to know when to break the rules. Of course, the Zen of Python would tell you that "Special cases aren't special enough to break the rules."