Do you want to share your content on R-bloggers? Click here if you have a blog, or here If you don’t.
Your code is often based on external systems-llms, APIs, databases or third-party libraries. Although these are essential for functionality, they do not have to be part of your tests.
Here is a practical pattern to test your code without trusting external dependencies, so that your tests remain quickly, reliable and cheap.
🧪 View this pattern how to test code with external dependencies.
🚫 Why immediately test external dependencies is a bad idea
If you test your code that an API mentions as it is:
- Tests will be slowBecause they have to wait until the external sources respond.
- Tests will be flakyBecause the external sources may not always be available or can return different results.
- Tests will be expensiveBecause you may have to pay for the external sources.
Or Tests are not written at allBecause there seem to be too many obstacles, it may not be worth it.
The alternative is to write Tests only for the code you own.
✅ Just test the code you own
The key is to test how your code behaves when dealing with external sources, without using the real one. Here is how:
- Make interfaces for interaction with the external sources.
- Use fake objects or spot to simulate the external sources in your tests.
- Write tests that verify the behavior of your code when dealing with the fake objects or spot.
- Use real objects in production code, but use fake objects or mocked in tests.
In this way you can test your code without trusting the external sources, making your tests quickly, reliable and cheap.
1. Make interfaces
Do not call a hard code to external sources. Instead of:
- Make interfaces that determine how your code talks to them.
- Use dependency injection to pass these interfaces on to your functions or classes.
This makes it easy to replace real implementations with fakes or spot during testing.
2. Use fake objects or spot
Simulate external systems in tests with fake objects or mocks:
- Offers the form and behavior of the expected API/database reactions.
- Boundary cases (eg time -outs, errors) in a predictable way.
This helps you to test how your code handles different scenarios without trusting the real service.
3. Write tests
Test the behavior of your code by claiming:
- Correct call to the fake/mocked interface.
- Correct handling of success, failure and marginal cases.
Follow the control – ACT – Assert Pattern for each test and keep each test focused on one expected outcome.
Make your objects in the same way as you would do in the production code, but use the fake objects or mocks instead of the real objects.
4. Use real objects in production code
In production:
- Install the real versions of the dependencies.
- Use one factory To make them. This keeps your production code clean and your tests flexible.
You can still overwrite dependencies in tests while using default values in production.
🧪 See the example about testing a function that {Ellmer} uses.
External sources are essential for your system, but they do not belong in your unit tests. By insulating them through interfaces and spot, you get faster, more reliable and more maintained tests – while keeping the production code just as clean.
Related
#code #tests #LLMS #APIs #databases #RBloggers


