简体   繁体   English

集成测试只是具有更少模拟的单元测试吗?

[英]Is integration testing just unit testing with fewer mocks?

...or am I oversimplifying? ...还是我过于简化了?

And if I am oversimplifying, what really is integration testing, and why is it dramatically less a topic of conversation than unit testing*? 如果我过于简化,那么实际上什么是集成测试?为什么它比单元测试*少谈论很多话题?

*Evidence - How often each tag is used on StackOverflow: *证据-每个标签在StackOverflow上使用的频率:

  • Unit Testing - 8,595 单元测试-8,595
  • Integration Testing - 557 集成测试-557

Unit testing is testing one component on its own - you are testing a "unit" (often a single class or a single method). 单元测试是单独测试一个组件-您正在测试“单元”(通常是单个类或单个方法)。

Integration testing is testing two components working together - you are seeing if they "integrate" well together. 集成测试正在测试两个组件协同工作-您正在查看它们是否可以很好地“集成”在一起。

Unit testing is performed by mocking the external components your one unit under test collaborates with. 单元测试是通过模拟一个被测单元与之协作的外部组件来执行的。

In integration testing, you are testing using the real thing, not mocks. 在集成测试中,您是使用真实的东西而不是模拟进行测试。 Hence integration tests are not always so easy to automate and may need some manual involvement. 因此,集成测试并不总是那么容易实现自动化,并且可能需要一些人工参与。

Integration testing is a generic term for testing multiple 'units'. 集成测试是测试多个“单元”的通用术语。 These can be components, classes, regression or acceptance tests. 这些可以是组件,类,回归或验收测试。 This is probably why it is not as popular a term - because it can have several interpretations, whereas unit test is 'the smallest possible testable thing.' 这可能就是为什么它不那么受欢迎的原因-因为它可以有多种解释,而单元测试是“最小的可测试的东西”。

With respect to mocks, you shouldn't need too many mocks if you are unit testing. 关于模拟,如果您正在进行单元测试,则不需要太多模拟。 If you are using many mocks this could be a 'test smell' indicating you are testing a class that is too highly coupled to other classes. 如果您使用许多模拟,这可能是“测试气味”,表明您正在测试的类与其他类的耦合度太高。

You can think of unit testing as something a developer does, to make sure that each small unit (a method, for example) of his code is working as expected. 您可以将单元测试视为开发人员的工作,以确保其代码中的每个小单元(例如,一种方法)都能按预期工作。 Integration testing is when you (or a QA tester) tests multiple units in an integrated, or somewhat more end-to-end fashion. 集成测试是指您(或QA测试人员)以集成或更端到端的方式测试多个单元。

Integration testing probably reflects more on real world use cases than unit testing does. 集成测试可能比单元测试更多地反映了真实的用例。

You're... roughly right, but there's a lot of interpretation about this topic. 您...大致正确,但是对此主题有很多解释。 Unit testing can be (relatively) cleanly defined as testing of components in isolation for explicit functionality; 单元测试可以(相对)干净地定义为对显性功能进行隔离的组件测试; from what I've seen, integration testing gets defined as "pretty much everything else". 从我所看到的来看,集成测试被定义​​为“几乎所有其他内容”。 That's not to say there's not a well-defined place for it; 这并不是说没有一个明确的地方; even with good interface design, compatible components have nice and clean integration functionality, and that integration functionality can and should be tested independent of individual unit tests. 即使具有良好的界面设计,兼容的组件也具有良好而干净的集成功能,并且可以并且应该独立于各个单元测试来测试集成功能。 It's just that sometimes, tests begin to blur the line between unit and integration test. 只是有时候测试会模糊单元测试和集成测试之间的界限。 At the very least, you can usually look at a test, and say "can this be decomposed into something simpler", and if not, you can usually define it as a unit test; 至少,您通常可以查看一个测试,然后说“可以将其分解为更简单的东西”,否则,通常可以将其定义为单元测试。 if you can't do that, however, those seem to usually get lumped as integration tests. 如果您不能做到这一点,那么这些似乎通常会被集成测试所混淆。

Both "unit" and "integration" testing have specific meanings, which others have given here. “单元”和“集成”测试都具有特定的含义,其他人在这里给出了这些含义。 And that's fine for discussion on StackOverflow... 这对于在StackOverflow上进行讨论非常合适...

Problem is that just about everyone else uses "unit testing" generically to encompass both. 问题在于几乎所有其他使用“单元测试”来通用地包含这两者。 How often have you seen "unit testing" on a Gantt chart, or heard someone say, "and the developers will do their units tests..."? 您有多少次在甘特图上看到“单元测试”,或听到有人说“开发人员将进行单元测试...”? Neither case means only testing of individual classes in isolation. 两种情况都不意味着仅单独测试各个类。 Or, I hope not! 或者,我希望不要!

Worse, if you respond, "oh, do you mean the developers will do their unit and integration testing?" 更糟糕的是,如果您回答:“哦,您是说开发人员将进行他们的单元集成测试吗?” then you'll be looked at funny, because integration testing is what QA people do, right? 然后您会觉得很有趣,因为集成测试是质量检查人员的工作,对吗?

Personally, I really don't make a strict distinction, since I see it as a continuum (is two real classes and some mocks still a "unit" test?). 就我个人而言,我确实没有做严格的区分,因为我将其视为一个连续体( 两个真实的类和某些模拟仍然是“单元”测试吗?)。 What I'm ultimately interested in is "testing done by a developer to demonstrate their code does what they think it should do." 我最终感兴趣的是“由开发人员进行的测试,以证明他们的代码完成了他们认为应该做的事情”。 Then I try to use whatever term is used by others in whatever context. 然后,我尝试在任何情况下使用他人使用的任何术语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM