ITPUB论坛-中国最专业的IT技术社区

 找回密码
 注册
查看: 605|回复: 9

Test F.I.R.S.T

[复制链接]
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:28 | 显示全部楼层 |阅读模式
Testing is one of the few ideas that really changed my perception about software. It has become part of my everyday coding routine and is something that once you learn using it, it affects the way you write software.
Developing a software without testing is the same as developing software without a version control system or having a database in production without taking backups. It’s really scary!
Creating good tests is the programmer’s responsibility. Tests are production code. Personally, to me, it is more important to have good tests than good code. I can always refactor bad code if I have a good test suite that I trust. Refactoring is cheap when you have a good test suite.
Tests have many properties, but today I’ll talk only about the FIRST principles.
  • Fast
  • Isolated/independent
  • Repeatable
  • Self-validating
  • Thorough/Timely


认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:29 | 显示全部楼层
Fast
I need my test to be really fast! I want a fast feedback loop. I want to be able to run the tests on every line of code that I write. I want to know if the last line that I’ve written broke the behavior of the program. This way I can easily identify the line of code that doesn’t work.
I want my tests to be like my IDE.
When I make a syntax error, the IDE immediately highlights the line that has the syntax error. Imagine if it took the IDE 5 minutes to give you feedback when a syntax error occurred. Would you wait for 5 minutes before you write your next line or you would write the code and later “fix” the errors? If your tests aren’t fast, you won’t run them often. If you don’t run them often, then you will not pay attention to them. If you don’t pay attention to your test, then you have no feedback loop about your progress. Do you want your IDE to tell you about a syntax error 5 minutes later?

使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:29 | 显示全部楼层
Isolated/Independent
I follow the 3A rule. Arrange, Act, Assert.
Arrange
This step builds the necessary state for your test case. I usually spend time thinking good names for variables and helper functions that build the state. They help to better understand the situation that the test case describes.
Act
Invoke the method under test only.
Assert
Assert the logical outcome/expectation. Logical assertions can have multiple physical assert statements. I usually consider creating functions that describe what a single or a group of physical assertions mean in the context of the test case.

使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:30 | 显示全部楼层
Repeatable
Tests must have deterministic results. Given that I provide an input X when the method under test is invoked, then the outcome should always be Y. The tests should not be affected by the order that they are called or how my times you run them.
Each test should own and arrange its own data. If I have common data structures with other test cases, then I create a helper function. The helper function name should make sense for both test cases. If the name of the helper function doesn’t make sense for both test cases or if I can’t find a good name that satisfies both test cases then I prefer to have 2 functions with good names with the same code. I treat such a function as an accidental duplication.

使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:30 | 显示全部楼层
Self-Validating
No manual inspection to check whether the test has passed or failed.

使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:31 | 显示全部楼层
Thorough/Timely
Thorough
My primary goal when writing tests is not to have 100% code coverage. I aim to have a good test suite that I can trust. But what trust really means? To me trusting the test suite is just asking the following question: “Am I confident to push this code to production?”. If the answer is yes, then I have a test suite that I can trust. I invest time and effort to build such as test suite.
Timely
I practice TDD, I write my tests before I write my code. This way I can describe a good test case and then go implement it. This means that I let the test drive the design. I let the use case that I want to implement become my test cases. I let the test cases describe the behavior/intention, and I go implement the behavior.
By writing the tests first, I am forced to describe in my test case the behavior rather than the implementation because I have no implementation yet. This is a nice trick! If I decide to refactor, which means, “Change the implementation without changing the behavior”, then all I need to do is keep the tests green. If the tests are green, because of TDD, the behavior is the same.


使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:31 | 显示全部楼层

使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-3 09:33 | 显示全部楼层

使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-4 14:37 | 显示全部楼层
good job

使用道具 举报

回复
认证徽章
论坛徽章:
986
紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-11-4 14:37 | 显示全部楼层
good job

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

TOP技术积分榜 社区积分榜 徽章 电子杂志 团队 统计 虎吧 老博客 知识索引树 读书频道 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档 | IT博客
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛 | SAP ERP系统
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 网站律师 隐私政策 知识产权声明
京ICP备16024965号 北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表