Write Tests for People

本文强调了在编写生产代码时,如何撰写高质量的测试以供理解代码的人使用。测试应作为代码的文档,清晰描述代码的工作方式,并通过明确的上下文、调用方式和预期结果来帮助理解不同场景下的代码行为。此外,测试的命名和组织方式应便于读者快速了解其目的和应用场景。

You are writing automated tests for some or all of your production code. Congratulations! You are writing your tests before you write the code? Even better!! Just doing this makes you one of the early adopters on the leading edge of software engineering practice. But are you writing good tests? How can you tell? One way is to ask "Who am I writing the tests for?" If the answer is "For me, to save me the effort of fixing bugs" or "For the compiler, so they can be executed" then the odds are you aren't writing the best possible tests. So who should you be writing the tests for? For the person trying to understand your code.

Good tests act as documentation for the code they are testing. They describe how the code works. For each usage scenario the test(s):

  1. Describe the context, starting point, or preconditions that must be satisfied
  2. Illustrate how the software is invoked
  3. Describe the expected results or postconditions to be verified

Different usage scenarios will have slightly different versions of each of these. The person trying to understand your code should be able to look at a few tests and by comparing these three parts of the tests in question, be able to see what causes the software to behave differently. Each test should clearly illustrate the cause and effect relationship between these three parts. This implies that what isn't visible in the test is just as important as what is visible. Too much code in the test distracts the reader with unimportant trivia. Whenever possible hide such trivia behind meaningful method calls — the Extract Method refactoring is your best friend. And make sure you give each test a meaningful name that describes the particular usage scenario so the test reader doesn't have to reverse engineer each test to understand what the various scenarios are. Between them, the names of the test class and class method should include at least the starting point and how the software is being invoked. This allows the test coverage to be verified via a quick scan of the method names. It can also be useful to include the expected results in the test method names as long as this doesn't cause the names to be too long to see or read.

It is also a good idea to test your tests. You can verify they detect the errors you think they detect by inserting those errors into the production code (your own private copy that you'll throw away, of course). Make sure they report errors in a helpful and meaningful way. You should also verify that your tests speak clearly to a person trying to understand your code. The only way to do this is to have someone who isn't familiar with your code read your tests and tell you what they learned. Listen carefully to what they say. If they didn't understand something clearly it probably isn't because they aren't very bright. It is more likely that you weren't very clear. (Go ahead and reverse the roles by reading their tests!)


by Gerard Meszaros

This work is licensed under a Creative Commons Attribution 3

标题基于Python的汽车之家网站舆情分析系统研究AI更换标题第1章引言介绍舆情分析在汽车行业的重要性、研究背景、意义及论文创新点。1.1舆情分析的研究背景与意义阐述汽车行业舆情分析对市场决策与品牌管理的价值。1.2国内外舆情分析系统研究现状概述国内外舆情分析系统的技术发展与应用情况。1.3论文研究方法与创新点说明本文采用的技术方法及在舆情分析中的创新之处。第2章相关理论总结舆情分析、文本挖掘与Python编程相关理论。2.1舆情分析基础理论介绍舆情分析的定义、流程及关键技术。2.2文本挖掘技术阐述文本预处理、特征提取与情感分析等技术。2.3Python编程语言基础概述Python在数据处理与可视化方面的优势。第3章系统设计详细介绍基于Python的汽车之家网站舆情分析系统的设计方案。3.1系统总体架构设计系统的整体架构、模块划分及数据流向。3.2数据采集模块设计介绍如何使用Python爬虫采集汽车之家网站数据。3.3数据处理与分析模块设计阐述数据清洗、情感分析与关键词提取等过程。3.4可视化展示模块设计说明如何使用Python库进行数据可视化展示。第4章系统实现阐述系统的具体实现过程与关键技术。4.1开发环境与工具选择介绍系统开发所使用的Python库与开发环境。4.2数据采集实现详细描述爬虫程序的设计与实现。4.3数据处理与分析实现阐述数据处理流程与情感分析算法的实现。4.4可视化展示实现说明可视化图表的设计与实现过程。第5章实验与分析对系统进行实验验证并分析结果。5.1实验数据集与评估指标介绍实验所采用的数据集与评估舆情分析效果的指标。5.2实验方法与步骤给出实验的具体方法与步骤,包括数据采集、处理与分析等。5.3实验结果与分析从准确率、召回率等指标对实验结果进行详细分析。第6章结论与展望总结研究成果并展望未来研究方向。6.1研究结论概括系统实现的主要成果与舆情分析效
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值