java 究竟什么是集成测试 - 与单元相比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16721443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:47:10  来源:igfitidea点击:

Exactly what is integration testing - compared with unit

javaphpunit-testingtestingintegration-testing

提问by Marty Wallace

I am starting to use unit testing in my projects, and am writing tests that are testing at the method/function level.

我开始在我的项目中使用单元测试,并且正在编写在方法/函数级别进行测试的测试。

I understand this and it makes sense.

我明白这一点,这是有道理的。

But, what is integration testing? From what i read it moves the scope of testing up to test larger features of an application.

但是,什么是集成测试?从我读到的内容来看,它将测试范围扩大到测试应用程序的更大功能。

This implies that I write a new test suite to test larger things such as (on an e-commerce site) checkout functionality, user login functionality, basket functionality. So here i would have 3 integration tests written?

这意味着我编写了一个新的测试套件来测试更大的东西,例如(在电子商务网站上)结帐功能、用户登录功能、购物篮功能。所以在这里我将编写 3 个集成测试?

Is this correct - if not can someone explain what is meant.

这是正确的 - 如果不是,有人可以解释是什么意思。

Also, does integration test involve the ui (web application context here) and would employ the likes of selenium to automate. Or is integration testing still at the code level but tying together difference classes and areas of the code.

此外,集成测试是否涉及 ui(此处为 Web 应用程序上下文),并且会使用 selenium 之类的工具进行自动化。或者集成测试仍然在代码级别,但将不同的类和代码区域联系在一起。

回答by Kna?is

Consider a method like this PerformPayment(double amount, PaymentService service);

考虑这样的方法PerformPayment(double amount, PaymentService service)

An unit test would be a test where you create a mock for the serviceargument.

单元测试是为service参数创建模拟的测试。

An integration test would be a test where you use an actual external service so that you test if that service responds correctly to your input data.

集成测试是一种测试,您使用实际的外部服务来测试该服务是否正确响应您的输入数据。

回答by Dherik

Unit tests are tests that the tested code is inside of the actual class. Another dependencies of this class are mocked or ignored, because the focus is test the code insidethe class.

单元测试是测试代码在实际类中的测试。这个类的另一个依赖被模拟或忽略,因为重点是测试类内部的代码。

Integration tests are tests that involves disk access, application service and/or frameworks from the target application. The integration tests run isolatedfrom another external services.

集成测试是涉及来自目标应用程序的磁盘访问、应用程序服务和/或框架的测试。集成测试与其他外部服务隔离运行。

I will give an example. You have a Spring application and you made a lot of unit tests to guarantee that the business logic is working properly. Perfect. But what kind of tests you have to guarantee:

我举一个例子。您有一个 Spring 应用程序,并进行了大量单元测试以保证业务逻辑正常工作。完美的。但是你必须保证什么样的测试:

  • Your application service can start
  • Your database entity is mapped correctly
  • You have all the necessary annotations working as expected
  • Your Filteris working properly
  • Your API is accepting some kind of data
  • Your main feature is reallyworking in the basic scenario
  • Your database query is working as expected
  • Etc...
  • 您的应用程序服务可以启动
  • 您的数据库实体已正确映射
  • 你有所有必要的注释按预期工作
  • Filter的工作正常
  • 您的 API 正在接受某种数据
  • 您的主要功能在基本场景中确实有效
  • 您的数据库查询按预期工作
  • 等等...

This can't be done with unit tests but you, as developer, need to guarantee that all things are working too. This is the objective of integration tests.

这不能通过单元测试来完成,但作为开发人员,您需要保证所有事情也都正常工作。这是集成测试的目标。

The ideal scenario is the integration tests running independent from another external systems that the application use in a production environment. You can accomplish that using Wiremock for Rest calls, a memory database like H2, mocking beans from some specific classes that call external systems, etc.

理想的场景是集成测试独立于应用程序在生产环境中使用的另一个外部系统运行。您可以使用 Wiremock 进行 Rest 调用、像 H2 这样的内存数据库、来自调用外部系统的某些特定类的模拟 bean 等来实现这一点。

A little curiosity, Maven have a specific plugin for Integration Tests: the maven failsafe plugin, that execute test classes that the name ends with IT(by default). Example: UserIT.java.

有点好奇,Maven 有一个特定的集成测试插件:maven failsafe plugin, 执行名称以IT结尾的测试类(默认情况下)。例子:UserIT.java

The confusion about what Integration Test means

关于集成测试意味着什么的困惑

