3.5.1 Unit Test
In software development, unit test is a crucial activity to ensure that every module of code functions properly, i.e. the code performs as desired. Unit tests are intended to test each line of code to make sure it functions properly. Error detection at an early stage is critical to prevent more serious issues in the later stages.
Unit test also help in outlining function of every section of code. This makes it easier for developers to understand and maintain their code. If the unit tests continue to pass, you may be certain that the primary functionality will continue to function even after you make significant modifications or rearrange the code.
Unit test can be applied to a function, a module or even to a single line of code. For instance, a mathematical formula is to be incorporated to calculate Zakat of yield from an agriculture land. So, the written formula needs to be tested by the developer for different values based on different possible scenarios; till this line of code passes multiple unit tests. Hence, unit tests ensure the quality of code, by providing developers the confidence in making changes, identify errors early and facilitate code management.
Two test cases are presented in Fig.37, for the code of shopping list example of Fig.33. Test 1 looks for the object "milk," which we know exists and anticipates that the function will return "true." Test 2 looks for the nonexistent object "eggs" and anticipates a "false" response from the method. The tests' outcomes are displayed in the Fig.38 based on the return value of the 'findItem' method.
The website layout is mostly unchanged, with a title and a paragraph displaying the unit test results. For the sake of simplicity, the 'findItem()' method is not modified in this example.
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <title>Unit Tests for Array Search</title>
5 </head>
6 <body>
7 <h1>Unit Tests for 'findItem' Function</h1>
8 <p id="testResults"></p>
9 <script>
10 const shoppingList = ["apples", "bread", "milk", "cheese"];
11
12 function findItem(itemName) {
13 for let i = 0; i < shoppingList.length; i++ {
14 if (shoppingList[i] == itemName) {
15 return true;
16 }
17 18 return false; 19 20 21 let testResults "; 22 testResults "Test 1 (Existing Item): "; 23 if (findItem("milk") true){ 24 testResults "Passed\n"; 25 } else{ 26 testResults "Failed\n"; 27 } 28 testResults "\n Test 2 (Non- Existing Item): "; 29 if (findItem("eggs") false){ 30 testResults "Passed\n"; 31 } else{ 32 testResults "Failed\n"; 33 } 34 document.getElementById("testResults").textContent testResults; 35 </script> 36 </body> 37 </html>
Unit Tests for 'findItem' Function
Test 1 (Existing Item): Passed
Test 2 (Non- - existing (lcm): Passed
Fig.38:Unit Test- 1.html
3.5.2. Debugging Allows to Analyze the Code
Debugging is useful as:
Debugging identifies flaws in the code that might affect the layout or functionality of your webpage.
Debugging allows you to observe step- by- step operation of your code.
You may create your website more quickly by identifying and rectifying mistakes right away.
Debugging techniques in IDE like Visual Studio.NET involve mainly:
Breakpoint:
Assume you are monitoring a certain line of code where you think that something is dubious. The application pauses at breakpoints and allows you to examine the code (variable values, data flow, etc.).