Why should I bother with unit testing if I can just use integration tests?

前端 未结 12 1349
别那么骄傲
别那么骄傲 2021-02-03 21:53

Ok, I know I am going out on a limb making a statement like that, so my question is for everyone to convince me I am wrong. Take this scenario:

I have method A, which ca

12条回答
  •  醉梦人生
    2021-02-03 22:10

    Another reason that I haven't seen anyone else address: You will not always be the one to maintain the code you write. Let's assume I've written A & B in separate modules, so that each is in a separate jar & has its own maven build file. Let's further assume that A is a Dao method, and B is some sort of Service layer. If I have written even a basic unit test for my Dao (perhaps using test driven development, even!), I have much more confidence that future changes to the database structure & query that may effect my dao are caught early. If, for example, on my team, another developer needs to add columns to a database table that my dao uses, I certainly want them to know right away if doing so will break my dao methods. If they have to wait to build all layers & run an integration test to verify that, they're just wasting precious development time, in my opinion. As a developer, I'd much rather run a 10 second unit test 10 times to fix & test any breaks I find than to run a 2 minute integration build & integration test run multiple times. Obviously, the times vary greatly depending on the size of the project (and your hardware), but those are the kinds of times I'm dealing with on my current project.

提交回复
热议问题