This post was originally published on Medium
这篇文章最初发表于Medium
Writing tests represents one of those few stages of software development that is usually overlooked, even though it may be one of the most important one. Developers mention it and usually are either uncomfortable and prefer not to write tests for their code, or have many excuses for not writing them at all.
编写测试代表了通常被忽略的那几个软件开发阶段之一,尽管它可能是最重要的阶段之一。 开发人员会提到它,通常会感到不舒服,他们不愿意为他们的代码编写测试,或者有很多借口根本不编写代码。
Well, tests are important:
好吧,测试很重要:
A software bug in a Therac-25 radiation therapy machine caused the death of five patients after receiving a massive dose of X-rays.
Therac-25放射治疗机中的软件错误在接受大量X射线照射后导致五名患者死亡。
Knight Capital lost half a billion dollars in half an hour when a software bug allowed computers to sell and buy millions of shares with no human oversight.
骑士资本(Knight Capital)在半小时内损失了10亿美元,当时一个软件漏洞允许计算机在无人监督的情况下买卖计算机。
These and many other stories tell how seemingly unimportant bugs can actually cause so many tragic disasters.
这些故事以及其他许多故事都说明,看似不重要的错误实际上会导致如此多的悲剧性灾难。
We know how critical software is becoming to every area of our lives. We know that the security, stability and correctness of this software has a crucial importance in our lives. So the question is: why do developers still avoid writing tests?
我们知道关键软件在我们生活的各个领域都变得越来越重要。 我们知道,该软件的安全性,稳定性和正确性对我们的生活至关重要。 所以问题是:为什么开发人员仍然避免编写测试?
借口1:“笔试只会花太多时间。” (Excuse #1: “Writing tests just takes too much time.”)
It’s easy to perceive writing tests as taking too much time, as opposed to jumping directly into the implementation part.
很容易将编写测试视为花费大量时间,而不是直接跳到实现部分。
This may be true in the short term, but if we take into consideration the time you may potentially need to fix all the bugs you could avoided by writing tests, then writing tests can actually save time — and even money — along the way.
在短期内这可能是正确的,但是如果考虑到时间,您可能需要修复通过编写测试可以避免的所有错误,那么编写测试实际上可以节省时间,甚至节省金钱。
Tests do not eliminate bugs completely (nothing can do that), but they can dramatically reduce them. They can even make you feel more confident that you’re not breaking any existing functionality when you add new features and refactor old ones.
测试不能完全消除错误(没有办法做到),但是可以大大减少错误。 它们甚至可以使您更有信心,在添加新功能并重构旧功能时不会破坏任何现有功能。
Tests also help you save time and protect your existing implementations from inexperienced programmers who recently joined your team. If these newcomers introduce bugs, your tests will fail. And when your tests fail, you become aware that something went wrong.
测试还可以帮助您节省时间,并保护您的现有实现免受最近没有经验的程序员(加入您的团队)的侵害。 如果这些新手引入了错误,则您的测试将失败。 当测试失败时,您会意识到出了点问题。
Project managers who lack a programming background — and thus may not understand the programming complexities that may arise all of a sudden — tend to have high expectations of the developers on their teams. They want to things done quickly, and fast production-ready code. They may consider it unreasonable to postpone a strict deadline.
缺乏编程背景的项目经理(因此可能无法理解突然出现的编程复杂性)往往对团队中的开发人员抱有很高的期望。 他们希望快速完成事情,并快速准备生产代码。 他们可能认为推迟严格的截止日期是不合理的。
These situations may put you into tough situations — where you have to choose between following best practices or getting things done quick-and-dirty.
这些情况可能会使您陷入困境–在这种情况下,您必须选择遵循最佳实践或快速完成工作。
You should try and find a few professional principles you can stick to no matter what. You should try your best to convince your manager of the importance of best practices, and how they pay off in the long run. And if your manager can’t be convinced, then you may consider changing jobs.
您应该尝试找到一些无论如何都可以坚持的专业原则。 您应该尽力使您的经理相信最佳做法的重要性,以及从长远来看它们如何产生收益。 而且,如果无法说服您的经理,那么您可以考虑换工作。
借口2:害怕编写测试 (Excuse #2: the fear of writing tests)
It’s no surprise that developers spend most of their working hours either reading code, writing code, or discussing new problems to solve with code. As a result, they have a very strong emotional attachment to it, and prefer to treat it as their precious property.
开发人员将大部分工作时间花费在阅读代码,编写代码或讨论要用代码解决的新问题上也就不足为奇了。 结果,他们对它有着强烈的情感依恋,并喜欢将其视为自己的宝贵财产。
Many are confident that they’re already experienced enough, and that they are capable of covering every possible scenario without much effort.
许多人相信自己已经有足够的经验,并且能够轻松应对各种可能的情况。
On the inside, they may harbor a sense of insecurity. And subjecting their code to tests may cause that insecurity to flare up.
在内部,他们可能怀有不安全感。 对他们的代码进行测试可能会导致这种不安全感加剧。
Maybe they pushed their code changes to the master branch faster than they should have, because they want to look like a productive employee to their manager. And now they’re afraid that writing tests might reveal bugs in their code. They’re afraid of being exposed as average programmers who cannot write bug-free code.
也许他们将代码更改推送到master分支的速度比他们应有的速度快,因为他们希望在经理中看起来像是富有成效的员工。 现在,他们担心编写测试可能会发现其代码中的错误。 他们害怕被无法编写无错误代码的普通程序员所接受。
Well, we should all do ourself a favor and never let our own sense of ego delude us into not writing tests. As uncomfortable as it may feel, we should take responsibility for our own work. This is one of the best ways to avoid bugs — bugs which can often have tragic consequences.
好吧,我们都应该帮自己一个忙,不要让我们自己的自我意识欺骗我们不编写测试。 尽管感觉不舒服,我们应该为自己的工作负责。 这是避免bug的最佳方法之一,而bug往往会带来悲剧性的后果。
Tests allow you to make big changes in your code quickly, since you can be confident that everything is working correctly. When you’re used to writing tests for your code, you will then usually be able to finish off your work a lot faster, as you will have instant visual feedback when something is failing as you will see a red light. As a result, you will also write better code, feel less stress, and eventually get promoted, as you’re ultimately bringing in more value with the work that you are doing.
测试使您可以快速地对代码进行重大更改,因为您可以确信一切正常。 当您习惯于为代码编写测试时,通常可以更快地完成工作,因为当出现故障时您会立即获得视觉反馈,因为您会看到红灯。 结果,您还将编写更好的代码,减轻压力,并最终获得晋升,因为您最终将通过所做的工作带来更多价值。
抽出时间进行测试 (Make time for testing)
Tests will never be able to catch 100% of bugs, but they will contribute to the security, stability and correctness of your code. Make time for them.
测试将永远无法捕获100%的错误,但是它们将有助于代码的安全性,稳定性和正确性。 为他们腾出时间。
This post was originally published on Medium
这篇文章最初发表于Medium