Reference no: EM132865251
ITAP2008 Software Testing - Victorian Institute of Technology
Lab 7
Creating a project and supporting unit tests
During this lab session, you'll go through the process of creating a new project, as well as some supporting unit tests.
Task 1: Creating a new class
In this task, you'll create a basic class Calculator.cs.
1 Open Visual Studio 2013.
2 From the main menu, select File | New | Project.
3 In the New Project dialog, select the Visual C# | Windows Desktop and Console Application template (As shown in Figure 1). Type "Calculator" as the Name (This will be the namespace) and click OK.
4 In Solution Explorer, Right-click the Program.cs and select Rename. Change the name to "MyCalculator.cs".
5 When asked to update the name of the class itself, click Yes.
6 Add the following C# code for the class MyCalculator.cs.
C#?
static void Main(string[] args)
{
int a = 5, b = 8, c = 233; int d = a + c - b; Console.WriteLine(d);
}
Note: For the purposes of this lab, all operations will be performed using the int value type. In the real world, calculators would be expected to scale to a much greater level of precision. However, the requirements have been relaxed here to focus on the debugging.
Task 2: Adding a Breakpoint
In this task, you'll add a breakpoint to the class and the code will stop execution from that breakpoint. Then you can manually run the code using key F11
There are different ways you can add break points.
1. First move the cursor to the line you need to add breakpoint. And from the menu select add Breakpoint.
2. Place a breakpoint by clicking in the left gutter
Once you added the breakpoint your IDE should now look something as in the figure 3.
Task 3: Building and Debugging Application
1. Build application, by clicking the Build |Build Solution
2. Now select Debug | Start Debugging.
3. Now select Debug | Start Debugging.
4. The program will stop at the breakpoint. Now use F11 to go forward in the code (line by line). You will be able to traverse line by line in the program. At the same time, you can check the attribute values and check the problems with assignment of the values.
Task 4: Understanding Debug Menu
Figure 12 shows the Debug menu with Visual Studio 2013. Using this user can start debugging process
by adding breakpoints. And stop debugging. Can step into codes and go through one line by line to examine the code.
Task 5: Debugging a Small Code
1. Create a new C# class with the name CalcFactorial. New Project | Windows Desktop | Console
Application
2. Add following code between class brackets. C#
static void Main(string[] args)
{
Console.WriteLine("Enter a Number to find Factorial"); int number = Convert.ToInt32(Console.ReadLine());
int fact = getFactorial(number);
Console.WriteLine("{0} Factorial is {1}", number, fact);
}
// New method getFactorial
public static int getFactorial(int num) { if (num == 1)
{
return 1;
}
return num * getFactorial(num-1);
}
3. Add breakpoint to the code on the keyboard input assignment statement. (Use enters the number here and assign the value on the integer type number variable.)
4. Build and debug the console application. Debug | Start Debugging
5. The application starts for the input.
6. User enters the value. (Note: You can enter any integer value.)
7. Now press F11. And move forward in the code. Note the yellow arrow. The yellow arrow shows that how F11 moves through you the code. It has moved to the next line and highlighted in
yellow. Same time note that the Autos section in the left hand side bottom input 5 has assigned on the variable number (this shows in red color).
8. Press F11. Observe how the yellow arrow moves.
9. The pointer checks the if condition.
10. Move the cursor onto the if statement. Then it shows whether the values assigned satisfy the condition or not. (Note: in the following example the statement become false due to not meeting the condition.)
11. Next F11 press.
12. The function getFactorial() is a recursive function and called recursively. Next f11 press will call the function again.
13. Now notice that the value for num variable is 4. Still does not meet the condition criteria.
14. The third call for function getFactorial(). The function will be recursively called and you will be able to see how the pointer moves and how conditions will be met.
15. Meeting if condition. The recursive call will deduct num variable and now numis 1 and enters to the if condition. See the display. It says now the condition is true with num variable.
16. Returning 1. Going into if condition and returning 1.
17. For the next F11 the pointer will exit from the function getFactorial and returns to the main method. Then executes Console.WriteLine statement and prints output on the console.
18. Printing the output on the console.
Task 5: Practice - How loops works - Understand by debugging
1. Create new console application and add following code segment to that.
C#
class Stars
{
static void Main(string[] args)
{
for (int i = 0; i <= 3; i++ )
{
for (int j = 0; j <= 3; j++)
{
Console.Write("*");
}
//Console.WriteLine();
}
Console.ReadLine();
}
2. Add breakpoint to the outer for loop as showing in the Figure 30.
3. Run the debugging process and see how for loops works and prints the stars.
Task 4: Exercises
1. Use following C# code segment.
o Expected output = 288
o Actual output = 219
2. Debug the code to locate the error and fix the error.
C#
class Stars
{
static void Main(string[] args)
{
int a = 48; int b = 2; int c = 9; int d = 3;
int f = a/b*9+3;
Console.WriteLine("The answer is {0}",f);
Console.ReadLine();
}
}
Note: All figures are attached in below file.
Attachment:- Lab Tutorial Lesson 7.rar