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 descriptive method names to describe what each test does. However this often leads to me exceeding the character limit.
This is an example of a function name that is too long.
class ClientConnectionTest(unittest.TestCase):
def test_that_client_event_listener_receives_connection_refused_error_without_server(self):
self.given_server_is_offline()
self.given_client_connection()
self.when_client_connection_starts()
self.then_client_receives_connection_refused_error()
My options
-
You could just write shorter method names!
I know, but I don't want to lose the descriptiveness of the test names.
-
You could write multi-line comments above each test instead of using long names!
This is a decent idea, but then I won't be able to see the test names when running the tests inside my IDE (PyCharm).
-
Perhaps you can continue the lines with a backslash (a logical line continuation character).
Unfortunately this isn't an option in Python, as mentioned in Dan's answer.
-
You could stop linting your tests.
This makes sense in some ways, but it's nice to encourage a well-formatted test suite.
-
You could increase the line length limit.
Our team likes having the limit because it helps keep the code readable on narrow displays, so this isn't the best option.
-
You could remove
test
from the start of your methods.
This is not an option. Python's test runner needs all test methods to start with test
or it won't pick them up.
Some test runners let you specify a regular expression when searching for test functions, although I'd rather not do this because it's extra setup for everyone working on the project.
-
You could separate
EventListener
into its own class and test it separately.
The Event Listener is in its own class (and is tested). It's just an interface that gets triggered by events happening within ClientConnection
. This kind of suggestion seems to have good intent, but is misdirected and doesn't help answer the original question.
-
You could use a BDD Framework like Behave. It's designed for expressive tests.
This is true, and I hope to use more of them in the future. Although I'd still like to know how to split function names across lines.
Ultimately
Is there a way in Python to split a long function declaration across multiple lines, as in the following example?
def test_that_client_event_listener_receives_
connection_refused_error_without_server(self):
self.given_server_is_offline()
self.given_client_connection()
self.when_client_connection_starts()
self.then_client_receives_connection_refused_error()
Will I have to bite the bullet and shorten it myself?