单元测试unit test,集成测试integration test和功能测试functional test的区别(英文和中文版)

以下内容转自 https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional-testing/

What are Unit Testing, Integration Testing and Functional Testing?

TAGS: TESTING

Finding your way around the maze that is JavaScript testing can sometimes be difficult. There are unit tests, integration tests, functional tests, E2E tests, browser tests… With so many buzzwords, who knows what they do and which one to use, what for, and when?

To help with that problem, in this article I’ll give you a guide comparing the different kinds of testing types available, and some recommendations for their use.

找出测试javascript的方法有时候非常困难。现在有单元测试,集成测试, 功能测试,E2E测试,浏览器测试,如此多时髦的测试,谁知道它们是怎么做的或者我们要用哪一个,什么时候用等等?

Unit Testing

Unit testing is the practice of testing small pieces of code, typically individual functions, alone and isolated. If your test uses some external resource, like the network or a database, it’s not a unit test.

Unit tests should be fairly simple to write. A unit tests should essentially just give the function that’s tested some inputs, and then check what the function outputs is correct. In practice this can vary, because if your code is poorly designed, writing unit tests can be difficult. Because of that, unit testing is the only testing method which also helps you write better code – Code that’s hard to unit test usually has poor design.

In a sense, unit testing is the backbone. You can use unit tests to help design your code and keep it as a safety net when doing changes, and the same methods you use for unit testing are also applicable to the other types of testing. All the other test types are also constructed from similar pieces as unit tests, they are just more complex and less precise.

Unit tests are also great for preventing regressions – bugs that occur repeatedly. Many times there’s been a particularly troublesome piece of code which just keeps breaking no matter how many times I fix it. By adding unit tests to check for those specific bugs, you can easily prevent situations like that. You can also use integration tests or functional tests for regression testing, but unit tests are much more useful because they are very specific, which makes it easy to pinpoint and then fix the problem.

When should you use unit testing? Ideally all the time, by applying test-driven development. A good set of unit tests do not only prevent bugs, but also improve your code design, and make sure you can later refactor your code without everything completely breaking apart.

Popular tools for unit testing include MochaJasmine and Tape.

单元测试

    单元测试是检测一小段代码的良好实践,典型的单元测试是测试独立的function,要保证这个测试是单独孤立的。如果你写的测试用了外部的资源,比如网络或者数据库,它就不是一个单元测试。

    单元测试应该是写起来很简单的。它基本上是给了这个要测的function的输入,检测这个function的输出是否正确就可以了。如果你的代码设计不好,单元测试就很难写,所以单元测试是唯一的能帮助你写好代码的测试方法-(重复一下)一般测试很难写的代码它的设计往往也不好。

   某种意义上,单元测试像是一个脊椎骨。单元测试可以帮助你设计你的代码,并且在你重构或者修改代码的时候有保证不会破坏代码功能。你写单元测试的方法可以被应用到写其他类型的测试上。其他类型的测试也可以和单元测试有相同的结构,只是它们更加复杂,不够简洁而已。

    单元测试也可以很好的阻止bug重复发生(regressions)。我们经常会遇到这种情况,有段很烦人的代码,无论我们修多少次,还是会有问题。通过增加单元测试来检查那些特定的bug,你可以很容易的阻止类似的情况发生。你也可以用集成测试或者功能测试来做回归测试,但是单元测试因为其能够更加针对细节而更加有用,单元测试可以很容易的定位问题并且解决问题。

    什么时候用单元测试合适呢?理想情况下任何时候,可以用测试驱动开发。好的单元测试不仅可以避免bugs产生,也可以帮助提高代码设计,还可以在以后重构代码的时候保证代码不会被改的四分五裂。

    现在比较时髦的单元测试工具有MochaJasmine 和 Tape

Integration Testing

As the name suggests, in integration testing the idea is to test how parts of the system work together – the integration of the parts. Integration tests are similar to unit tests, but there’s one big difference: while unit tests are isolated from other components, integration tests are not. For example, a unit test for database access code would not talk to a real database, but an integration test would.

Integration testing is mainly useful for situations where unit testing is not enough. Sometimes you need to have tests to verify that two separate systems – like a database and your app – work together correctly, and that calls for an integration test. As a result, when validating integration test results, you could for example validate a database related test by querying the database to check the database state is correct.

Integration tests are often slower than unit tests because of the added complexity. They also might need some set up or configuration, such as the setting up of a test database. This makes writing and maintaining them harder than unit tests, so you should focus on unit tests unless you absolutely need an integration test.

You should have fewer integration tests than unit tests. You should mainly use them if you need to test two separate systems together, or if a piece of code is too complex to unit test. But in the latter case, I would recommend fixing the code so it’s easy to unit test instead.

Integration tests can usually be written with the same tools as unit tests.

