# White Box Testing

The term 'white box' is used because of the internal perspective of the system.&#x20;

<figure><img src="https://824924021-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjEG2NepZQd4awmKtG6qY%2Fuploads%2FxKdsqvFEjd4Fz67IGZr7%2FWhite-Box-Testing.png?alt=media&#x26;token=04a7ec24-d6c1-4fe7-bbb1-00da7479e8ca" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
The clear box or white box or transparent box name denote the ability to see through the software's outer shell into its inner workings.
{% endhint %}

## Developers do White Box Testing&#x20;

In this, the developer will test every line of the code of the program. The developers perform the White-box testing and then send the application or the software to the testing team, where they will perform the [black box testing](https://qatesting.gitbook.io/qa/manual-testing/types/black-box-testing) and verify the application along with the requirements and identify the bugs and sends it to the developer.

The developer fixes the bugs and does one round of white box testing and sends it to the testing team. Here, fixing the bugs implies that the bug is deleted, and the particular feature is working fine on the application.

Here, the test engineers will not include in fixing the defects for the following reasons:

* Fixing the bug might interrupt the other features. Therefore, the test engineer should always find the bugs, and developers should still be doing the bug fixes.
* If the test engineers spend most of the time fixing the defects, then they may be unable to find the other bugs in the application.

### Advantages of White Box testing

* White box testing optimizes code so hidden errors can be identified.
* Test cases of white box testing can be easily automated.
* This testing is more thorough than other testing approaches as it covers all code paths.
* It can be started in the SDLC phase even without GUI.

### Disadvantages of White Box testing

* White box testing is too much time consuming when it comes to large-scale programming applications.
* White box testing is much expensive and complex.
* It can lead to production error because it is not detailed by the developers.
* White box testing needs professional programmers who have a detailed knowledge and understanding of programming language and implementation.

### Difference between white-box testing and black-box testing

Following are the significant differences between white box testing and black box testing:

<table data-full-width="true"><thead><tr><th>White-box testing</th><th>Black box testing</th></tr></thead><tbody><tr><td>The developers can perform white box testing.</td><td>The test engineers perform the black box testing.</td></tr><tr><td>To perform White Box Testing, we should have an understanding of the programming languages.</td><td>To perform Black Box Testing, there is no need to have an understanding of the programming languages.</td></tr><tr><td>In this, we will look into the source code and test the logic of the code.</td><td>In this, we will verify the functionality of the application based on the requirement specification.</td></tr><tr><td>In this, the developer should know about the internal design of the code.</td><td>In this, there is no need to know about the internal design of the code.</td></tr></tbody></table>

<div data-full-width="true"><figure><img src="https://images.unsplash.com/photo-1422207049116-cfaf69531072?crop=entropy&#x26;cs=srgb&#x26;fm=jpg&#x26;ixid=M3wxOTcwMjR8MHwxfHNlYXJjaHwyfHx5aW4lMjBhbmQlMjB5YW5nfGVufDB8fHx8MTY5MTU3ODM0MHww&#x26;ixlib=rb-4.0.3&#x26;q=85" alt=""><figcaption></figcaption></figure></div>

## Techniques Used in WBT

## Statement testing&#x20;

Each statement in the code is executed at least once. This ensures that each line of code is executed.

If the code is:

```java
x = 5;  
y = 10;java
z = x + y; 
Print z;
```

A test case would simply execute all 4 statements.

## Branch testing

Each decision point (if/else, switch case, etc.) in the code is tested so that both paths (true and false branches) are executed.

If the code is:

```java
if(a > b){
    c = a;    
} else {
    c = b; 
}
```

Two test cases would be:

1. a > b is true
2. a > b is false

## Condition testing

Each boolean condition in if/else statements, while loops, etc. is tested for both true and false values.

If the code is:

```java
 if(age > 18){
    eligible = true;
}
```

Test cases would be:

1. age = 19
2. age = 17

## Path testing

All possible paths through the code are tested. This achieves high coverage but can be difficult to implement for complex code.

## Loop testing

All loops in the code are tested for a minimum of 0, 1 and multiple iterations. Loop boundaries and conditions are also tested.

For a while loop:

```java
while(i < 10){
    i++;
}
```

Test cases would be:

1. i = 0 (loops 10 times)
2. i = 5 (loops 5 times)
3. i = 10 (loops 0 times)

## Data flow testing&#x20;

Tests are designed based on definition-use pairs of variables to ensure all definitions and uses of variables are exercised.

For code with variables:

```java
a = 5;  
b = a + 2;  
c = b * 3;
```

Test cases would test the definition of a, and the uses of a in b and c.

## Logical testing

Complex logical expressions are evaluated for all possible logical combinations of inputs.

For a complex logical expression:

```java
result = (a && b) || (c && !d)
```

Test cases would evaluate all combinations of a,b,c,d.
