---
title: "Discussion on \"Keeping Tests Valuable: Using Branching Coverage to Improve Software Quality\" | Hashnode"
description: "Discussion on \"Keeping Tests Valuable: Using Branching Coverage to Improve Software Quality\". Branch coverage provides valuable insight into areas of your code that require the most attention and testing."
url: "https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Keeping Tests Valuable: Using Branching Coverage to Improve Software Quality](https://thepragmaticengineer.hashnode.dev/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality?utm_source=hashnode&utm_medium=feed)

Branch coverage provides valuable insight into areas of your code that require the most attention and testing.

Mar 13, 2023 · 12 min read · thepragmaticengineer.hashnode.dev  
Updated Apr 3, 2023  
Read the article

-   [#testing](https://hashnode.com/tag/testing)
-   [#software-testing](https://hashnode.com/tag/software-testing)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#programming-blogs](https://hashnode.com/tag/programming-blogs)
-   [#programming](https://hashnode.com/tag/programming)

## Written by

[Rafael dos Santos Miguel Filho](https://hashnode.com/@ThePragmaticEngineer)

@ThePragmaticEngineer · Brazil

  
[View profile](https://hashnode.com/@ThePragmaticEngineer)

Software Engineer

Hi, I'm Rafael, but you can call me Rafa! 😄 I'm a software engineer passionate about everything that involves technology, with more than 5 years of experience in the market. Throughout my career, I worked in banking institutions, developing innovative solutions and contributing to the digital transformation of the financial sector. With a strong interest in software architecture and testing, I am constantly looking to specialize and improve my skills in this area. Here, you will find tips, tutorials and discussions on the most varied topics related to architecture, good practices and software testing, always with the aim of sharing knowledge and learning from other developers. I value the exchange of experiences and I am always willing to listen and learn from my professional colleagues. Feel free to get in contact, leave your comments and share your own experiences and knowledge. Together, we can build a stronger, more collaborative community in the software development world.

## Responses 4

Markdown supported · ⌘↵ to post

[Rahul Shinde](https://hashnode.com/@Rahul0Dev)

[Mar 25, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/641f0ea505c482464c90ca47)

Thanks for sharing [Rafael dos Santos Miguel Filho](https://hashnode.com/@ThePragmaticEngineer)

[Luca Marcy](https://hashnode.com/@Alynar1234)

[Mar 15, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/641191d4de6bc77a79838770)

Wow

[Sandeep Panda](https://hashnode.com/@sandeep)

[Mar 14, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/6410fb39d0ccf16ed818318f)

Great piece of content! Featured today. Keep it up. :)

[Rafael dos Santos Miguel Filho](https://hashnode.com/@ThePragmaticEngineer)

[Mar 14, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/6410fb39d0ccf16ed818318f#reply-6410ff3b23ae346d76040389)

Thank you very much, I always hope to continue to bring more content to the platform!

[Anthony Fung](https://hashnode.com/@anthonyf)

[Mar 14, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/6410ee16d0ccf16ed8183076)

Good points made about the number of branches and complexity. I think it's important to consider that some branches could be guard statements at the beginning of methods, and it's important to keep those in place.

It looks like xUnit is being used in the PhoneTest example. I know it's possible to parameterise tests in NUnit for different inputs to quickly test various scenarios (e.g. input length); I think this is possible with xUnit too, so we don't have to write a test for each input.

I completely agree that we should try to simplify code where possible though, and that breaking it into smaller modules can help. I usually break long methods into separate service classes where possible and inject them using dependency injection. A bonus of this is that the logic can be tested independently, and test setup does not have to include code that would otherwise be used to navigate to the code that needs testing.

[Anthony Fung](https://hashnode.com/@anthonyf)

[Mar 14, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/6410ee16d0ccf16ed8183076#reply-64110734d0ccf16ed81832af)

[Rafael dos Santos Miguel Filho](https://hashnode.com/@ThePragmaticEngineer) In addition to keeping logical units small, I find that writing in a functional-programming style also helps with testability too.

I know it isn't always possible in real-world programming but I find it often easier to make test assertions on the result of a method call, than to verify various mocks from the test setup, etc.

[Rafael dos Santos Miguel Filho](https://hashnode.com/@ThePragmaticEngineer)

[Mar 15, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/6410ee16d0ccf16ed8183076#reply-6411290ed0ccf16ed8183520)

[Anthony Fung](https://hashnode.com/@anthonyf) Nice, I think it's important to have this view you commented, in the last year I have adopted a more simplistic testing strategy, look at the requirement, understand the expected behaviors and just check what really matters in the assert. I also look for invalid scenarios, unusual inputs that I wouldn't normally think of if I just did the happy path of unit testing.

Obviously this is not always easy to apply, especially in complex systems. I am writing about this as well. What strategy do you usually put into practice for your test cases?

[Anthony Fung](https://hashnode.com/@anthonyf)

[Mar 15, 2023](https://hashnode.com/posts/keeping-tests-valuable-using-branching-coverage-to-improve-software-quality/640e830987c97760def75647/comment/6410ee16d0ccf16ed8183076#reply-641231c0ab81b092709ac4c1)

[Rafael dos Santos Miguel Filho](https://hashnode.com/@ThePragmaticEngineer) That’s a good question. I think it’s important that tests are useful but not brittle. I know that some people strive for 100% coverage of every scenario. Not only does this consume a lot of time in writing (e.g. testing every getter/setter), it also makes requirements changes very difficult to implement. There’s always a balance that can be difficult to achieve.

For my test cases, I’ll usually start off with the happy case. I’ll also study the code, looking for subtle cases where things can go wrong, and try to test those ‘bad’ cases. This can be things like expecting exceptions to be thrown, or that the appropriate response is given if a process fails. To avoid test code duplication, I’ll often use TestCases (in NUnit) with various inputs. If I’m expecting some failures, I’ll also parameterise the test for expected output; I can then assert that the output is as expected.

Functional method calls are best for this as the method will have a return value to assert against. If it can be made idempotent, even better; I can run (and re-run) the method multiple times in the debugger if necessary.

If the test is for making sure that multiple services get called with the expected inputs, one way I might do this is mock the service. I’ll do this in a way that the mock takes the arguments for a method call and stores them in a local test variable. In this way, there’s also something to assert against. If using Moq (and probably other testing libraries), it’s also possible to check its invocations. This can sometimes be trickier though as it might be called by things that aren’t being tested. Specifying e.g. that the fourth invocation should be a specific value can lead to a brittle test as it’s relying on a specific invocation.

If the method I’m testing is functional and relies of the output of other services, I won’t always verify that the service mocks have been called – it’s implicit that they haven’t been called if the final output is wrong.
