VOOZH about

URL: https://thenewstack.io/expedia-3-tips-for-more-effective-unit-testing/

⇱ Expedia: 3 Tips for More Effective Unit Testing - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-01-30 03:00:18
Expedia: 3 Tips for More Effective Unit Testing
DevOps / Software Development

Expedia: 3 Tips for More Effective Unit Testing

There’s absolutely no point doing unit tests if they aren’t done correctly. Expedia engineer Lorenzo Dell’Arciprete points out common unit test mistakes that cause some to be ineffective.
Jan 30th, 2023 3:00am by Jessica Wachtel
👁 Featued image for: Expedia: 3 Tips for More Effective Unit Testing

There’s absolutely no point doing unit tests if they aren’t done correctly. Expedia’s Senior Software Engineer Lorenzo Dell’Arciprete recently wrote a helpful blog post that points out common unit test mistakes that cause some to be ineffective.

Writing unit tests is quite possibly the least interesting part of the software development process, except maybe for the moment of relief when they pass. Sometimes. Because not all passes are passes and not all fails are fails. It’s important to write tests that distinguish between the two.

Dell’Arciprete offered three tips to make unit testing more effective: testing for exceptions, working with unit test mocks, and using test data rather than production data. Read on to learn more about each of these.

Test for Exceptions

At first thought, it might seem simple to write a unit test to check if a specific illegal scenario will trigger a particular exception getting thrown.

👁 Image

The example above does check for the exception, meaning it will fail if anything other than MyCystomException is thrown. But something is still missing because if forbiddenMethod doesn’t throw at all, the test will still pass. In that case, the test isn’t a pass, the test is just slightly incomplete.

Dell’Arciprete offers two solutions — use a framework (the original post referenced JUnit) with a specifically designed feature for this instance or create the checks manually.

The framework provided feature looks like this:

👁 Image

With manual checks, the test should fail if it doesn’t follow the expected course which looks like this:

👁 Image

Reset the Mocks

Mocks are commonly used to simulate external states and/or behavior in unit testing. Consider the mock environment as a whole rather than each specific test when writing these tests. Consider the example below:

👁 Image

Run the tests in order and they’ll pass, but not in the reverse. The initial state of the test is mockExternalStore.isValid == true but because the second test alters the state and keeps it altered for future tests, the second test will pass but the first will automatically fail if run in reverse order.

Dell’Arciprete explained that writing a function to reset the mocks before each test solves this problem. Most frameworks have a setup function, likely named something similar to beforeEach, to ensure each test starts from the same context. The revised code is shown below.

👁 Image

Use Test Data in Tests

This one is simple and straightforward. Don’t use production data in unit testing, rather provide regular and edge case parameters for tests either manually or via a data provider (@DataProvider). He wrote that using production data in tests, “defeats the purpose of the test itself.”

Consider testing a hashing function based on a constant key. The function in production looks like this:

👁 Image

Meaning the unit test looks like this:

👁 Image

But then the thought creeps in, “the test will break if someone changes MY_KEY,” followed by the temptation to automate the generation of the parameters.Dell’Arciprete warns against that:

👁 Image

Heed the warning. The production code replication in the test here is clear, and while Dell’Arciprete does admit this is an “extreme” example, he follows the admission by saying that, “it can happen at a smaller scale unless you watch out for this.”

Conclusion

While there isn’t a wide delta between useful tests and sorta-useful tests, unit tests are nonetheless incredibly powerful when executed correctly. Dell’Arciprete provided specific examples but they can be applied more widely: Just a few simple tweaks can make a world of difference to your unit tests.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.