Code coverage tutorial for your React Native app — Part 1
In this tutorial, I would like to show you how easy it is to create code coverage for your React Native application. We will use the popular create-react-native-app to set up our application. Everything we need is already in place and keeping an eye on the overall code coverage is a no-brainer.
In this tutorial, you will learn:
- Code coverage meaning
- Simplified QA processes & compliance audits Faster time to market.
- Code Coverage use
- Code Coverage Methods
- Statement Coverage
- Decision Coverage
- Branch Coverage
- Condition Coverage
- Code Coverage Tools
- Advantages of Using Code Coverage
What is code coverage?
Code coverage is a measure which describes the degree of which the source code of the program has been tested. It is one form of white box testing which finds the areas of the program not exercised by a set of test cases. It also creates some test cases to increase coverage and determining a quantitative measure of code coverage.
In most cases, the code coverage system gathers information about the running program. It also combines that with source code information to generate a report about the test suite’s code coverage.
Why use code coverage?
To know how well our tests actually test our code To know whether we have enough testing in place
To maintain the test quality over the lifecycle of a project
It helps you to measure the efficiency of test implementation It offers a quantitative measurement.
It defines the degree to which the source code has been tested.
Code coverage methods
Statement Coverage Decision Coverage Branch Coverage
1. Statement coverage:
this is a white box testing technique in which all the executable statements in the source code are executed at least once. It is used for calculation of the number of statements in source code which have been executed. The main purpose of Statement Coverage is to cover all the possible paths, lines and statements in source code.
Statement coverage is used to derive scenario based upon the structure of the code under test.
The formula to calculate Statement Coverage:
Generally in any software, if we look at the source code, there will be a wide variety of elements like operators, functions, looping, exceptional handlers,.
Let’s understand this with below sample, how to calculate statement coverage.
2. Decision Coverage
Decision Coverage is a white box testing technique which reports the true or false outcomes of each boolean expression of the source code. The goal of decision coverage testing is to cover and validate all the accessible source code by checking and ensuring that each branch of every possible decision point is executed at least once.
The formula to calculate Decision coverage:
Let’s understand this with below sample, how to calculate decision coverage.
3. Branch coverage
Branch coverage technique is used to cover all branches of the control flow graph. It covers all the possible outcomes (true and false) of each condition of decision point at least once. Branch coverage technique is a white box testing technique that ensures that every branch of each decision point must be executed.
However, branch coverage technique and decision coverage technique are very similar, but there is a key difference between the two. Decision coverage technique covers all branches of each decision point whereas branch testing covers all branches of every decision point of the code.
The formula to calculate Branch Coverage:
Let’s understand this with below sample, how to calculate branch coverage.
Advantages of branch coverage:
Allows you to validate-all the branches in the code
Allows you to find those areas which are not tested by other testing methods It allows you to find a quantitative measure of code coverage
Branch coverage ignores branches inside the Boolean expressions
4. Condition coverage
where A, B and C represent atomic Boolean expressions (i.e. not divisible in other Boolean sub-expressions). In order to ensure Condition coverage criteria for this example, A, B and C should be evaluated at least one time “true” and one time “false” during testes.
Condition Coverage is a testing method used to test and evaluate the variables or sub-expressions in the conditional statement. The goal of condition coverage is to check individual outcomes for each logical condition. Condition coverage offers better sensitivity to the control flow than decision coverage.
The formula to calculate Condition coverage:
Let’s understand this with below sample, how to calculate conditional coverage.
Code coverage tools:
Below are the list of code coverage tools:
Advantages of code coverage
- Increased product quality
- Enhanced customer satisfaction
- Simplified QA processes & compliance audits Faster time to market
Code base will be easier to maintain (the code you’re writing may be used for a long time as the application is extended, so adequate testing greatly reduces the effort needed to ensure code quality as more features get added to the application later on).
It helps you to measure the efficiency of test implementation.
Code coverage tells you how well the source code has been exercised by your test bench while Functional coverage measures how well the functionality of the design has been covered.