Measuring Code Quality with Test Coverage Metrics

Code quality and test coverage metrics are two important metrics which comes in play during software testing. These two aspects of testing are interrelated but there is no direct formula to evaluate in terms of each other.  Let’s understand what is test coverage and how it is evaluated in the general terms.

Test Coverage

The testing team speaks of percentage of test coverage for a particular project but are they evaluating this percentage score of test coverage in the correct way? The test team designs the test cases based on the functional requirement document but those test cases are actually covering the line of codes i.e. in other words when the testing team execute these test cases, the system ensure that all lines of codes for the given program has executed successfully or covered during the testing. The answer could be that it cannot be ensured but functionality wise test coverage is solid. Therefore, the test coverage should be evaluated in terms of lines of code with the help of following formula.

  • Supposes a project under test has 10,000 lines of code. Say X = 10,000.
  • The test cases which are executed just cover 7500 lines of code. Say Y = 7,500.

The actual test coverage will be (7,500/10,000) * 100 = 75%. Generally, if the test coverage exceeds 80% then the test team concludes that they have enough test coverage to ensure the code quality. This test coverage will be directly proportional to the code quality but still there are many other aspects which the testing team should take into consideration.

Test Coverage Metrics

 The testing team should take into consideration about the number of good tests a well as number of negative tests for any project on top of the test coverage. This is where it gets quite interesting when we speak about code quality in terms of test coverage. Therefore, if the testing team says they have 100% test coverage by touching all lines of codes, it does not ensure the code quality but the code quality will depend on the following two factors.

  1. Adequate test coverage i.e. 80% or more than that. 100% test coverage can be an ideal case.
  2. Good test cases for the functional testing.

How to achieve Good test cases for a project?

Well, it’s not a big deal and for an experienced tester this should be very simple. The tester while writing the test cases should take into consideration about the good test cases, negative test cases, happy and unhappy test scenarios for the application flow. This could be ensured with the help of traceability matrix for the requirements vs test coverage. The traceability matrix will always ensure that you have appropriate test scenario covered for a particular requirement and there are underlying test cases to cover the functional testing in terms of both positive and negative aspects of the testing. If 100% functional aspects are covered for testing in the traceability matric then it will be good to say that you posses good test cases in your test repository to cover the functional testing.

We can have the best coverage with the use of traceability matrix for requirements but in the actual sense are the requirements complete? Who is going to take the accountability of incomplete requirements on which our good test cases are built and at the same time, we are targeting to test the code quality. Therefore, the concrete requirements are one of the pre-requisite for developing good test cases and hence the evaluation of the code quality will be impacted accordingly.

How to conclude on code quality based on test coverage?

We have already discussed that if we have adequate test coverage in terms of lines of code and good test cases based on the traceability matrix of requirements then we can ensure the code quality of the project depending on the test results. Here, we are just covering one aspect of the code quality but in real sense there are lot more stuff which impacts the code quality. There are many code scanner tools which helps to evaluate the code quality from the technical perspective. The developer has written the code which is serving the functional purpose but actually that functionality might have coded in the wrong way or the developer has not followed the required coding standards. There could be the events in the code where the code block is dead or the control is not reachable at all. There could be chances for the code block ends into infinite loop. The code may not be properly optimized and taking a toll on the overall system performance. There could be memory leakage or the connections are not properly closed after the block has executed successfully. There could be inappropriate garbage collection at code blocks for the software module. These are some of the performance caveats which directly or indirectly impacts the code quality and cannot be ignored. Therefore, it is advisable to conduct the non functional or performance testing for the software project because these challenges can only be disclosed through code scanner tools such as SONAR CUBE or after conducting the proper performance testing.

Security testing is another aspect of software testing that impacts the code quality. We won’t like to run in the situation where our application is working fine both in terms of functional and performance aspect but unauthorized users can easily break through the system and steal the critical business related information. Therefore, the application should be well tested for security and this ensure how robust the application code has been developed in term of mitigating the security breaches.

Conclusion

Adequate Test coverage metrics can help to reveal the code quality up to a certain extent. The code quality metrics further depends on the other factors. These factors are good test cases designed with the help of traceability metrics, performance testing, code scanning, and security testing. When all these testing aspects are completed successfully then it will be wise to conclude that the code quality is unbeatable and hats off to the overall project team work.

That is all for today! We will continue with the other articles in further session. Please feel free to post your questions relaed to Test coverage metrics.


⇓ Subscribe Us ⇓


If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:


 

Check email in your inbox for confirmation to get latest updates Software Testing for free.


  Happy Testing!!!
 

5 thoughts on “Measuring Code Quality with Test Coverage Metrics”

Leave a Comment

Share This Post