简体   繁体   中英

Can someone give a concrete example of a unit test adding value when integration tests already exist?

Let's assume we are not doing TDD (for which unit tests are obviously part and parcel), and have integration tests for all the use cases.

The integration tests assume assume a certain input and validate the output is as expected.

My thinking is that adding a unit test for a method that is traversed in an integration test, using the same data as would exist in the method in the integration test , would not expose any additional bugs.

That would lead to the conclusion that provided you have suffcient integration tests you do not then need to unit test the same code.

So, can someone give a concrete example where a unit test could expose a bug in the above scenario?

Integration tests can be seen as a form of Acceptance Testing . They ensure that the software is doing what it is supposed to be doing.

Unit tests , on the other hand, aren't particularly useful for customers. A customer is not concerned that the InitializeServerConnection is failing, but they are concerned that they're unable to send internal messages to their co-workers as a result.

So what good are unit tests for? They are a development tool, full stop. A unit test verifies that a cog in the machine is working properly. And if it is not, it is very easy to see it failing .

Arialdo Martini offers a great explanation:

Oversimplifying, a software system can be seen as a network of cooperating modules. Since they cooperate, some of them depend on other.

[...]

With integration and end-to-end tests you would be able to find all the broken features. 系统测试

Yet, this is not of any help in guessing where the bug is. The same system, with the same bug, would result in these unit test failures:

单元测试

So, even though a unit test doesn't add any business value, it does add value in the form of reducing the amount of time spent manually testing, debugging, and sifting through code looking for the root cause of an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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