# Software Testing - Branch/Decision Testing

Updated on May 28, 2011 Branch testing and decision testing are closely related. We will treat them same. When 100% coverage is concerned, the two techniques are the same.

Branch and decision testing require examination of the source code and the creation of tests that will exercise individual branches of code. A branch is a route from a conditional statement. On a flowchart a branch is a line leaving a decision diamond. For 100% Branch Coverage we should test all branches in a piece of code, we need to achieve all possible results from all of the decision statements.

## An Example

Take, as an example, the following source code:

```Read a                  // Executable
IF a > b THEN           // Executable
Print "a > b"       // Executable
IF a > 10 THEN      // Executable
Print "a > 10"  // Executable
ENDIF
ELSE
Print "a =< b"      // Executable
ENDIF```

This is the code we used in Statement Testing example. The code reads two numbers into variables (a and b). It then prints out a message indicating which number is the larger. If the number in variable a is larger than the number in variable b then the number a is compared to the number 10.

Executable code is indicated in the comments. The ELSE and ENDIF statements are considered to be part of earlier IF statements.

We can represent this code in a flow chart. Every box in the flowchart represents an individual statement.

## Test 1

As before, we shall run the code with the values: 30 and 20. We expect the output to look like:

a > b
a > 10

The exercised branches are highlighted in the flowchart below:

This test has exercised two of the four branches in the code, achieving 50% Branch Coverage.

## Test 2

For our second test we shall run the code with the values: 20 and 30. We expect the output to look like:

a <= b

This test exercises one new branch of the code as highlighted in the following flowchart. We have not yet exercised the N branch from the a > 10 test.

We have now exercised 3 of the 4 branches, achieving 75% Branch Coverage.

## Test 3

To exercise the remaining branch, we shall run the code with the values: 3 and 2. We expect the output to look like:

a > b

The exercised branches are highlighted below.

By performing all three tests (Tests 1, 2 and 3) we can test all branches of the code. In this case, 100% branch testing requires one more test than 100% statement testing. This is because statement testing does not require us to test branches where there are no executable statements. It can be shown that 100% Statement Coverage requires no more tests than are required for 100% Branch Coverage.

working
{"ft_lang_label":"__label__en","ft_lang_prob":0.8762823,"math_prob":0.8215724,"size":2648,"snap":"2022-40-2023-06","text_gpt3_token_len":605,"char_repetition_ratio":0.15279879,"word_repetition_ratio":0.07112971,"special_character_ratio":0.23791541,"punctuation_ratio":0.089108914,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95672727,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-04T16:44:56Z\",\"WARC-Record-ID\":\"<urn:uuid:71f86e87-b0d9-47cb-b910-e1a331154f31>\",\"Content-Length\":\"95562\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5a76b47b-880b-43ce-812e-5112d0fbe31e>\",\"WARC-Concurrent-To\":\"<urn:uuid:813f2721-4f33-42ea-a45e-14e0bc159c24>\",\"WARC-IP-Address\":\"52.70.155.212\",\"WARC-Target-URI\":\"https://hubpages.com/technology/BranchDecisionTesting\",\"WARC-Payload-Digest\":\"sha1:4N6WRKPMOFI24PEDIE74Q2PB66ZI5ZZG\",\"WARC-Block-Digest\":\"sha1:4FZ2TTU35AIOCXHCW5KFG4D35LBPJOBV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500140.36_warc_CC-MAIN-20230204142302-20230204172302-00796.warc.gz\"}"}