Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration tests/unit tests: Doing too many integration tests?

I have been designing a number of unit tests that mock out each dependency that each Unit Has. This works as expected. I now need to do integration tests.

So for example, I have 1 layer that depends on 2 additional layers, I have tested each individual layer separately as a unit mocking each dependency out but I now need to perform integration tests.

So imagine I have the following three layers..

  Layer1 > Layer2 > Layer3

I can do an integration test on Layer1 which would has real instances (rather than mocks) for Layer2 and Layer3.

So should I do an integration test for Layer2? which include the following workflow

  Layer2 > Layer3

Again nothing is mocked out here, they are integration tests.

The problem I am seeing is that my integration test from Layer1 really covers the same integration test for Layer2.

I am not too sure if I am taking this a little too far. I know its better to have more tests rather than not enough but I am seeing duplicates when testing layer1

  Layer1 > Layer2 > Layer3

and layer2

  Layer2 > Layer3

So I could probably just test Layer1 - using integration tests

  Layer1 > Layer2 > Layer3

which would probably cover layer2 and layer3 integration tests

  Layer2 > Layer3 
like image 804
Martin Avatar asked May 11 '26 14:05

Martin


2 Answers

You should test whatever scenario is achievable in your application (via customer/components interactions). If Layer2 can interact with Layer3 without Layer1 interference - test for it.

Think of integration testing as of testing of entire use case. Is there a situation where your customer can start by invoking Layer2 alone? If there is, test it. If not, why would you do that? Never test something that is not used. This is the same as writing code that "someone might need later". It's waste of time, don't do that.

like image 124
k.m Avatar answered May 15 '26 07:05

k.m


I tend to define integration test loosely. If I see a test that covers the interaction between several classes, I tend to call it an integration test (there's another term for this, but I can't recall it right now). To me, it's not just about layer integration.

The idea is that you should do integration tests based on key scenarios and/or important use cases. From those things, you'll know where your integration tests should start from. Don't create integration tests for the sake of creating it. They tend to be too much work and maintenance when used for less important bits.

In your example, if you have an integration test on Layer1 which hits all three layers, then that covers your scenario adequately. As mentioned in an earlier answer, there's no need to purposely test Layer2 > Layer3 unless you're saying that something else may hit Layer2 and bypass Layer1 completely. But even if that were the case, I'd prepare an integration test starting from that "something else". Only test Layer2 > Layer3 when you have a use case that starts from Layer2.

like image 41
aberrant80 Avatar answered May 15 '26 08:05

aberrant80