<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Michael Nunez</title>
    <description>The latest articles on Forem by Michael Nunez (@_michaelnunez).</description>
    <link>https://forem.com/_michaelnunez</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F31117%2Fab4eb06f-e6cb-4fb2-b117-7a4625ae852f.jpg</url>
      <title>Forem: Michael Nunez</title>
      <link>https://forem.com/_michaelnunez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/_michaelnunez"/>
    <language>en</language>
    <item>
      <title>Test Coverage - going from bad to good, and knowing when to do it</title>
      <dc:creator>Michael Nunez</dc:creator>
      <pubDate>Mon, 28 Aug 2017 21:34:08 +0000</pubDate>
      <link>https://forem.com/_michaelnunez/test-coverage---going-from-bad-to-good-and-knowing-when-to-do-it</link>
      <guid>https://forem.com/_michaelnunez/test-coverage---going-from-bad-to-good-and-knowing-when-to-do-it</guid>
      <description>&lt;p&gt;Having production code with good test coverage is very beneficial to a development team.&lt;br&gt;
Broad coverage can catch bugs and unintended behavior from entering the codebase, and everyone can sleep better at night, but when a code base doesn't have much coverage (or any at all), it can be really difficult to introduce it.&lt;br&gt;
We'll discuss when you should introduce or increase test coverage, and go in depth into the ways in which it's beneficial for you and your team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coverage for legacy codebases?
&lt;/h2&gt;

&lt;p&gt;As a huge believer in tests, I make a concerted effort to add coverage to every project I work on.&lt;br&gt;
Realistically though, there are times where tests are just not that beneficial.&lt;br&gt;
For instance, when dealing with a legacy codebase (an internal API that is rarely updated), adding coverage offers diminished returns.&lt;br&gt;
If your legacy codebase works reliably in production with little to no regression, adding tests retroactively may simply work to verify what is already validated in production.&lt;br&gt;
Often, the legacy parts of an application serve as dependencies, so I suggest focusing your efforts toward testing the new API leveraging the old code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Still unsure of the benefits to coverage?
&lt;/h2&gt;

&lt;p&gt;Here are some benefits to increased test coverage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure your code works as expected&lt;/li&gt;
&lt;li&gt;Deploy code with greater confidence&lt;/li&gt;
&lt;li&gt;Decrease the likelihood of 1am phone calls&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Ensure your code works as expected&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Adding tests encourages you to conceptualize how you want your program to behave and account for edge cases.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Deploy code with greater confidence&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The greater your coverage, the more confident you can be that new features did not introduce regressions.&lt;br&gt;
In the event that deployment introduces a bug, tests can serve as documentation to both track down the bug and squash it.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Decrease the likelihood of 1am phone calls&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Pager duty is not fun. Seriously.&lt;br&gt;
Nor is it fun for the person calling you.&lt;br&gt;
Increased coverage will help prevent bugs that have the habit of waking you up in the middle of the night.&lt;br&gt;
Future you would appreciate that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Okay, I'm sold! How do I start adding coverage?
&lt;/h2&gt;

&lt;p&gt;Once your team is on board with increasing test coverage, I suggest first writing test for new feature work.&lt;br&gt;
That way, new changes to the codebase are covered, and good habits will start to form amongst team members (and in turn, new hires).&lt;/p&gt;

&lt;p&gt;Your next priority should be adding coverage for existing code (non-legacy), but this must be done much more carefully.&lt;br&gt;
There are generally two ways to go about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scrap the existing implementation, one discrete module at a time, and test drive the recreation of its code&lt;/li&gt;
&lt;li&gt;Add tests to validate the existing code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Scrap &amp;amp; test drive&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scrapping and test driving will likely result in better design and code clarity.&lt;/li&gt;
&lt;li&gt;The tests will serve as a form of documentation for your teammates and future self.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Throwing out existing code can result in you missing a particular business case that wasn't clear within the implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Add coverage to pre-existing code&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generally a quicker process than scrapping and test driving&lt;/li&gt;
&lt;li&gt;Coverage of an existing API can make refactoring easier&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;May work to simply validate poor design.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Side effects
&lt;/h2&gt;

&lt;p&gt;Testing certainly has a learning curve, so your team may spend up-front time figuring out how to best test new and old code.&lt;br&gt;
This can result in a decrease in sprint velocity, and may require more complexity points in story estimation.&lt;br&gt;
That said, adding tests to new features will reduce bugs, and in turn, decrease the cost of having to squash them later,&lt;br&gt;
and adding coverage to existing code will pay down technical debt that can prove crippling if left unpaid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;To wrap up, increasing test coverage will improve design, increase team confidence and cohesion, and decrease the stress associated with regression.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Senior, Lead, or Principal developer in NYC? &lt;a href="http://www.stridenyc.com/careers"&gt;Stride is hiring&lt;/a&gt;! Want to level up your tech team? &lt;a href="http://www.stridenyc.com/our-work"&gt;See how we do it&lt;/a&gt;! &lt;a href="http://www.stridenyc.com"&gt;www.stridenyc.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>testcoverage</category>
      <category>test</category>
      <category>coverage</category>
      <category>agile</category>
    </item>
  </channel>
</rss>