集成测试

    就像这个测试的名字(集成)所表达的意思一样,集成测试想要测试的是这个系统的各个部分在一起合作的如何-将各个代码块集成在一起。集成测试和单元测试很类似,但是有一个很大的不同:单元测试是不依靠于其他组件的,只测试某段代码,但是集成测试不是。举个例子,一段数据库访问代码的单元测试是不需要和真正的数据库连接的,但是集成测试不是这样。

    有的情况下,单元测试不能够满足测试要求,这个时候集成测试就大有用处。有时候你需要验证两个不同的系统是否在一起正常的通信和正常运转,比如你的应用程序和数据库是否集成正确,运转正常,这个时候就需要集成测试了。结果是,当验证集成测试结果时,你通过写数据库连接语句检查数据库状态是否正常而顺便做了数据库连接测试。

    集成测试比单元测试会慢一些,是因为它比单元测试更加复杂。集成测试可能需要配置环境,例如建一个测试数据库之类。这就使得写集成测试和维护集成测试比单元测试难,所以你应该更专注于写单元测试,除非你真的需要集成测试再写它。(本翻译者并不认同最后一句话,我觉得它们的职责不一样,所以集成测试还是有必要写的)

    你应该写更多的单元测试,更少的集成测试。如果你要测试两个分开的系统部分或者一段代码很复杂不容易写单元测试,你可以用集成测试。但是对于后一种情况(代码复杂到不容易写单元测试),我建议是修改代码使它容易些单元测试。

    集成测试也可以用单元测试的那些工具来做。

 

Functional Testing

Functional testing is also sometimes called E2E testing, or browser testing. They all refer to the same thing.

Functional testing is defined as the testing of complete functionality of some application. In practice with web apps, this means using some tool to automate a browser, which is then used to click around on the pages to test the application.

You might use a unit test to test an individual function and an integration test to check that two parts of the play nice. Functional tests are on a whole another level. While you can have hundreds of unit tests, you usually want to have only a small amount of functional tests. This is mainly because functional tests can be difficult to write and maintain due to their very high complexity. They also run very slowly, because they simulate real user interaction on a web page, so even page load times become a factor.

Because of all this, you shouldn’t try to make very fine-grained functional tests. You don’t want to test a single function, despite the name “functional” perhaps hinting at it. Instead, functional tests should be used for testing common user interactions. If you would manually test a certain flow of your app in a browser, such as registering an account, you could make that into a functional test.

While in unit and integration tests you would validate the results in code, functional test results should be validated the same way as you would validate it if you were a user of the page. Going with the registration example, you could validate it by checking that the browser is redirected to a “thanks for registering page”.

You should use functional tests if you have some repeated tests you do manually in the browser, but be careful to not make them too fine-grained, as they can easily become a nightmare to maintain. I know, because I’ve seen it happen many times.

The most common tool used for functional testing is Selenium. Running Selenium is usually done with Selenium WebDriver, or Protractor. Sometimes PhantomJS and CasperJS can be used as well, especially if you don’t need to test in real browsers.

      功能性测试也被称为E2E测试,或者浏览器测试。它们指的是一件事情。

      功能测试被解释为测试应用程序的全部的完整的功能。对于互联网应用程序来说,这意味着用一些工具在浏览器上自动化测试,在页面上自动点击来测试应用程序。

  你可以用单元测试来测试单独的function,也可以用集成测试来测试两个部分是否一起正常工作。功能测试完全是另外一个级别上的。一般情况下,你有几百个单元测试但是往往只想要少量功能测试。一个主要原因是因为功能测试的复杂度高,所以不容易写和维护它。功能测试通常运行很慢,是因为它们模拟的是web页面上的真实用户交互,页面加载时间也算是运行慢的一个原因。

  根据以上原因,你不应该做粒度太小的功能测试。你不想测试一个单独的功能,尽管这个测试的名字是这样。功能测试应该做的是测试普通用户的交互行为。如果你想手动用浏览器测试一个确定的使用过程,例如注册账户,那么你应该使用功能测试。

  单元测试和集成测试是用代码来验证结果,功能测试结果验证就和网站用户验证结果一样。还拿注册的例子来说,测试成功标准就是浏览器直接跳到“thanks for registering page感谢注册”这个页面。 

  如果你有大量重复的浏览器上要手动做的测试,你可以试试功能测试,但是要仔细不要把测试做的太细粒度了,因为这样维护起来会很麻烦。我知道这个结果是因为它在我身上发生了很多次。

  最常见的功能测试工具是Selenium。我们可以用Selenium WebDriver和 Protractor做功能测试,也可以用PhantomJS ,CasperJS,尤其是在你不需要在真正的浏览器上测试时。 

In closing

Unit tests should be your main focus when testing JavaScript code. They are the easiest to write and maintain, and provide many benefits beyond reducing bugs. Integration and functional tests should be in a supporting role, for where unit tests are not suitable.

For more on how unit tests relate to some other testing types, you should also check out my article discussing the differences between Unit Testing, TDD and BDD.

最后

  在javascript代码中,单元测试是最应该花费你的精力去写的。它们最容易写和维护,而且有很多好处,这些好处包括减少bug。当有的地方不适合写单元测试时,可以谢谢集成测试和功能测试。

  你如果想看更多的关于单元测试和其他测试的关系,你可以看看我的文章:the differences between Unit Testing, TDD and BDD

 
posted @ 2016-12-07 21:26  暖暖的寒寒  阅读(8513)  评论(0编辑  收藏  举报