Is it possible to break a long function name across multiple lines?
<p>Our development team uses a PEP8 linter which requires a <strong>maximum line length of 80 characters</strong>.</p>

<p>When I'm writing unit tests in python, I like to have <strong>descriptive method names</strong> to describe what each test does. However this often leads to me exceeding the character limit.</p>

<p>Here is an example of a function that is too long...</p>

<pre><code>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()
</code></pre>

<h3>My Options:</h3>

<ul>
<li><blockquote>
  <p>You could just write shorter method names!</p>
</blockquote>

<p>I know, but I don't want to lose the descriptiveness of the test names.</p></li>
<li><blockquote>
  <p>You could write multi-line comments above each test instead of using long names!</p>
</blockquote>

<p>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).</p></li>
<li><blockquote>
  <p>Perhaps you can continue the lines with a backslash (a logical line continuation character).</p>
</blockquote>

<p>Unfortunately this isn't an option in Python, as mentioned in Dan's answer.</p></li>
<li><blockquote>
  <p>You could stop linting your tests.</p>
</blockquote>

<p>This makes sense in some ways, but it's nice to encourage a well-formatted test suite.</p></li>
<li><blockquote>
  <p>You could increase the line length limit.</p>
</blockquote>

<p>Our team likes having the limit because it helps keep the code readable on narrow displays, so this isn't the <em>best</em> option.</p></li>
<li><blockquote>
  <p>You could remove <code>test</code> from the start of your methods.</p>
</blockquote>

<p>This is not an option. Python's test runner needs all test methods to start with <code>test</code> or it won't pick them up.</p>

<p>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.</p></li>
<li><blockquote>
  <p>You could separate the EventListener into its own class and test it separately.</p>
</blockquote>

<p>The Event Listener <strong><em>is</em></strong> 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.</p></li>
<li><blockquote>
  <p>You could use a BDD Framework like <a href="http://pythonhosted.org/behave/" rel="noreferrer">Behave</a>. It's designed for expressive tests.</p>
</blockquote>

<p>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.</p></li>
</ul>

<h3>Ultimately...</h3>

<p>Is there a way in Python to <strong>split a long function declaration across multiple lines</strong>?</p>

<p>For example...</p>

<pre><code>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()
</code></pre>

<p>Or will I have to bite the bullet and shorten it myself?</p>