<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mutation testing Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/mutation-testing/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/mutation-testing/</link>
	<description>Software development blog</description>
	<lastBuildDate>Mon, 23 Aug 2021 06:41:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://blog.yumasoft.pl/wp-content/uploads/2021/05/cropped-yumasoft_icon_transparent-32x32.png</url>
	<title>mutation testing Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/mutation-testing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Make Your Tests Bulletproof With Mutation Testing</title>
		<link>https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/</link>
					<comments>https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 23 Aug 2021 01:42:02 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mutation testing]]></category>
		<category><![CDATA[mutation tests]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=557</guid>

					<description><![CDATA[<p>What is mutation testing? How can we generate test cases &#8220;for free&#8221;? How to make our software tests bulletproof, so we can refactor the code with more confidence? At Yumasoft, we believe that testing is an inseparable part of software development. Mutation testing, which recently got a bit more attention in programming communities, is a&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/">Make Your Tests Bulletproof With Mutation Testing</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">What is mutation testing? How can we generate test cases &#8220;for free&#8221;? How to make our software tests bulletproof, so we can refactor the code with more confidence?</p>



<p class="wp-block-paragraph">At Yumasoft, we believe that <a href="https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/">testing</a> is an inseparable part of software development. Mutation testing, which recently got a bit more attention in programming communities, is a great way of improving our tests. In today&#8217;s article, we&#8217;re exploring what mutation testing is and why everyone should be using it ?</p>



<span id="more-557"></span>



<h2 class="wp-block-heading">What is mutation testing?</h2>



<p class="wp-block-paragraph">To make it clear, mutation testing isn&#8217;t just a new type of tests you can write. At least not in a casual sense of a test ?</p>



<p class="wp-block-paragraph"><strong>Mutation testing is a technique for validating</strong> <strong>your already-existing tests</strong>. They basically allow <strong>to test our tests</strong>.</p>



<p class="wp-block-paragraph">Mutation tests are not for people who don&#8217;t write tests (go away, we don&#8217;t like you!). They are for people who care about testing and want to make their tests better.</p>



<h2 class="wp-block-heading">How does mutation testing work?</h2>



<p class="wp-block-paragraph">Mutation tests introduce <em>mutations </em>into our source code under tests (you can call it production code). Mutations can be any changes in the source code you can think of. Let it be a modification of a condition within an <code>if</code> statement, replacement of a <code>string</code> value or change of an arithmetic operator &#8211; you name it. All you need to know is that <strong>mutation testing is based on automatically introducing changes to your source code</strong>.</p>



<p class="wp-block-paragraph">Our codebase with such a change (a single one) introduced is called a <em>mutant</em>. It makes sense &#8211; our production code is now somehow <em>mutated</em>.</p>



<p class="wp-block-paragraph">What happens with such a mutant then? All our existing tests are executed against it. Such tests&#8217; execution may produce two outcomes:</p>



<ul class="wp-block-list"><li>all tests have passed &#8211; that&#8217;s a <strong>bad scenario</strong>. It means that our <strong>mutant has <em>survived</em>.</strong> In other words, it means that a modification of our production source code wasn&#8217;t detected by our tests suite</li><li>at least one test has failed &#8211; that&#8217;s a <strong>good scenario</strong>. It means that our <strong>mutant has been <em>killed</em></strong>. This is what we want, because it means our tests are well-covering and detect changes in the production code.</li></ul>



<p class="wp-block-paragraph">You can see the flow of mutation testing on the following schema:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="353" height="580" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_MutationTestsSchema.png" alt="Mutation testing schema" class="wp-image-562" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_MutationTestsSchema.png 353w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/1_MutationTestsSchema-183x300.png 183w" sizes="(max-width: 353px) 100vw, 353px" /><figcaption>Source: <a href="https://peterevans.dev/posts/mutation-testing/">The blog of Peter Evans</a></figcaption></figure></div>



<h2 class="wp-block-heading">Example of mutation tests</h2>



<p class="wp-block-paragraph">To make mutation tests more practical, let&#8217;s see an example. We&#8217;ll be using <a href="https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/">StrykerJS </a>for production code written in TypeScript.</p>



<p class="wp-block-paragraph">This is our production code:</p>



<script src="https://gist.github.com/dsibinski/38b88a48d8a5a715a3745bc4e6954c3c.js"></script>



<p class="wp-block-paragraph">We have of course written some tests for this code. When we execute these tests with <a href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">jest</a>, we can see the following result:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img decoding="async" width="956" height="598" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/2_TestsFullCoverageExample.png" alt="Output of jest tests" class="wp-image-566" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/2_TestsFullCoverageExample.png 956w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/2_TestsFullCoverageExample-300x188.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/2_TestsFullCoverageExample-768x480.png 768w" sizes="(max-width: 956px) 100vw, 956px" /></figure></div>



<p class="wp-block-paragraph">So we have 100% coverage for <code>DiscountApplier</code> class! What&#8217;s more, we have 100% statements, branch, functions and line coverage! This sounds perfect, right? Why should we bother more? ?</p>



<p class="wp-block-paragraph">Let&#8217;s deep dive and run StrykerJS mutator against our app. After <a href="https://stryker-mutator.io/docs/stryker-js/getting-started/">installing</a> and <a href="https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/">configuring Stryker</a> properly, we can execute <code>npx stryker run</code> command. That&#8217;s the final result:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img decoding="async" width="650" height="101" src="https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_StrykerRunResult.png" alt="Output of npx stryker run command. 46 mutants killed, 10 survived, 82.14% mutant coverage" class="wp-image-567" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_StrykerRunResult.png 650w, https://blog.yumasoft.pl/wp-content/uploads/2021/08/3_StrykerRunResult-300x47.png 300w" sizes="(max-width: 650px) 100vw, 650px" /></figure></div>



<p class="wp-block-paragraph">Whoooaa&#8230; we see here some 82.14% value, not 100% anymore ? This is what I call <strong>mutant coverage</strong>.</p>



<p class="wp-block-paragraph">Remember what we said above about survived and killed mutants? Pay attention to our mutations results above &#8211; we have <strong>46 mutants killed</strong>, but <strong>10 of them survived</strong>. It means that we have a potential to add more tests to cover these scenarios.</p>



<p class="wp-block-paragraph">We will not be going through each mutant and adding the test for it in this article, but I can tell you what these results can give you. In the beginning, <strong>when our mutant coverage was 82.14% with 10 mutants survived, we had 6 unit tests</strong> written for <code>DiscountApplier</code> class. In the end, after working out and killing each mutant one-by-one, <strong>we finished with 12 unit tests and 100% mutant</strong> coverage.</p>



<p class="wp-block-paragraph"><strong>In 10 minutes</strong> <strong>(time needed to add these 6 tests), I got</strong> <strong>twice as many unit tests as in the beginning</strong>. Now, both &#8220;traditional&#8221; and mutant coverages are 100%. My confidence in refactoring or modifying the production code is much higher now. Notice that we got this all &#8220;for free&#8221;. Mutation testing tool (StrykerJS in that case) did everything for us. This is huge! ?</p>



<h2 class="wp-block-heading">How to start writing mutation tests</h2>



<p class="wp-block-paragraph">Just find a proper mutation testing tool for the framework/programming language you work with. Here are a few most popular examples:</p>



<ul class="wp-block-list"><li>JavaScript/TypeScript/C#/Scala: <a href="https://stryker-mutator.io/">Stryker mutator</a></li><li>Ruby: <a href="https://github.com/mbj/mutant">mutant</a></li><li>Java: <a href="https://pitest.org/">Pitest</a></li><li>PHP: <a href="https://infection.github.io/">Infection</a></li></ul>



<p class="wp-block-paragraph">Go straight to the docs of your tool of choice and start mutating ? The messages about each mutant are very clear, so adding new tests to kill them will be very easy. It&#8217;s like someone sitting next to you and telling you exactly what test you should add for your code. That&#8217;s actually a pretty decent solution for anyone who doesn&#8217;t like writing tests ? </p>



<p class="wp-block-paragraph">I don&#8217;t see any reason why anyone would not use mutation testing. It&#8217;s such a great tooling that I don&#8217;t see any disadvantages in using it. It can greatly help you with TDD as well as an additional &#8220;layer&#8221; of inventing new test cases and patching your code. Believe me, you&#8217;ll see the benefits straightaway.</p>



<p class="wp-block-paragraph">So &#8211; download one of the mutation tools and make your tests bulletproof today ? Or at least see how bad they are now ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/">Make Your Tests Bulletproof With Mutation Testing</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
