Mocks are a power tool and you can use them to do things that they are not the right tool for the job.
Stop mocking your system
61–70 of 178 posts
Re: Stop mocking your system
#62However, for tests you want to run locally and quickly, the question should be, can you get the speed advantage of unit tests with the largest possible amount of integration? Can you only mock things that are "outside the system" (such as DB, networked services, and file system)?
IMO the biggest risk of integration testing is non-cleanup. I feel that is one of the major positive use cases for mocking. A mock DB will not retain garbage from previous tests, or previous runs of the same test.
Re: Stop mocking your system
#63I feel this can be summarised as "integration tests > unit tests". And yes. However, for tests you want to run locally and quickly, the question should be, can you get the speed advantage of unit tests with the largest possible amount of integration? Can you only mock things that are "outside the system" (such as DB, networked services, and file system)? IMO the biggest risk of integration testing is non-cleanup. I f…
Re: Stop mocking your system
#64Re: Stop mocking your system
#65It goes roughly like this:
- write one integration test for the "happy path".
- using some test coverage report, identify untested cases.
- write unit test for those.
I find I helps me find a good balance between time invested writing tests and benefits reaped from those tests.
Do you have a similar process?
Re: Stop mocking your system
#66Neither of these conditions is an argument against unit testing or mocking. Both indicate a fairly basic gap in how you're testing that should be fixed directly. If integration tests would catch this gap, you're still missing the basic thing you need while incurring the cost and risks of integration tests.
Re: Stop mocking your system
#67I feel this can be summarised as "integration tests > unit tests". And yes. However, for tests you want to run locally and quickly, the question should be, can you get the speed advantage of unit tests with the largest possible amount of integration? Can you only mock things that are "outside the system" (such as DB, networked services, and file system)? IMO the biggest risk of integration testing is non-cleanup. I f…
Containers do a good job on this. One should check out the Testcontainers[1] project that makes a good use of containers in language level. [1] https://github.com/testcontainers/testcontainers-go
The upside of mocks is that the values returned are literally hard coded.
Re: Stop mocking your system
#68I'm not at all an expert in testing; but I instinctively take the attitude that every line of test code is code that itself has to be tested, even though it's never going into production. I don't entirely trust that instinct, but I'm suspicious of test rigs and mocks. But I don't know how to test a "module" without them. Que faire.
Re: Stop mocking your system
#69Earlier quoted context omitted.
>I think this is supposed to mean using dependency injection instead. I'm pretty sure he means "just use integration tests".
I think so too. Unfortunately, integration tests are too slow, so the practice doesn't scale if one is trying to TDD. Insinuating integration testing into every user story will lead to friction. Test run times will balloon, cycle times will get extended and resentment will grow for the test suite and the team's testing regime. If your test suites cannot complete quickly (seconds), then they cost more than they're wor…
If integration tests get more useful outcomes than unit tests in some situation but TDD only works well with unit tests, maybe that means TDD isn’t the best process to use in that situation. Isn’t the essence of agile development to recognise what is working well and what is not, so you can make changes where they are needed?
If your test suites cannot complete quickly (seconds), then they cost more than they're worth.
I disagree with this. The goal of testing in software development is to improve results. Any testing methodology should be evaluated accordingly. Fast feedback is good, other things being equal. However, if going slower means getting more valuable feedback — identifying more defects, providing more actionable information about how to correct them, checking for a broader range of potential problems — then maybe going slower is the right choice.