gpt4 book ai didi

python - 关于编写测试友好代码的任何提示?

转载 作者:太空狗 更新时间:2023-10-29 19:36:13 25 4
gpt4 key购买 nike

是否有编写测试友好的 Python 代码的指南?

我的信念:

  • 一种方法做一件事。
  • 不要使用副作用。

还有其他建议吗?

最佳答案

测试驱动

我能给你的编写测试友好代码的最好技巧是先编写测试。然后编写生产代码(TDD)。 Uncle Bob设计了三个简单的书写规则 TDD :

  1. You are not allowed to write any production code unless it is to make afailing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficientto fail; and compilation failures arefailures.
  3. You are not allowed to write any more production code than issufficient to pass the one failingunit test.

特别是这句话应该沉入:

If you think about this you willrealize that you simply cannot writevery much code at all withoutcompiling and executing something.Indeed, this is really the point.

编写可测试代码

另请阅读 google 测试专家的“Writing Testable Code”(适用于 java,但也适用于 python 的大部分)。您还应该下载/阅读 complete PDF在那个页面上。但快速回顾一下:

  1. Constructor does Real Work
  2. Digging into Collaborators
  3. Brittle Global State & Singletons
  4. Class Does Too Much

关于python - 关于编写测试友好代码的任何提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4710621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com