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

后端 未结 7 1125
南旧
南旧 2021-02-04 23:02

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

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 23:33

    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.

提交回复
热议问题