# White box testing: What are they and how to implement one in Python?

You don't have to be a testing professional to understand the concepts of white box testing and black box testing., its definitions are as simple as applying these techniques. And Python, There are already libraries that can help us to complement the study of code blocks and algorithms by applying these techniques., but, What is a white box test?

White box tests (also known as glass box tests or structural tests) focus on the procedural details of the software, so its design is strongly linked to the source code, which it's, this means that we have to carry out a "study", Or better, an analysis of our code, for this, There are three fundamental guidelines for a successful white box test.

Coverage tests: It is basically about executing at least once each sentence. But to comply with the coverage tests, several test cases are needed:

• Each condition must be fulfilled in one case and not in another.
• And it may be impossible to cover the 100%
• Code that never runs: impossible conditions

Condition tests: Meet or not each part of each condition. Multiple test cases are needed:

• Determine simple expressions in the conditions
• One for each logical operand or comparison
• Each simple expression must be fulfilled in one case and not in another, being decisive in the result.

Loop testing: It is about verifying that the cycles or loops, as much as “while” (repeat while) like the loop “for” (repeat for), are not infinite or work correctly:

• Get special replay numbers
• Simple loops:
○ Repeat zero, once and twice
○ Repeat a middle number (typical) times
○ Repeat max-1, maximum and maximum +1!
• Nested loops
○ Repeat a middle number (typical) the inner loops, the minimum external ones, and vary the repetitions of the intermediate loop tested.
○ Test it with each nesting level

However, once these concepts are understood, let's see a basic example of this type of test:

Proof of coverage and conditions:

If it is true that A>1 and B>2 y C>3:
• You can execute S = (A+B) * (B+C) * (A+C)
• But S = S + 1 is never going to be fulfilled, since S will never be less than 0, since the first condition asks that they be numbers greater than 0.
• Coverage tests would only work on:

✔ S= (A+B) * (B+C) * (A+C)
✔ S= S/(ABC)
✘ S= S+1 (which is the same as S + = 1)

If it is not fulfilled that A>1 and B>2 y C>3:
• Operations would never be executed within the condition (I would not do anything directly).
✘ S=(A+B) * (B+C) * (A+C)
✘ S=S/ABC
✘ S=S+1

Loop test:
Si N > 0:
• You can execute the instructions that follow
Si N <= 0:
• The instructions that follow would never be executed

At the same time, we can work with Python to get a coverage test percentage, simply installing a library called "Coverage”, which provides us with that percentage according to the code block that we are going to implement with it.
For this, we open our terminal / console and execute the following instruction:

pip install coverage

To verify that it is actually installed, we are going to execute the following command inside our terminal or command prompt:

coverage –version

And ready!, We already have the Coverage library installed, now to use it, we go to the path where our file or script is located to analyze it from our terminal / Command Prompt.
Once our script or file.py is located, inside our terminal we execute the command: coverage run nombre_de_script.py and we proceed to carry out our coverage tests:

If we do the first test (A > 1 and B > 2 y C > 3), we can now execute a new command: coverage report

What does that report mean?, easy, "Stmts" means the lines of code where the instructions were executed, y “miss”, where they were not executed, Thus, the coverage test was a 90%, why?, because in advance, we knew that a line of code was not going to be executed:

✔ S= (A+B) * (B+C) * (A+C)
✔ S= S/(ABC)
✘ S= S+1 (which is the same as S + = 1)

And what would have happened if N was equal to 0?:

He gave us a proof of coverage of the 20%, since what is inside the loop would never be executed, only one line of code was executed.
What's more, Coverage can create a report in an HTML file with the date and time of when we performed our test, we just run: coverage html, within the same directory it creates a folder called "htmlcov" where, if we double click on index.html, open the report in our browser:

These tests can help us a lot to be able to see which parts of the code are not being executed, You can find the official Coverage documentation at: https://coverage.readthedocs.io/en/coverage-5.1/ (site in english).

Latest posts by Facundo Kimbo (see all)