Skip to content

Week 9: Test Planning and Boundary Testing in C#


1. Purpose of Testing

Goal of Testing: Ensure that code meets requirements, behaves as expected, and handles both typical and edge cases.
Common Types of Testing:
Unit Testing: Tests individual components (e.g., methods or classes) in isolation to verify they function correctly.
Boundary Testing: Tests the limits or boundaries of inputs to ensure the code handles edge cases accurately.

2. Boundary Testing

Definition: A method of testing that focuses on the boundary values of input ranges.
Purpose: Validates that code handles values at the edges of valid ranges correctly, which are often sources of errors.
Example:
If a function is designed to accept numbers between 1 and 10, boundary tests would include values 0, 1, 10, and 11 to ensure proper handling of both within-range and out-of-range inputs.

Boundary Testing Terms

Lower Bound: The smallest valid value.
Upper Bound: The largest valid value.
Just Below/Above Bound: Values immediately outside the valid range, used to test edge conditions.

3. Unit Testing with MSTest

Introduction to MSTest

MSTest: A popular framework in C# for creating and running unit tests.
Basic Structure:
Arrange: Set up objects and initial conditions.
Act: Invoke the code being tested.
Assert: Verify that the result matches the expected outcome.

Example of an MSTest Unit Test

csharp
Copy code
[TestMethod]
public void Test_IsRunning_StudentCountBoundary()
{
// Arrange
QUTUnit unit = new QUTUnit();
unit.StudentCount = 1;

// Act
bool isRunning = unit.IsRunning();

// Assert
Assert.IsTrue(isRunning, "Unit should be running with at least 1 student.");
}

Common Assert Methods

Assert.AreEqual(expected, actual): Verifies that the actual result matches the expected value.
Assert.IsTrue(condition): Verifies that a condition is true.
Assert.IsFalse(condition): Verifies that a condition is false.

4. Boundary Testing Example in MSTest

Scenario: Testing a method that requires at least 1 student enrolled for the unit to run.
Example Test:
csharp
Copy code
[TestMethod]
public void Test_IsRunning_Boundary()
{
QUTUnit unit = new QUTUnit();
unit.StudentCount = 0;
Assert.IsFalse(unit.IsRunning(), "Unit should not run with zero students.");
unit.StudentCount = 1;
Assert.IsTrue(unit.IsRunning(), "Unit should run with one or more students.");
}

Explanation: This test validates both just below and at the lower boundary condition (0 and 1 students), ensuring the IsRunning method performs as expected.

5. Best Practices for Testing

Identify Edge Cases: Test the limits of input data to uncover potential issues.
Keep Tests Independent: Each test should be self-contained and not rely on others.
Use Descriptive Names: Clearly name test methods to describe what is being tested and the expected outcome.
Follow the Arrange-Act-Assert Pattern: Organize tests in three distinct sections to improve readability and maintainability.

Quick Tips for MCQ Preparation

Understand Boundary Testing: Focus on recognizing scenarios where lower and upper bounds are tested.
MSTest Fundamentals: Be familiar with [TestMethod] attributes and common assertion methods.
Analyze Testing Scenarios: Practice interpreting test case scenarios to predict the expected outcomes based on boundary conditions.
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.