测试驱动开发 测试前移
I’ve been programming for five years and, honestly, I have avoided test-driven development. I haven’t avoided it because I didn’t think it was important. In fact, it seemed very important–but rather because I was too comfortable not doing it. That’s changed.
我已经编程五年了,说实话,我避免了测试驱动的开发。 我没有回避它,因为我认为这并不重要。 实际上,这似乎非常重要-而是因为我很不愿意这样做。 改变了。
什么是测试? (What is Testing?)
Testing is the process of ensuring a program receives the correct input and generates the correct output and intended side-effects. We define these correct inputs, outputs, and side-effects with specifications. You may have seen testing files with the naming convention filename.spec.js
. The spec
stands for specification. It is the file where we specify or assert what our code should do and then test it to verify it does it.
测试是确保程序接收正确的输入并生成正确的输出和预期的副作用的过程。 我们用规范定义这些正确的输入,输出和副作用。 您可能已经看到了使用命名约定filename.spec.js
测试文件。 spec
代表规格。 在该文件中,我们指定或声明代码应执行的操作,然后对其进行测试以验证其作用。
You have two choices when it comes to testing: manual testing and automated testing.
关于测试,您有两种选择:手动测试和自动测试。
手动测试 (Manual Testing)
Manual testing is the process of checking your application or code from the user’s perspective. Opening up the browser or program and navigating around in an attempt to test functionality and find bugs.
手动测试是从用户角度检查应用程序或代码的过程。 打开浏览器或程序并四处导航,以尝试测试功能并查找错误。
自动化测试 (Automated Testing)
Automated testing, on the other hand, is writing code that checks to see if other code works. Contrary to manual testing, the specifications remain constant from test to test. The biggest advantage is being able to test many things much faster.
另一方面,自动化测试正在编写代码,以检查其他代码是否有效。 与手动测试相反,各个测试的规格保持不变。 最大的优势是能够更快地测试许多东西。
It’s the combination of these two testing techniques that will flush out as many bugs and unintended side-effects as possible, and ensure your program does what you say it will. The focus of this article is on automated testing, and in particular, unit testing.
正是这两种测试技术的结合,将消除尽可能多的错误和意外的副作用,并确保您的程序按照您的意愿行事。 本文的重点是自动化测试,尤其是单元测试。
There are two main types of automated tests: Unit and End-to-End (E2E). E2E tests test an application as a whole. Unit tests test the smallest pieces of code, or units. What is a unit? Well, we define what a unit is, but in general, it’s a relatively small piece of application functionality.
自动化测试主要有两种类型:单元测试和端到端(E2E)。 端到端测试对整个应用程序进行测试。 单元测试测试最小的代码段或单元。 什么是单位? 好了,我们定义了一个单元是什么,但是一般来说,它只是一个相对较小的应用程序功能。
回顾: (Recap:)
- Testing is verifying our application does what it should. 测试正在验证我们的应用程序应执行的操作。
- There are two types of tests: manual and automated 有两种类型的测试:手动和自动
Tests assert that your program will behave a certain way. Then the test itself proves or disproves that assertion.
测试断言您的程序将以某种方式运行。 然后,测试本身将证明或反对该断言。
测试驱动开发 (Test-Driven Development)
Test-driven development is the act of first deciding what you want your program to do (the specifications), formulating a failing test, then writing the code to make that test pass. It is most often associated with automated testing. Although you could apply the principals t成器,使我们的收入达到数百万美元!
结论 (Conclusion)
Some of what we did was for demonstration purposes. Testing whether our number was within a specified range is fun, but that formula can be mathematically proven. So a better test might be to make sure the formula is called.
我们所做的一些是出于演示目的。 测试我们的数字是否在指定范围内很有趣,但是该公式可以通过数学证明。 因此,更好的测试可能是确保调用该公式。
Also, you could get more creative with the random ID generator. For example, if it can’t find a unique number, the function could automatically increase the range by one.
另外,您可以通过随机ID生成器获得更多创意。 例如,如果找不到唯一编号,则该功能可以自动将范围增加一。
One other thing we saw was how our tests and even specifications might crystalize a bit as we test and refactor. In other words, it would be silly to think nothing will change throughout the process.
我们看到的另一件事是,在我们测试和重构时,我们的测试甚至规范可能如何变得明确。 换句话说,认为在整个过程中什么都不会改变是很愚蠢的。
Ultimately, test-driven development provides us with a framework to think about our code at a more granular level. It is up to you, the developer, to determine how granular you should define your tests and assertions. Keep in mind, the more tests you have, and the more narrowly focused your tests are, the more tightly coupled they become with your code. This might cause a reluctance to refactor because now you must also update your tests. There is certainly a balance in the number and granularity of your tests. The balance is up to you, the developer, to figure out.
最终,测试驱动的开发为我们提供了一个框架,可以在更细粒度的层次上考虑我们的代码。 由开发人员来决定要定义测试和断言的粒度。 请记住,您拥有的测试越多,测试的重点越狭窄,它们与您的代码的耦合就越紧密。 这可能会导致不愿重构,因为现在您还必须更新测试。 测试的数量和粒度肯定是平衡的。 余额由开发人员来确定。
Thanks for reading!
谢谢阅读!
woz
沃兹
翻译自: https://www.freecodecamp.org/news/an-introduction-to-test-driven-development-c4de6dce5c/
测试驱动开发 测试前移