Is it possible to break a long function name across multiple lines?

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.

Here is an example of a function that is too long...

    class ClientConnectionTest(unittest.TestCase):

        def test_that_client_event_listener_receives_connection_refused_error_without_server(self):

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.

Edit: 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. Also, it doesn't really answer the original question.

  • You could separate the 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.


Is there a way in Python to split a long function declaration across multiple lines?

For example...

    def test_that_client_event_listener_receives_

Or will I have to bite the bullet and shorten it myself?

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.