Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2190
Abstract
DE like Rider <a href="https://xunit.net/docs/getting-started/netfx/jetbrains-rider">here</a>).</p><figure id="ef9a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ecwvl3qRSP7vYEfvs40U-g.png"><figcaption></figcaption></figure><p id="7d71">Edit Tests.csproj file to add a reference to the existing “Service” project and add the four missing packages (xUnit is already added).</p>
<figure id="36f1">
<div>
<div>
<iframe class="gist-iframe" src="/gist/odinnou/1c98f42e61302521ba05f923a18187ea.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="22cf">Then, add some business logic inside your Service: a panda renamer 🐼.</p><blockquote id="24cc"><p>💢 <b>WHAT?! That’s not TDD!
</b>Yeah, that’s irrelevant to the purpose of this article, but I intentionally left an interface unimplemented.</p></blockquote>
<figure id="f591">
<div>
<div>
<iframe class="gist-iframe" src="/gist/odinnou/5302c00e3c0e74795232a0eda8c81a08.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="6a2a">From this code, we could highlight two test cases.</p><ol><li>Providing an empty name to the Execute function, check if it threw an ArgumentNullException then verify if pandaFetcher has not been called.</li><li>Providing a valid name to the function, check if the new panda name is equal to the “newName” parameter.</li></ol><h1 id="3807">Combine forces</h1><p id="4c34">Now, we are ready to create our first unit test file named “PandaRenamerTest.cs” inside the “Tests” project.</p><p id="cec8">We’ll use the power of these four packages (xUnit, FluentAssertions, Moq and AutoFixture) to reduce the code complexity and increase reliability.</p>
<figure id="c0f8">
<div>
<div>
<iframe class="gist-iframe" src="/gist/odinnou/2d91c8195c3c90b84ec12f1b0e8908af.j
Options
s" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="5fbf">That’s all! Few lines of code, no fixture, and good readability.
From now on, I’m sure you’ll enjoy reaching the 80% code coverage.</p><h1 id="9986">Hold on!</h1><p id="4e6a">You’ve reached 80% of code coverage, great! You could now invest some time to ensure that the architecture will be followed throughout the project.</p><p id="753e">ArchUnitNET was built for this purpose if you want to check that all of your use cases avoid using code from “Service.Mordor.*” namespace, you could!</p>
<figure id="d5dd">
<div>
<div>
<iframe class="gist-iframe" src="/gist/odinnou/e386aa6f7b3d8ac998c3db0e224a4994.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="b3b3">If, unfortunately, a developer uses some code from the “Service.Mordor.*” namespace inside a “Core” use case, the unit test will fail with a clear error message.</p><figure id="3fef"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ReDW95Ez615BDCP0Ajs6fw.png"><figcaption></figcaption></figure><h1 id="7641">Conclusion</h1><p id="9293">I regret to inform you that 80% code coverage is not enough 🙃, but it’s a good start!
Indeed, the next step is to check if your code works well in its entirety.
We’ll use integration tests to reach ~90% code coverage in the next chapter.</p><p id="8ee7">You can access the entire source code of this post on <a href="https://github.com/odinnou/testing-right-way">GitHub</a>, stay tuned!</p><h1 id="3bc0">Edit #1</h1><p id="3f3f">As <a href="undefined">xeladu</a> pointed out: “A code coverage metric of 80% or similar doesn’t indicate that your testing is good.” and he’s right.
You should test your tests with mutation testing to increase theirs reliability, <a href="https://stryker-mutator.io/">Stryker</a> is a great tool!</p><h1 id="8366">Edit #2</h1><p id="bbd9">Updated to .NET 7.0 !</p></article></body>