<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jordi Pellat on Medium]]></title>
        <description><![CDATA[Stories by Jordi Pellat on Medium]]></description>
        <link>https://medium.com/@j.pellat?source=rss-b7adc91e4d34------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wiceK8pQ9zlZmO3n.jpg</url>
            <title>Stories by Jordi Pellat on Medium</title>
            <link>https://medium.com/@j.pellat?source=rss-b7adc91e4d34------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 00:56:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@j.pellat/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The art of writing Trustful Tests]]></title>
            <link>https://medium.com/@j.pellat/the-art-of-writing-trustful-tests-35eb5c24d304?source=rss-b7adc91e4d34------2</link>
            <guid isPermaLink="false">https://medium.com/p/35eb5c24d304</guid>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[unit-testing]]></category>
            <dc:creator><![CDATA[Jordi Pellat]]></dc:creator>
            <pubDate>Wed, 12 Jun 2019 04:16:13 GMT</pubDate>
            <atom:updated>2019-06-12T04:16:13.160Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bQGclDLJYdYwS1AjyO1oNw.jpeg" /></figure><p>As engineers, we have the <strong>temptation to look inside the box to facilitate our testing</strong>, and it always comes with a toll. When our tests require context about the implementation details, we make these details part of the requirements of the tests.</p><p>The result is that we cannot refactor without changing the tests. If you’re not able to refactor your code, it rots.</p><p>You should be the one to decide <strong>which rules you want to enforce in your System</strong> and which you don’t. These rules should be up for change in the future. By setting up the right design of your tests and decouple from the details of implementation, you’ll be able to manipulate the behavior of your system without having to change your tests.<strong> You’ll have the certainty that the System fulfills the requirements you decided to enforce</strong>.</p><h3>The problem</h3><p>Imagine a system that implements a “Send Message” feature. Users can send text messages, and the receiver will get notified when that happens. The receiver can then retrieve the message by its ID. The <strong>simplified version of the code</strong> could look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e77e9d63f90501bf3faebe8adc1f5bb0/href">https://medium.com/media/e77e9d63f90501bf3faebe8adc1f5bb0/href</a></iframe><p>Now look at a typical way of testing this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53cc32b3656ec12ab4315e9003597ce4/href">https://medium.com/media/53cc32b3656ec12ab4315e9003597ce4/href</a></iframe><p>If you have a test like this, you’ll likely have a lot of them. Persisting data is a common feature in a system, and verifying the <em>save</em> method is called, will probably frequent in a lot of tests too.</p><p>What is the problem with this approach? Here’s the two main weakness of this design:</p><h4>It breaks the<a href="https://en.wikipedia.org/wiki/Single_responsibility_principle"> Single Responsibility Principle</a>:</h4><p>Tests, like any other object, should be <strong>responsible for a single part of the System</strong>. The responsibility for this test class is to <strong>verify the behavior of the “Send Message” feature</strong>. <br>Why does it have to know about what are the functions used to call the storage? Why does it have to know about how to get the storage or even the fact that the storage exists?<br>This test has now an <strong>added responsibility: “Know how the storage works”</strong> creating a dependency that will make our System harder to refactor.</p><h4>It tests implementation, not behavior:</h4><p>An excellent way to think about the behavior we want to test is to think about how to <strong>test it without using in the definition the details of the implementation</strong>. In this case, I would define the behavior of our system as:</p><blockquote>When a message is sent it can be recovered with it’s ID</blockquote><p>There’s no mention about the storage and definitively no mention about the <em>save() </em>method of the storage or the <em>get().</em> If one day I decide this kind of storage is not what our system needs, I should not need to change this test as this test should not depend on how it’s stored<strong>, the storage is just the easier implementation to make my tests pass.</strong></p><p>We test so we can verify the behavior of our system doesn’t change (we don’t add bugs) when we change the structure of our code (refactor). A corollary rule from this axiom is that <strong>our test code should not change when we refactor unit under test</strong>. On the current implementation of this test just changing the name of the method <em>save()</em> will force us to change the test code and, as a result, <strong>we cannot verify the behavior of the system is still the same.</strong></p><h3>An Example of how this approach breaks</h3><p>One day an infra engineer of your company brings up a decision made by their team. Developers were not using the interface to do atomic operations and were saving the objects through the save interface almost always. As a result, developers would have code looking like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a082eb79b77b85452751d9e94161d53/href">https://medium.com/media/6a082eb79b77b85452751d9e94161d53/href</a></iframe><p>Instead of using the right interface like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c7d1c40e82da4d9850a433b741ffd56/href">https://medium.com/media/7c7d1c40e82da4d9850a433b741ffd56/href</a></iframe><p>The infrastructure team is frustrated because they couldn’t get to enforce the atomic interface, so now they <strong>have decided that the method <em>save()</em> will be not enough to persist an object, after making all the <em>save()</em> you must call the function <em>flush()</em></strong>. They think this way, it will be easier to enforce atomic operations, and it will improve consistency and performance. They tell you it’s not optional. <strong>In one month if you don’t call </strong><em>flush()</em><strong> after performing </strong><em>save()</em><strong> the data will not be kept.</strong></p><p>Observe that no behavior of the system has changed, no Product Manager has come to you with a new policy for saving; it’s just an implementation detail. <strong>How can you ensure that all the places </strong><em>save()</em><strong> is called </strong><em>flush()</em><strong> will be called when it’s time to persist?</strong></p><p>When refactoring your code to add the <em>flush()</em> calls, <strong>you’ll have to change both tests and code.</strong> You’ll have to add logic in the tests to verify that not only <em>save()</em> has been called but also <em>flush(), </em>and that <em>flush()</em> was called after <em>save(). </em>You’ll have to do that for a lot of tests because saving is a widespread operation in the System. At the end you’ll find out:</p><ul><li>You have no way to know that flush is called in all places. <strong>So no way to tell if your system still works.</strong></li><li>The logic of the storage is hard to express in mocks.</li><li>That <strong>you’ll need to change all your tests, </strong>you have no way to know you haven’t introduced regressions.</li></ul><p>After one month everyone in the company is running because some weird bug has appeared, <em>flush()</em> was not called in an essential part of the System. How could this have been avoided?</p><h3>The solution</h3><p>Without changing any code on production, here’s how you could write this test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a98dd6de3d3bc12e2ce72fc6d017be8c/href">https://medium.com/media/a98dd6de3d3bc12e2ce72fc6d017be8c/href</a></iframe><p>With this change, we accomplish two goals:</p><ul><li><strong>We’re testing the behavior of the feature</strong>. The test is unaware of how the system under test is implemented, giving complete freedom for refactoring without having to change the test. On the example of how it breaks, this test never changes.</li><li><strong>We have a centralized definition of the storage to one place</strong>. It doesn’t matter the technique you’ll use; in this case, we’re making all our tests inherit from the company base test class implementation. The important part is that when one part of the system changes, it gets reflected only in one part of the code. That is a symptom of following the Single Responsibility Principle.</li></ul><p>Don’t take as an example injecting this dependency by mocking the function <em>get_storage(). </em>You should change that by the dependency injection solution you prefer. Depending on the language and environment, you can find the solution that better adapts to you. On the codebase I’m working right now we use a Service Locator.<strong> I recommend taking a read to </strong><a href="https://martinfowler.com/articles/injection.html"><strong>Martin’s Fowler article on this subject if you want to dig more</strong></a></p><h4>Building a Fake</h4><p>The <em>StorageFake</em> has the<strong> responsibility of centralizing the behavior of the storage</strong>. The reason why we need to develop an alternative implementation to the storage is that using the real storage in our tests would make them slow and probably flaky.</p><p>There are multiple ways of building this <em>FakeStorage, </em>and none that proves the best in all situations. You should adapt to your environment to take this decision. The rules that drive my decision are:</p><ul><li><strong>The simpler, the better:</strong> Fake objects have to be as simple as possible, so anyone can understand them and make it very hard to introduce bugs into them.</li><li><strong>The same for all the tests:</strong> The interfaces and functionality of the fake should be the same for all the tests. When the definition of these dependencies changes, there should be a centralized place where to change their behavior.</li><li><strong>Performant:</strong> It’s essential my tests run fast as they’re part of my Development Cycle (I use TDD adapted to my needs).</li></ul><p>In the codebase I’m currently working the Fake would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b524871cbdeee3c15416614ba431452/href">https://medium.com/media/0b524871cbdeee3c15416614ba431452/href</a></iframe><p>It behaves like a Storage, but <strong>instead of persisting in a Database</strong>, which is slow and could fail, <strong>it does it in a dictionary,</strong> which is fast and easy to use. The code can be understood very quickly, and writing takes no more than 5 minutes.</p><p>In our codebase, generating a new service for the production Service Locator forces the developer to create a fake version to be injected in the Service Locator for testing. It’s implementation grow depending on the needs of the team, and everyone reuses it as any call to this service in the testing environment will return the fake.</p><p>Now the system has a component that centralizes the logic of how the Storage behaves. We could introduce new requirements, like that the object saved hasn’t been saved before:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad5bcfaff6f0f4f65f904af37d1149de/href">https://medium.com/media/ad5bcfaff6f0f4f65f904af37d1149de/href</a></iframe><p>This change will detect all the units that are failing this requirement. We finally have control over how all the code interacts with the storage.</p><h3>Now it doesn’t break</h3><p>Let’s go back to our example about the infra engineer adding this <em>flush() </em>function. Now I only have to make a single change in my system to introduce the new requirement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36c6d46e519e1b005e4706033dbe44a0/href">https://medium.com/media/36c6d46e519e1b005e4706033dbe44a0/href</a></iframe><p>Observe how now, when you load an object, it will only return the objects that have been flushed. As a result, the test for send message will fail and without changing the code of the test. Now I can focus on making my refactor with safety:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94e58986435b45cb6119611287008d01/href">https://medium.com/media/94e58986435b45cb6119611287008d01/href</a></iframe><p><strong>The code of the test doesn’t change</strong></p><p><strong>FakeStorage simulates the behavior of the Storage without its limitations in performance</strong>. A single person can solve the <em>flush()</em> problem safely without the context of all the details about the business logic of the system.</p><h3>It’s not black and white</h3><p>Someone could ask, <strong>how do you know that this object has been saved in persistent storage and not in memory?</strong> I believe it for three reasons:</p><ul><li>In our current system, <strong>it’s harder to store objects in memory </strong>(create a dictionary and save objects, generate an id, … ) <strong>than calling the storage for persisting an object and getting an id</strong>. There’s no reason for an engineer to do so</li><li>I have tests that stop the system and restart it between calls for <strong>End To End integration</strong>.</li><li>If an engineer does <strong>it would be caught before landing</strong> in a Code Review or by a Linter</li></ul><p>These are the tradeoffs I choose to do as an engineer because of the environment I’m working on where we have protections against saving stuff in memory instead of in storage. But i<strong>f the same solution would work for all circumstances, engineering would be very easy.</strong></p><p>Your requirements could be different. Could be you want to make sure that the <strong>object has been saved in storage</strong> concretely doing something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4776599a3cc73c1e89c870606534f524/href">https://medium.com/media/4776599a3cc73c1e89c870606534f524/href</a></iframe><p>By doing this, <strong>you’re coupling your system to this fact there’s a storage</strong>, which could be a reasonable tradeoff if that’s part on how you define your behavior. The important part is that <strong>the logic of the dependency is still centralized</strong>, and we always test behavior. Now part of the system behavior is to use the storage interface to save which is very different than verifying the <em>save()</em> function has been called.</p><p>A friend of mine told me that <strong>he would like to control how many flushes</strong> have been made because performance is crucial for him. Then he could have something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0827581d7e8a9f9ab0623f78b030c4f5/href">https://medium.com/media/0827581d7e8a9f9ab0623f78b030c4f5/href</a></iframe><p>You could also <strong>enforce that all operations are atomic</strong> by enforcing that <em>flush()</em> only has been called once:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/173b6374daa09e373f726aa5ce01cb89/href">https://medium.com/media/173b6374daa09e373f726aa5ce01cb89/href</a></iframe><p>Now you have the tools to drive the System where you want safely, instead of being driven by the requirements. Now your tests are well abstracted</p><h3>Conclusion</h3><p>Your tests should be a well-designed system, following the same rules as the rest of your designs. Abstracting your dependencies and the details of implementation from your tests will improve your design.</p><p>Once done you’ll find a new set of tools for manipulating the behavior of your system which will help you adapt as new requirements from your dependencies arrive, and you refactor your code to maintain the quality that makes you performant.</p><p><strong>And Trustful Tests, helps you sleep better at night.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=35eb5c24d304" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>