Some people understand the "integration test" as a test involving the "integration" to other external systems that the currently system use. This kind of tests can only be done in a environment where you have allthe systems up and running to attend you. Nothing fake, nothing mocked.

有些人将“集成测试”理解为涉及与当前系统使用的其他外部系统“集成”的测试。这种测试只能在所有系统都已启动并运行的环境中进行。没有虚假,没有嘲笑。

This might be only a naming problem, but we have a lack of tests (what I understand as integration tests) that attends the necessity of the items described above. On contrary, we are jumping for a definition of unit tests (test class only) to a "integration" test (the whole real systems up). So what is in the middle of it if not the integration tests?

这可能只是一个命名问题,但我们缺乏满足上述项目必要性的测试(我理解为集成测试)。相反,我们正在将单元测试的定义(仅限测试类)跳转到“集成”测试(整个真实系统)。那么如果不是集成测试,它的中间是什么?

You can read more about this confusion on this articleby Martin Fowler. He separates the "integration tests" term on two meanings: the "broad" and "narrow" integration tests:

您可以Martin Fowler 的这篇文章中阅读有关这种混淆的更多信息。他将“集成测试”一词分为两种含义:“广泛”和“狭义”集成测试:

narrow integration tests

  • exercise only that portion of the code in my service that talks to a separate service
  • uses test doubles of those services, either in process or remote
  • thus consist of many narrowly scoped tests, often no larger in scope than a unit test (and usually run with the same test framework that's used for unit tests)

broad integration tests

  • require live versions of all services, requiring substantial test environment and network access
  • exercise code paths through all services, not just code responsible for interactions

狭窄的集成测试

  • 仅在我的服务中执行与单独服务通信的那部分代码
  • 使用这些服务的测试替身,无论是在进程中还是远程
  • 因此由许多范围狭窄的测试组成,范围通常不大于单元测试(并且通常使用与单元测试相同的测试框架运行)

广泛的集成测试

  • 需要所有服务的实时版本,需要大量的测试环境和网络访问
  • 在所有服务中执行代码路径,而不仅仅是负责交互的代码

You can get even more details on this article.

您可以获得有关本文的更多详细信息。

回答by Serberuss

Unit testing is where you are testing your business logic within a class or a piece of code. For example, if you are testing that a particular section of your method should call a repository your unit test will check to make sure that the method of the interface which calls the repository is called the correct number of times that you expect, otherwise it fails the test.

单元测试是您在类或一段代码中测试业务逻辑的地方。例如,如果您正在测试方法的特定部分应该调用存储库,您的单元测试将检查以确保调用存储库的接口方法的调用次数与您期望的正确次数相同,否则将失败考试。

Integration testing on the other hand is testing that the actual service or repository (database) behavior is correct. It is checking that based on data you pass in you retrieve the expected results. This ties in with your unit tests so that you know what data you should retrieve and what it does with that data.

另一方面,集成测试是测试实际服务或存储库(数据库)行为是否正确。它正在检查根据您传入的数据检索预期结果。这与您的单元测试相关联,以便您知道应该检索哪些数据以及它对这些数据的作用。

回答by pappati

As far as I see the selenium tests should be in another test suite. Those tests are the most fragile test in nature even if you write them correctly. Here you can use Specflow or some other kind of specification by example framework. Perhaps you can call these tests as acceptance tests. These are for developers and business experts too. The integration, or module tests normally do not use UI. The integration tests exercise some classes which are working together. These are lower level tests than the selenium tests, and a bit easier to maintain. These tests are for developers only.

据我所知,硒测试应该在另一个测试套件中。即使您正确编写,这些测试也是自然界中最脆弱的测试。在这里,您可以通过示例框架使用 Specflow 或其他类型的规范。也许您可以将这些测试称为验收测试。这些也适用于开发人员和业务专家。集成或模块测试通常不使用 UI。集成测试练习了一些协同工作的类。这些是比 selenium 测试更低级别的测试,并且更容易维护。这些测试仅供开发人员使用。

回答by ottodidakt

Here are a couple of constraints that a good unit test satisfies. Meeting these constraints also required good testable code.

下面是一个好的单元测试满足的几个约束。满足这些约束还需要良好的可测试代码。

  1. No I/O - disk or network
  2. Only one assertion (if multiple, they should be minor variations of each other)
  3. Does not exercise (cover) much more production code than what it asserts
  1. 无 I/O - 磁盘或网络
  2. 只有一个断言(如果有多个,它们应该是彼此的微小变化)
  3. 不会执行(涵盖)比它所声明的更多的生产代码

These constraints usually don't apply to integration tests.

这些约束通常不适用于集成测试。