<?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 tests Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/mutation-tests/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/mutation-tests/</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=6.9.4</generator>

<image>
	<url>https://blog.yumasoft.pl/wp-content/uploads/2021/05/cropped-yumasoft_icon_transparent-32x32.png</url>
	<title>mutation tests Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/mutation-tests/</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>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>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>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><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>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>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>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>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>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>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>This is our production code:</p>



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



<p>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>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>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>Whoooaa&#8230; we see here some 82.14% value, not 100% anymore ? This is what I call <strong>mutant coverage</strong>.</p>



<p>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>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><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>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>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>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>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>
		<item>
		<title>How To Use StrykerJS With jest And TypeScript</title>
		<link>https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/</link>
					<comments>https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 14 Jul 2021 06:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jest]]></category>
		<category><![CDATA[mutation tests]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[stryker]]></category>
		<category><![CDATA[stryker mutator]]></category>
		<category><![CDATA[strykerjs]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=407</guid>

					<description><![CDATA[<p>Continuing the topic of frontend tests with jest, we&#8217;re taking a look at StrykerJS today. Stryker Mutator is one of the most popular tools for mutation testing. We recently struggled with configuring StykerJS to work properly with jest and TypeScript. I&#8217;d like to share our solution to these problems in this article. Jest config First,&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/">How To Use StrykerJS With jest And TypeScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Continuing <a href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">the topic of frontend tests</a> with <code>jest</code>, we&#8217;re taking a look at StrykerJS today. Stryker Mutator is one of the most popular tools for mutation testing. We recently struggled with configuring StykerJS to work properly with <code>jest</code> and TypeScript. I&#8217;d like to share our solution to these problems in this article.</p>



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



<h2 class="wp-block-heading">Jest config</h2>



<p>First, make sure that your <a href="https://jestjs.io/docs/configuration"><code>jest</code> configuration</a> is correct. If you&#8217;re with TypeScript (and you <a href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">should</a> ?), I recommend using <a href="https://www.npmjs.com/package/ts-jest">ts-jest</a> configuration preset. Our config looks as follows:</p>



<script src="https://gist.github.com/dsibinski/04419106e45b2136f63c2255a8a77fba.js"></script>



<p>If everything is fine, you should be able to execute <code>npm test</code> command (assuming it&#8217;s defined in <code>package.json</code> as <code>"test": "jest"</code>) without issues. All your tests should be executed.</p>



<p>We now need to make StrykerJS working with that.</p>



<h2 class="wp-block-heading">StrykerJS with jest configuration</h2>



<p>If you install <a href="https://www.npmjs.com/package/@stryker-mutator/core">StrykerJS</a> as described <a href="https://github.com/stryker-mutator/stryker-js">here</a>, you&#8217;d expect that <code>npx stryker run</code> command runs mutations and everything is just fine. However, if you&#8217;re having wierd issues and errors with that, it probably means that Stryker is not properly configured to execute tests with <code>jest</code>.</p>



<p>When you run <code>npx stryker run</code>, it creates mutants (modifications in your source code) and runs tests against them. It means that StrykerJS must run your tests in the same way you run them. So, if you run tests with <code>jest</code>, Stryker must be able to do the same.</p>



<p>We struggled with many issues related to running StrykerJS with <code>jest</code>. Some of them were related purely to <code>jest</code>, but some of them to TypeScript as well. After numerous trial-and-errors and consulting with StrykerJS community members, we worked out a configuration that finally works fine for us.</p>



<p>Going straight to the point, here&#8217;s the <code>stryker.conf.json</code> file that would make StrykerJS work fine with <code>jest</code> and TypeScript:</p>



<script src="https://gist.github.com/dsibinski/116b47fe141aaf7f71b2e8f1c83137b2.js"></script>



<p>To make it work, install the following npm packages: <a href="https://www.npmjs.com/package/@stryker-mutator/core">@stryker-mutator/core</a>, <a href="https://www.npmjs.com/package/@stryker-mutator/jest-runner">@stryker-mutator/jest-runner</a> and <a href="https://www.npmjs.com/package/@stryker-mutator/typescript-checker">@stryker-mutator/typescript-checker</a>.</p>



<p>Notice few important settings here:</p>



<ul class="wp-block-list"><li> <code>"checkers": ["typescript"]</code> &#8211; this enabled a <a href="https://stryker-mutator.io/docs/stryker-js/typescript-checker/">TypeScript checker</a> plugin, which makes mutations being checked for types before running tests on them. It means that if a mutation introduced by Stryker doesn&#8217;t compile because of types issues, it won&#8217;t be taken into account for mutations (which makes perfect sense)</li><li>&#8220;<code>disableTypeChecks</code>&#8221; &#8211; you still need to tell StrykerJS to not check types when compiling your code with type errors (mutants). It will compile them without types checking, but thanks to TypeScript checker plugin these ones will be filtered out for mutations</li><li><code>"tsconfigFile": "tsconfig.json"</code> &#8211; make sure to point to the TS config file you&#8217;re using here</li><li><code>"concurrency": 4</code>: this is another setting we experimented with to make Stryker working correctly. Without it, we were getting weird errors about lack of memory when running mutations (<code>FATAL ERROR: Committing semi space failed. Allocation failed - JavaScript heap out of memory</code>)</li><li><code>"testRunner": "jest"</code> &#8211; this one enables the <code>jest</code> as our tests runner</li><li><code>"jest"</code> &#8211; here you tell Stryker to use your <code>jest.config.js</code> file we talked about at the beginning</li></ul>



<p>With these settings, I hope that you are able to see this beautiful output of <code>npx stryker run</code> in your console:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="827" height="156" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result.png" alt="" class="wp-image-414" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result.png 827w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result-300x57.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/Stryker_mutations_result-768x145.png 768w" sizes="auto, (max-width: 827px) 100vw, 827px" /></figure></div>



<h2 class="wp-block-heading">StrykerJS with jest and TypeScript &#8211; summary</h2>



<p>I hope this is helpful for you. StrykerJS is a developing framework, so some things are still hard to figure out. We believe mutation tests are a great way of improving our tests, so we use them on a daily basis. If you ever have any issues with StrykerJS, I encourage you to reach out to <a href="https://join.slack.com/t/stryker-mutator/shared_invite/enQtOTUyMTYyNTg1NDQ0LTU4ODNmZDlmN2I3MmEyMTVhYjZlYmJkOThlNTY3NTM1M2QxYmM5YTM3ODQxYmJjY2YyYzllM2RkMmM1NjNjZjM">the community on Slack</a>. They are extremely helpful there ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/">How To Use StrykerJS With jest And TypeScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/07/how-to-use-strykerjs-with-jest-and-typescript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
