<?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>integration tests Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/integration-tests/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/integration-tests/</link>
	<description>Software development blog</description>
	<lastBuildDate>Sun, 01 Aug 2021 05:24:26 +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>integration tests Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/integration-tests/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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 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="(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 fetchpriority="high" 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="(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 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="(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>
	</channel>
</rss>
