<?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>testing Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/testing/</link>
	<description>Software development blog</description>
	<lastBuildDate>Thu, 21 Oct 2021 02:37:53 +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>testing Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/testing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>When Not To Use TDD?</title>
		<link>https://blog.yumasoft.pl/2021/10/when-not-to-use-tdd/</link>
					<comments>https://blog.yumasoft.pl/2021/10/when-not-to-use-tdd/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Thu, 21 Oct 2021 06:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=719</guid>

					<description><![CDATA[<p>Some time ago we talked about 7 reasons to start using Test Driven Development. As soon as you learn it and start practicing it on a daily basis, it might be tempting to always be TDDing. However, TDD is not always a good fit. There are some cases when it might not be worth using&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/10/when-not-to-use-tdd/">When Not To Use TDD?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Some time ago we talked about <a href="https://blog.yumasoft.pl/2021/09/7-reasons-to-start-doing-test-driven-development/">7 reasons to start using Test Driven Development</a>. As soon as you learn it and start practicing it on a daily basis, it might be tempting to always be <em>TDDing</em>. However, TDD is not always a good fit. There are some cases when it might not be worth using TDD. Let&#8217;s explore them today ?</p>



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



<h2 class="wp-block-heading">Prototypes</h2>



<p>You may consider not doing TDD with prototypes. By <em>prototype</em>, I mean an app that will most probably be thrown away or completely changed very soon. It may be created when you are experimenting with some technology/framework or are just asked to <em>conduct research</em> on something.</p>



<p>TDD gives you a high code/flow coverage, which may need to be changed in case of prototype projects. If you are in such situation, consider resigning from TDD at this stage.</p>



<h2 class="wp-block-heading">Short living apps</h2>



<p>Similarly to prototypes, short living solutions might be the ones where TDD is not the best fit. It applies to one-time-use apps or scripts which are supposed to do one thing well and quickly.</p>



<p>Short living apps are the ones where you may consider not using Test Driven Development. I&#8217;d even argue whether you need any kind of tests for such programs. Testing such apps or even scripts may be art for art&#8217;s sake. If something is supposed to realize a given task without fireworks (like reusability, maintenance etc.), I wouldn&#8217;t bother with testing it. It&#8217;s better to focus on solving your current, short-lived problem than on practicing the profound TDD idea ?</p>



<h2 class="wp-block-heading">Unclear requirements</h2>



<p>One of the reasons not to use TDD might be unclear requirements. The idea of writing tests in Test Driven Development is to define the expected outcomes for predefined incomes. As we said <a href="https://blog.yumasoft.pl/2021/09/7-reasons-to-start-doing-test-driven-development/">in the previous article</a>, Test Driven Development can help spot the edge cases and make the business requirements more <em>close to the code</em>. However, if you don&#8217;t have these requirements defined, TDD might be a waste of time (at this stage). If the requirements actually arrive or are completely changed, your significant set of tests will need to be changed or even removed.</p>



<h2 class="wp-block-heading">MVP</h2>



<p>If you&#8217;re creating an <a href="https://en.wikipedia.org/wiki/Minimum_viable_product">MVP</a>, with very short time and budget, TDD might not be worth the struggle. It&#8217;s probably better to resign from using TDD in that case. You should rather focus on your product and the business value it needs to deliver. TDD might slow down the delivery time, which, in case of MVPs, can be critical. It&#8217;s better to deliver the minimum viable product without TDD than to not deliver it with TDD ?</p>



<p>However, remember to add some tests, even post-factum. MVPs have a high chance of survival, so you&#8217;d want your final product to be tested and testable. You need to find a good balance here, not to make your solution completely untestable when it&#8217;s ready for production use.</p>



<h2 class="wp-block-heading">Tests writing beginner</h2>



<p>If you are a total beginner in writing tests in general, Test Driven Development might not be the best approach to start with. You may consider not to use TDD at the beginning of your testing journey.</p>



<p>To make Test Driven Development effective, you should be able to write tests quickly and efficiently. Starting to learn software testing with Test Driven Development may make you feel overwhelmed and discouraged. If you&#8217;re a beginner, I suggest to first write some unit tests, get familiar with that and then try to learn Test Driven Development. </p>



<p>It would be great if you can find some smaller and well-defined task to start with TDD. If you don&#8217;t have such tasks, try solving one of the programming katas (e.g. <a href="https://codingdojo.org/kata/StringCalculator/">this one</a>) with TDD. </p>



<h2 class="wp-block-heading">When not to use TDD &#8211; summary</h2>



<p>Like everything, TDD is not a gold standard for every possible case. It should be used as often as possible, but not always. Remember to always weight the gains and losses from using Test Driven Development in a given project or application at a given time.</p>



<p>What&#8217;s your experience with Test Driven Development? Can you list any more cases where it&#8217;s not worth using it? Share your thoughts in the comments below!</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/10/when-not-to-use-tdd/">When Not To Use TDD?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/10/when-not-to-use-tdd/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>7 Reasons To Start Doing Test Driven Development</title>
		<link>https://blog.yumasoft.pl/2021/09/7-reasons-to-start-doing-test-driven-development/</link>
					<comments>https://blog.yumasoft.pl/2021/09/7-reasons-to-start-doing-test-driven-development/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 20 Sep 2021 10:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=656</guid>

					<description><![CDATA[<p>Why should you start doing Test Driven Development (TDD) today? What are the benefits of TDD, and how can it help the developers and business? Let&#8217;s explore 7 reasons everyone should be TDD-ing ? Moving design into code With test driven development, you create your tests before writing production code*. Having that in mind, one&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/09/7-reasons-to-start-doing-test-driven-development/">7 Reasons To Start Doing Test Driven Development</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Why should you start doing Test Driven Development (TDD) today? What are the benefits of TDD, and how can it help the developers and business? Let&#8217;s explore 7 reasons everyone should be TDD-ing ?</p>



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



<h2 class="wp-block-heading">Moving design into code</h2>



<p>With test driven development, you create your tests before writing production code*.</p>



<p>Having that in mind, one of the benefits of using TDD is that you need to <strong>move your design requirements into code before you even write a single line of business logic</strong>.</p>



<p>It means that you need to spend time thinking about <a href="https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/">good names</a> for your tests. I often create empty tests straightaway after staring to work on a task. Spending time to create well-named test methods can help you quickly spot missing parts in the design.</p>



<p><span style="text-decoration: underline;">*By <em>production code</em> I mean the actual business logic <em>except</em> the tests&#8217; code. This is only to differentiate here, but I personally consider my tests part of the production code in real life. Tests are as important as all the code.</span></p>



<h2 class="wp-block-heading">Finding edge cases</h2>



<p>Going further, if you create many empty tests with well-thought names, you will surely find many edge cases. Thanks to test driven development, <strong>you&#8217;ll verify the business requirements by your code <em>without </em>writing even a single line of it</strong>. Discovering edge cases and boundary conditions will let you <strong>clarify the requirements or even change/adjust them <em>before </em>any production code is written</strong> ?</p>



<h2 class="wp-block-heading">Structuring your code beforehand</h2>



<p>You&#8217;ll see that TDD forces you to think about the code you&#8217;re about to write beforehand. You&#8217;re going to think about production code to-be-written: how and where to add it, what methods, parameters and classes are needed. Thanks to that, you can quickly notice that maybe the place you&#8217;d normally place the new code in is not the best choice. Maybe it requires too much changing instead of modifying.</p>



<p>Such approach leads to <strong>taking and rethinking architectural and structural decisions before writing a single line of production code</strong>. In the end, it saves you a lot of time. Without TDD, you&#8217;d probably just add this new code where you thought it&#8217;s appropriate and struggle to refactor it later. And refactoring without tests is dangerous ?</p>



<h2 class="wp-block-heading">Allowing to refactor</h2>



<p>Test driven development is based on <a href="https://blog.cleancoder.com/uncle-bob/2014/12/17/TheCyclesOfTDD.html">red-green-refactor cycle</a>. <strong>As soon as your test is green, you can safely (for sure much more safely than without tests, especially if you combine TDD with <a href="https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/">mutation testing</a>) refactor the code you&#8217;ve just written</strong>.</p>



<p>In the end, the quality of your code is much higher than it would be without tests. You don&#8217;t feel the <em>fear of refactoring</em>, which is a common blocker for improving code quality in legacy systems.</p>



<h2 class="wp-block-heading">Making implementation very easy</h2>



<p>When you move from red to green phases of TDD, it&#8217;s time for implementation. From my experience, <strong>writing production code that satisfies TDD-produced tests is very easy</strong>. </p>



<p>I find it much easier than writing code not covered by TDD-based tests. You exactly know what to do and which tests must pass. Thanks to that, your implementation is minimal &#8211; it should only make the test(s) green.</p>



<h2 class="wp-block-heading">Creating current code documentation</h2>



<p>Implementing a new feature with test driven development will leave you with a comprehensive set of tests. From my experience, I write few times more tests with TDD than in a &#8220;traditional&#8221; <em>tests-after-code</em> mode.</p>



<p>As a nice side effect, <strong>you&#8217;re building and growing the only possible, current documentation of your code &#8211; set of your tests</strong>. Only tests can tell you the truth about your code. It can&#8217;t be achieved with any other kind of documentation. Notice one more advantage &#8211; <strong>you&#8217;re creating documentation&#8230; <em>without </em>writing any documentation!</strong> ?</p>



<p>Current, well-named tests are very helpful not only for developers, but also for the so-called <a href="https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/#How_naming_tests_well_can_help_the_business">business people</a>.</p>



<h2 class="wp-block-heading">Spotting architectural problems</h2>



<p>Last but not least, test driven development helps to solve application&#8217;s architectural problems. Tests should be relatively easy to write.<strong> If the test is hard to write before production code exists, it might mean that the architecture of your solution is wrong.</strong> There&#8217;s a problem somewhere that should be addressed.</p>



<p>That&#8217;s why it&#8217;s very important to start working with TDD as early as possible in your project. There might be some cases where TDD it not suitable (maybe I&#8217;ll write a separate article about it ?), but in quite mature and stable projects, it should be a gold standard. Altering the architecture is much easier early in the project. </p>



<p></p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/09/7-reasons-to-start-doing-test-driven-development/">7 Reasons To Start Doing Test Driven Development</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/09/7-reasons-to-start-doing-test-driven-development/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Run Cypress Tests From NUnit Test (C#)</title>
		<link>https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/</link>
					<comments>https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 13 Sep 2021 13:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[end-to-end tests]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=623</guid>

					<description><![CDATA[<p>In one of our projects, we recently started using Cypress for end-to-end testing. Cypress executes tests written in JavaScript or TypeScript. However, we wanted to first perform some data-preparation operations using C#. In fact, we needed to run Cypress tests from NUnit C# tests. In this article, I&#8217;m sharing how this can be done ?&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/">Run Cypress Tests From NUnit Test (C#)</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In one of our projects, we recently started using <a href="https://www.cypress.io/">Cypress</a> for end-to-end testing. Cypress executes tests written in JavaScript or TypeScript. However, we wanted to first perform some data-preparation operations using C#. In fact, we needed to run Cypress tests from NUnit C# tests.</p>



<p>In this article, I&#8217;m sharing how this can be done ? It will also be useful if you use another testing framework than NUnit, but the examples are based on it.</p>



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



<h2 class="wp-block-heading">Starting point</h2>



<p>You can find the code accompanying this article here: <a href="https://github.com/dsibinski/cypress-csharp-example">https://github.com/dsibinski/cypress-csharp-example</a></p>



<p>Our starting point is a React application with Cypress tests:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="888" height="332" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest.png" alt="React app with Cypress test" class="wp-image-628" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest.png 888w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest-300x112.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/1_WebAppCypressTest-768x287.png 768w" sizes="(max-width: 888px) 100vw, 888px" /></figure></div>



<p>I assume that your tests specs are placed within <code>cypress/integration</code> folder. The only spec we have is called <code>submit_spec.ts</code> and it placed in a subfolder called <code>form</code>.</p>



<p>Now imagine, that your backend is written in C#. Maybe you&#8217;d like to perform some data initialization <em>before </em>Cypress tests are executed.</p>



<p>In our case, we have a C# backend, and we&#8217;ve already had a mechanism for running integration tests. This mechanism runs our server app with an in-memory database, which makes the maintenance of tests very easy.</p>



<p>We wanted to use the same mechanism for our new, E2E Cypress tests. It means that we would be able to write some C# initialization code (data creation mostly) and then run Cypress tests from NUnit test.</p>



<p>What we want to achieve is to have a C#-Cypress NUnit test look as follows:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img decoding="async" width="382" height="162" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/2_RunningCypressTestFromCSharpIdea.png" alt="The picture shows how to run cypress tests from nunit by only calling RunCypressTest(&quot;form/submit_spec.ts&quot;) - the method we will add soon" class="wp-image-630" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/2_RunningCypressTestFromCSharpIdea.png 382w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/2_RunningCypressTestFromCSharpIdea-300x127.png 300w" sizes="(max-width: 382px) 100vw, 382px" /></figure></div>



<p>Let&#8217;s now see how we can implement the <code>RunCypressTest</code> method ?</p>



<h2 class="wp-block-heading">Implementing a method to run Cypress tests from NUnit</h2>



<p>To make things easier, let&#8217;s create a base class all our test classes can use. We&#8217;ll call it <code>CypressTestsBase</code> and add a <code>RunCypressTest</code> method to it: </p>



<script src="https://gist.github.com/dsibinski/70631294e17f4a0230f4fdbbfbc71fe1.js"></script>



<p>This code does the job. Few interesting parts:</p>



<ul class="wp-block-list"><li>Line 12 &#8211; I&#8217;m getting there the path to the web app workspace where <code>npx cypress run</code> command can be executed. It might be more complicated in your scenario, so you should adjust accordingly here</li><li>Lines 13-20: thanks to setting <code>RedirectStandardInput</code> to <code>true</code>, we can later execute a custom command via <code>cmd</code>; setting <code>RedirectStandardOutput</code> to <code>true</code> allows us to write everything <code>npx cypress run</code> command does to NUnit test&#8217;s output</li><li>Line 28: <code>TestContext.Out.WriteLine(cypressProcessOutput);</code> actually writes the Cypress output to NUnit test result</li><li>Line 29: here we handle errors during Cypress execution. <code>npx cypress run</code> executed via <code>cmd</code> will return a code different from 0 if anything goes wrong.</li></ul>



<p>We can now change <code>FormTests</code> class to run its corresponding Cypress test:</p>



<script src="https://gist.github.com/dsibinski/758bee0d1f3cee14375ebe65b393d5ef.js"></script>



<p>Finally, it allows to run our C# test, which then runs its corresponding Cypress spec and prints the output:</p>



<div class="wp-block-image"><figure class="aligncenter size-full is-resized"><img decoding="async" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/3_CypressTestResultInNUnitResult-1.png" alt="Run Cypress tests from NUnit - sample passed test in JetBrains Rider" class="wp-image-638" width="706" height="307" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/3_CypressTestResultInNUnitResult-1.png 706w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/3_CypressTestResultInNUnitResult-1-300x130.png 300w" sizes="(max-width: 706px) 100vw, 706px" /></figure></div>



<h2 class="wp-block-heading">Cypress errors in NUnit result</h2>



<p>Let&#8217;s see what happens if something is broken on Cypress/web app&#8217;s side. Let&#8217;s run our test, but with our web app not running.</p>



<p>The NUnit test fails, and we can clearly see what was wrong:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="999" height="323" src="https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult.png" alt="Run Cypress tests from NUnit - failed test in JetBrains Rider, all details from cypress visible" class="wp-image-640" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult.png 999w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult-300x97.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/09/4_CypressTestFailedNUnitResult-768x248.png 768w" sizes="auto, (max-width: 999px) 100vw, 999px" /></figure></div>



<p>You will see similar errors if something goes wrong in a Cypress test. Cypress also <a href="https://docs.cypress.io/guides/guides/screenshots-and-videos">saves compressed videos and screenshots</a>, so if something goes wrong, diagnosing such failed tests is very easy.</p>



<h2 class="wp-block-heading">Summary</h2>



<p>I hope you found this article on how to run Cypress tests from NUnit useful. Remember that it not only works with NUnit &#8211; it will be suitable for any testing framework. You might need to adjust some parts of the solution, e.g. printing the results to the test result&#8217;s output.</p>



<p>This solution allowed us to improve our testing by adding a new type of tests to our <a href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">pipeline</a>. Right next to <a href="https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/">proper naming</a> and <a href="https://blog.yumasoft.pl/2021/08/make-your-tests-bulletproof-with-mutation-testing/">mutation testing</a>, E2E tests add another layer of security and improve the quality of our software.</p>



<p>Once more, you can find the complete source code <a href="https://github.com/dsibinski/cypress-csharp-example">here</a>. If you want, feel free to copy it and change it according to your needs ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/">Run Cypress Tests From NUnit Test (C#)</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/09/run-cypress-tests-from-nunit-test-csharp/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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>Why Naming Tests Matters?</title>
		<link>https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/</link>
					<comments>https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 26 Jul 2021 06:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[integration tests]]></category>
		<category><![CDATA[software testing]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[unit tests]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=418</guid>

					<description><![CDATA[<p>Do you even care about naming tests? Or maybe you use some random, not-much-telling names for your test methods? At Yumasoft, we treat testing as an inseparable part of software development. It turns out that the tests you write can tell a lot about your software. However, when named carelessly, these tests have much less&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/">Why Naming Tests Matters?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Do you even care about naming tests? Or maybe you use some random, not-much-telling names for your test methods? At Yumasoft, we treat testing as an inseparable part of software development. It turns out that the tests you write can tell a lot about your software. However, when named carelessly, these tests have much less value. Let&#8217;s talk about naming tests properly, so both programmers and business can get more value of them.</p>



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



<h2 class="wp-block-heading">Consequences of bad tests&#8217; names</h2>



<p>You may think I&#8217;m exaggerating here. Why would I care about naming my tests methods? These are only tests&#8230; Let&#8217;s not waste time on thinking about such unimportant things. However, before we dig more into that, let&#8217;s see how typical badly-named tests look like. Below, I&#8217;m presenting the types of badly named tests that I see most often. I also used to name my tests like that ?</p>



<h3 class="wp-block-heading">Random names</h3>



<p>This is what I like the most:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="346" height="134" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/1_RandomTestsNames.png" alt="Rider and unit tests result: Test1, Something1, Test11..." class="wp-image-426" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/1_RandomTestsNames.png 346w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/1_RandomTestsNames-300x116.png 300w" sizes="auto, (max-width: 346px) 100vw, 346px" /></figure></div>



<p>Just random names. Surprisingly, I see that quite often. What does such a list of passing tests tell us? Why do they pass? What do these tests verify? We know nothing about them. We can only see that <em>there are some test</em>s. What they test, how and under what conditions &#8211; can&#8217;t say ?&#x200d;<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2642.png" alt="♂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<h3 class="wp-block-heading">Tested object described</h3>



<p>This is another type of tests I sometimes encounter. A bit better, yet still far from fully informative:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="346" height="152" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/2_ObjectsUnderTestsNamed.png" alt="Naming tests with objects under tests named, for example GetUser_Test1, Login_Test1. Class named UsersServiceTests" class="wp-image-428" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/2_ObjectsUnderTestsNamed.png 346w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/2_ObjectsUnderTestsNamed-300x132.png 300w" sizes="auto, (max-width: 346px) 100vw, 346px" /></figure></div>



<p>Looking at these tests list, we can at least say a few things:</p>



<ul class="wp-block-list"><li>there exists a <code>UsersService</code> class which is under tests here</li><li><code>UsersService</code> has at least two methods: <code>GetUser</code> and <code>Login</code></li><li>under some <em>unknown conditions</em>, <code>GetUser</code> and <code>Login</code> methods <em>probably</em> work</li></ul>



<p>However, we still don&#8217;t know what it <em>actually means</em> that these tests pass. Does it mean that login and getting users are well-tested and work correctly? What conditions need to be met so the user can log in? What parameters do we have to provide to <code>GetUser</code> method to use it properly? Again &#8211; we can&#8217;t say.</p>



<h3 class="wp-block-heading">Expected outcome described</h3>



<p>Let&#8217;s go one step further. A bit improved tests might look as follows:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="384" height="157" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/3_ObjectsAndOutcomesNamed-1.png" alt="Naming tests with objects under test and expected outcomes named, for example: GetUser_ReturnsUserData " class="wp-image-432" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/3_ObjectsAndOutcomesNamed-1.png 384w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/3_ObjectsAndOutcomesNamed-1-300x123.png 300w" sizes="auto, (max-width: 384px) 100vw, 384px" /></figure></div>



<p>Someone tried to be a bit more descriptive here. That&#8217;s good, isn&#8217;t it? ? Let&#8217;s try to see how many things we can say about our codebase now:</p>



<ul class="wp-block-list"><li>there exists a <code>UsersService</code> class which is under tests here</li><li><code>UsersService</code> has at least two methods: <code>GetUser</code> and <code>Login</code></li><li><code>GetUser</code> method returns user data</li><li><code>GetUser</code> might <em>sometimes</em> throw <code>UserNotFoundException</code></li><li><code>Login</code> logs in the user correctly under <em>some</em> conditions</li><li><code>Login</code> returns a user token</li><li><code>Login</code> might <em>sometimes</em> throw <code>WrongCredentialsException</code></li></ul>



<p>We already know much more about our codebase! However, notice that we still often use very uncertain words like &#8220;sometimes&#8221; or &#8220;some&#8221;. It doesn&#8217;t feel we&#8217;re sure about these outcomes. We still don&#8217;t know <em>under what conditions</em> these tests pass or exceptions are thrown.</p>



<h2 class="wp-block-heading">Why badly named tests are bad?</h2>



<p>I think you already know that at this point. Badly named tests are confusing. They don&#8217;t tell us enough about our codebase. One of the goals of tests is to be a <em>live code documentation.</em> But how can they be if they are very undescriptive? How can we know what happens in case such a test fails: </p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="617" height="152" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/4_UndescriptiveTestFails.png" alt="Naming tests and failure of nondescriptive test" class="wp-image-434" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/4_UndescriptiveTestFails.png 617w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/4_UndescriptiveTestFails-300x74.png 300w" sizes="auto, (max-width: 617px) 100vw, 617px" /></figure></div>



<p>What do we know looking at that? Why could this test fail? If there&#8217;s a non-technical person looking as this result in a CI system while programmers are not around &#8211; how can (s)he tell what&#8217;s wrong and how to potentially fix that?</p>



<p>This result tells us nothing more than the <code>Login</code> process does not throw <code>WrongCredentialsException</code>. But we don&#8217;t know <em>when </em>it should throw this error. What are the <em>conditions</em> that should be met for this exception to be thrown? Again &#8211; we can&#8217;t say.</p>



<p>Notice that I used the best from all examples we had so far. What if this test was named <code>Test1()</code> and it failed? Does it mean anything? Is it something serious blocking the release of our app, or we can ignore it? ?</p>



<p>I guess you now see very well what I mean by badly named tests. I hope you also understand &#8211; and agree ? &#8211; that  such tests&#8217; names are bad.</p>



<p>Fortunately, we all here write tests, right? ? So, if we are already putting some effort in creating tests, let&#8217;s put a bit more to make them informative and useful for others.</p>



<h2 class="wp-block-heading">Naming tests done well</h2>



<p>There are a lot of unit tests naming conventions. You can read about them in many great articles like <a href="https://medium.com/@stefanovskyi/unit-test-naming-conventions-dd9208eadbea">this one</a>. There&#8217;s a lot of <a href="https://stackoverflow.com/questions/155436/unit-test-naming-best-practices">discussion</a> around naming tests conventions.</p>



<p>However, independently of which naming strategy you choose, I think a well named test should tell someone who reads its name the following things:</p>



<ul class="wp-block-list"><li><strong>what</strong> is tested (class name)</li><li><strong>which part</strong> of it is tested (method name)</li><li>what is the <strong>expected result</strong></li><li>what are the <strong>conditions</strong> for the expected result</li></ul>



<p>Many of tests naming conventions fulfil these 4 requirements.</p>



<p>Let&#8217;s come back to our last example of badly named tests (but still its best version): </p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="384" height="157" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/3_ObjectsAndOutcomesNamed-1.png" alt="Tests named with objects under test and expected outcomes named, for example: GetUser_ReturnsUserData " class="wp-image-432" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/3_ObjectsAndOutcomesNamed-1.png 384w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/3_ObjectsAndOutcomesNamed-1-300x123.png 300w" sizes="auto, (max-width: 384px) 100vw, 384px" /></figure></div>



<p>Let&#8217;s use one of the naming conventions I like:</p>



<p><em>MethodName</em>_<strong>Should</strong>_<em>ExpectedResult</em>_<strong>When</strong>_<em>Conditions</em></p>



<p>Applying this to our tests, the final result is as follows:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="677" height="158" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/5_WellNamedTests-1.png" alt="Well named tests, for example GetUser_Should_ReturnUserData_When_ExistingUserIdProvided" class="wp-image-437" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/5_WellNamedTests-1.png 677w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/5_WellNamedTests-1-300x70.png 300w" sizes="auto, (max-width: 677px) 100vw, 677px" /></figure></div>



<p>Now, for each test, we can see what is tested (<code>UsersService</code>), which part of it (e.g. <code>Login</code> method), what&#8217;s the expected result (e.g. <code>ReturnUserToken</code>) and under what conditions (e.g. <code>CorrectUsernameAndPasswordProvided</code>). This is very descriptive and valuable. Now, when something goes wrong:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="891" height="147" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/6_WellNamedTestsOneFailing.png" alt="One test failing from all properly named. Thanks to its descriptiveness, it's easy to see what's wrong" class="wp-image-438" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/6_WellNamedTestsOneFailing.png 891w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/6_WellNamedTestsOneFailing-300x49.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/6_WellNamedTestsOneFailing-768x127.png 768w" sizes="auto, (max-width: 891px) 100vw, 891px" /></figure></div>



<p>We know a lot more than before. We can easily see that when wrong credentials are provided, the <code>WrongCredentialsException</code> is not thrown (while it should). It makes it easier to judge whether this is blocking for the release of our app or not. We probably don&#8217;t want to let users login with incorrect credentials ?</p>



<p>Another, very important factor for naming your tests well is that <strong>the whole team agrees on one naming convention</strong>. I think it&#8217;s even more important than choosing a particular naming convention. Choose whatever suits you and your team. There are <a href="https://medium.com/@stefanovskyi/unit-test-naming-conventions-dd9208eadbea">a lot of options</a>, but in the end it doesn&#8217;t really matter which one you agree on. Just stick to it and make sure it delivers all value you need.</p>



<h2 class="wp-block-heading">How naming tests well can help the programmers </h2>



<p>Correctly and descriptively named tests can help you as a programmer. Imagine that you are about to introduce a change in <code>UsersService</code>, but you have no idea how it works. The first thing I do in such cases is to open a corresponding tests file and scan all unit tests.</p>



<p>First, I expect that the tests file is named <code>UsersServiceTests</code>, because my team follows a common naming convention. Thanks to that, I can immediately open this class in my IDE and quickly scan all unit tests names:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="884" height="211" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/7_QuickTestsScan.png" alt="Quickly scanning well-named tests in a test class as in IDE" class="wp-image-442" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/7_QuickTestsScan.png 884w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/7_QuickTestsScan-300x72.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/7_QuickTestsScan-768x183.png 768w" sizes="auto, (max-width: 884px) 100vw, 884px" /></figure></div>



<p>This tells me a lot about the <code>UsersService</code>. If I want to see only how <code>Login</code> method works, I can easily filter out all tests with names starting with &#8220;Login&#8221;. Again &#8211; thanks to following the naming convention.</p>



<p>The next thing is that when you start making changes to the production code of <code>UsersService</code> and one of the tests fails, you know what&#8217;s wrong very quickly.</p>



<p>Another advantage of naming tests properly is when you are adding new tests. If you&#8217;re obliged to follow a descriptive tests naming convention, you need to think very well what your new code under tests should do and under what conditions. This makes your production code more structured and well-thought. Just give it a try, especially if you use TDD. I often start my TDD session with writing empty tests named correctly. This gives me many ideas of the production code I&#8217;m going to write straight after.</p>



<h2 class="wp-block-heading">How naming tests well can help the business</h2>



<p>I think I don&#8217;t need to explain that to you anymore. However, remember that your tests might be useful for other people than programmers. All non-technical people involved in the project can have a great use of the tests if you name them correctly.</p>



<p>I hope that your <a href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">tests are executed as part of a CI process</a> you use for your app deployment. As I wrote before, imagine having a late Friday release of your app. As a programmer, you&#8217;re the last one to commit this one last bug fix, and you go home. The project manager stays alone, waiting for the CI build to finish, so he can accept and deploy the new version of the app. But then, one of the tests fails. If your tests are named well, he will be able to easily judge whether this is blocking the deployment or not. I think that gives a lot of value to the business.</p>



<p>In case of some applications, where classes/services are created close to the domain of the app, business people can also explore the set of tests for a given service to grasp an idea of how it works and why it works as it does. Similarly, when you explore the tests class in your IDE. This wouldn&#8217;t be possible in every project, but it might also become a benefit of well-named tests in some cases.</p>



<h2 class="wp-block-heading">Summary</h2>



<p>I hope that after reading this article, you know why naming tests matters. Giving your test a descriptive name is a small effort, but it brings huge benefits. Both to your fellow programmers, but also to non-technical, so-called &#8220;business people&#8221;.</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/why-naming-tests-matters/">Why Naming Tests Matters?</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/why-naming-tests-matters/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>
		<item>
		<title>How To Configure Jest Tests on TeamCity</title>
		<link>https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/</link>
					<comments>https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 12 Jul 2021 10:30:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jest]]></category>
		<category><![CDATA[jest teamcity]]></category>
		<category><![CDATA[jest teamcity reporter]]></category>
		<category><![CDATA[teamcity]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tests]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=381</guid>

					<description><![CDATA[<p>If you are using jest to write JavaScript/TypeScript tests like this one: and you&#8217;re looking for how to show their results in TeamCity as follows: Then this article will help you configure that ? Prerequisites To run jest tests on TeamCity, you need to use jest as your testing framework of course ? If you&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">How To Configure Jest Tests on TeamCity</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you are using <code>jest</code> to write JavaScript/TypeScript tests like this one:</p>



<script src="https://gist.github.com/dsibinski/5266776ab31d225e6dc97278d4f5f02f.js"></script>



<p>and you&#8217;re looking for how to show their results in TeamCity as follows:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="696" height="314" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-1.png" alt="Jest tests on TeamCity - sample output" class="wp-image-387" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-1.png 696w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-1-300x135.png 300w" sizes="auto, (max-width: 696px) 100vw, 696px" /></figure></div>



<p>Then this article will help you configure that ?</p>



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



<h2 class="wp-block-heading">Prerequisites</h2>



<p>To run <code>jest</code> tests on TeamCity, you need to use <a href="https://www.npmjs.com/package/jest">jest</a> as your testing framework of course ? If you use TypeScript &#8211; which I <a href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">definitely recommend</a> &#8211; <a href="https://www.npmjs.com/package/ts-jest">ts-jest</a> package will also be useful.</p>



<p>Having these packages installed, your basic <code>jest.config.js</code> file should look similar to this:</p>



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



<p>You should also have a <code>test</code> script defined in <code>scripts</code> section of your <code>package.json</code> file:</p>



<script src="https://gist.github.com/dsibinski/751a5df5169684cc44eb3d4497e3bb2a.js"></script>



<p>With this setup, you are able to execute your tests with <code>npm test</code> command and see the results in the console:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="781" height="272" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console.png" alt="Jest tests output in VS Code" class="wp-image-391" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console.png 781w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console-300x104.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/jest_tests_results_console-768x267.png 768w" sizes="auto, (max-width: 781px) 100vw, 781px" /></figure></div>



<p>Such tests results are however not recognized in any way by <a href="https://www.jetbrains.com/teamcity/">TeamCity</a> build server. Let&#8217;s see how to configure that.</p>



<h2 class="wp-block-heading">Installing TeamCity reporter</h2>



<p>In order to see the tests results in TeamCity, we need the <code>npm test</code> command to produce a different output when run on TC server. This output must be compatible with <a href="https://www.jetbrains.com/help/teamcity/service-messages.html#Reporting+Tests">TeamCity&#8217;s service messages</a>.</p>



<p>Fortunately, you don&#8217;t need to implement it by yourself ?</p>



<p>First, install <a href="https://www.npmjs.com/package/jest-teamcity">jest-teamcity</a> npm package to your project. Next, add <code>jest-teamcity</code> to <code>reporters</code> configuration option in your <code>jest.config.js</code> file:</p>



<script src="https://gist.github.com/dsibinski/7e9a94a8374d5901d452844e8a4fc9d5.js"></script>



<h2 class="wp-block-heading">Adding TeamCity build step</h2>



<p>Next, login to your TeamCity server. You need to know the location of <code>npm.cmd</code> file on your TeamCity server. It should be in NodeJS installation catalog. In our case, the path to the file is <code>C:\Dev\nodejs\npm.cmd</code>. </p>



<p>Now, go to TeamCity web administration interface and create a new build configuration or edit a current one. Then, add a new build step:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="618" height="212" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_add_new_step.png" alt="TeamCity - adding a new build step button" class="wp-image-393" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_add_new_step.png 618w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_add_new_step-300x103.png 300w" sizes="auto, (max-width: 618px) 100vw, 618px" /></figure></div>



<p>Configure the new step as follows:</p>



<ul class="wp-block-list"><li><strong>Runner type</strong>: <em>Command line</em></li><li><strong>Step name</strong>: <em>Jest JS tests</em></li><li><strong>Execute step</strong>: <em>If all previous steps finished successfully</em> (or anything else you prefer)</li><li><strong>Working directory</strong>: name of the catalog in your web app where you normally run <code>npm test</code></li><li><strong>Run</strong>: <em>Custom script</em></li><li><strong>Custom script</strong>: <em>&#8220;your_path_to_nodejs\npm.cmd&#8221; run test</em>, for example: <em>&#8220;C:\Dev\nodejs\npm.cmd&#8221; run test</em></li><li><strong>Format stderr output as</strong>: <em>error</em></li></ul>



<p>If everything is configured correctly, during the next build you should see <code>jest</code> tests output recognized and nicely reported by TeamCity:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="696" height="314" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-2.png" alt="Jest tests on TeamCity - jest tests results in a build" class="wp-image-394" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-2.png 696w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_jest_tests_result-2-300x135.png 300w" sizes="auto, (max-width: 696px) 100vw, 696px" /></figure></div>



<p>If that&#8217;s not the case, make sure that <code>TEAMCITY_VERSION</code> environment variable is set on your TeamCity machine. That&#8217;s how <code>jest-teamcity</code> reporter recognizes when to output standard <code>jest</code> output and when to use TeamCity&#8217;s service messages format.</p>



<h2 class="wp-block-heading">Debugging jest tests on TeamCity locally</h2>



<p>If you run <code>npm test</code> locally, you will see that the output is still normal, not the TeamCity format. That&#8217;s correct, because as we said before, the <code>TEAMCITY_VERSION</code> environment variable must exist in order to see TeamCity-formatted output. Normally we don&#8217;t want to see TeamCity-formatted tests output locally, of course ?</p>



<p>It means that if you want to debug this configuration locally and actually see the TC tests output on your dev machine, you need to define such environment variable locally.</p>



<p>In order to do that in Windows, go to <code>Edit the system environment variables</code>, click on <code>Evironment Variables...</code> and add a new one in <code>System variables</code> section. Call this variable <code>TEAMCITY_VERSION</code> and give it some value, for example <code>2021.1</code>:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="653" height="168" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_version_evironment_variable.png" alt="Adding new system environment variable called &quot;TEAMCITY_VERSION&quot; with value of &quot;2021.1&quot;, so the jest reporter can output TeamCity-formatted result" class="wp-image-396" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_version_evironment_variable.png 653w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/TeamCity_version_evironment_variable-300x77.png 300w" sizes="auto, (max-width: 653px) 100vw, 653px" /></figure></div>



<p>If you&#8217;re on another operating system, add the environment variable according to your system&#8217;s requirements.</p>



<p>Next, restart Visual Studio Code or the console tool you are using and execute <code>npm test</code> again. Now you should see the tests results output in TeamCity format:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="214" src="https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-1024x214.png" alt="TeamCity-formatted jest tests results in VS Code console" class="wp-image-397" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-1024x214.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-300x63.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format-768x161.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/07/npm_test_jest_teamcity_format.png 1127w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<h2 class="wp-block-heading">Jest tests in TeamCity &#8211; summary</h2>



<p>I hope you found this piece of advice useful. At Yumasoft, we value tests a lot, both backend and frontend ones. However, the tests which are not run automatically are worth nothing. That&#8217;s why we always have all of our tests run automatically as part of our CI process (TeamCity in the sample project&#8217;s case).</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/07/how-to-configure-jest-tests-on-teamcity/">How To Configure Jest Tests on TeamCity</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-configure-jest-tests-on-teamcity/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
