Getting the Most Out of Test Coverage

Getting the Most Out of Test Coverage

Having a solid test suite is instrumental to having confidence in your system, but how do you know if your test suite is good?

All metrics for evaluating the quality of a test suite have to be applied carefully; if taken to an extreme blindly, they all can cause harm. Common metrics include:

  • count of lines of test
  • maintainability of the test suite
  • speed of the test suite
  • how much of the code is run by the test suite

This last metric, called coverage, is what we will look at today.

What is Test Coverage

Test coverage counts how much of the application code is used by the test suite. Consider the following example code:

def application_code(animal)
	if animal == "cat"
		puts "felines rock!"
	elsif animal == "dog"
		puts "canines are better"
	end
end

def test
	raise "Test failed" unless application_code("cat").include?("feline")
end        

In this example, the test only tests the cat side of things, the dog is left out. In terms of test coverage, this means that of the 7 lines of application code, 6 are being run by the test, giving 86% test coverage.

Why is Test Coverage Good

Test coverage gives you an easy, understandable metric for your test suite. I can easily say that a test suite with 1% coverage is almost certainly not well tested, whereas at 99% coverage, it almost certainly is. In addition to how much coverage you have, where you have coverage makes a big difference. If you are a bank, having 100% coverage on the finances section is much more valuable than 100% coverage on a “where’s the closes branch” feature.

When you have high coverage and a quality test suite, you can be more confident that the system works as specified. This increases the speed at which new features can developed and deployed.

Why is Test Coverage Bad

Test coverage can easily be misleading:

def divide(a, b)
	a / b
end

def test
	raise "Test failure" unless divide(10, 5) == 2
end        

In this test, we are getting 100% code coverage. Congratulations!

Just don’t pass 0 in for b. Or do 11 / 5 cause that is 2 instead of 2.2 And good luck if you pass in something that isn’t a number. Currently, the code will happily try to compute 11 / ??.

Poorly written tests can increase the coverage metric without testing the code well. This leads to a false sense of confidence.

How to Use Test Coverage Well

I typically recommend testing the critical sections of your code first. This is the checkout path for an e-commerce site, posting on social media, or searching on a knowledge-sharing service.

Having high coverage of this critical code means you can deploy with confidence, and make updates without worrying about spooky actions infecting your code.

要查看或添加评论,请登录

Jonathan Bennett的更多文章

  • Form Objects: A Rails-Friendly Approach

    Form Objects: A Rails-Friendly Approach

    accepts_nested_attributes is a great tool when working with ActiveRecord objects. But what if you're managing complex…

  • Using Accessories to Extend Your Kamal Projects

    Using Accessories to Extend Your Kamal Projects

    We previously explored how to integrate a Docker image into a Kamal project. Did you know you can also use it as an…

  • Mastering Nested Attributes

    Mastering Nested Attributes

    Here are a few quick tips to help you sidestep some common issues with accepts_nested_attributes: 1. Don’t Forget the…

  • One Form, Multiple Objects with Rails Magic

    One Form, Multiple Objects with Rails Magic

    If you've ever needed to handle complex object relationships with a single form, accepts_nested_attributes might be…

  • Kamal Isn’t Just for Rails…

    Kamal Isn’t Just for Rails…

    Kamal plays wonderfully with Rails, but did you know it’s a general-purpose container tool? I recently set up Metabase…

  • Bad App Ideas: Apple Watch Edition

    Bad App Ideas: Apple Watch Edition

    Sometimes there are apps that just shouldn't be made. For today, I'm going to focus on the Apple Watch.

  • More Spaghetti, Less Spaghetti Code

    More Spaghetti, Less Spaghetti Code

    There are few meals I enjoy more than pasta. I would eat it basically in basically any form 8 days a week if given the…

  • Who is Your Ideal Customer?

    Who is Your Ideal Customer?

    Too many founders come up with a great idea and start moving on it right away. This is almost always a mistake! When…

  • You are the Mechanical Turk

    You are the Mechanical Turk

    Sometimes building out any type of software to test your idea might not be very effective. In these cases, it might be…

  • Using Prototypes for Customer Validation

    Using Prototypes for Customer Validation

    Creating custom software is time and cost-prohibitive. In addition, we know the least at the start of the project! This…

社区洞察

其他会员也浏览了