0% found this document useful (0 votes)
16 views6 pages

Lab 5

Uploaded by

lhglalnbp123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Lab 5

Uploaded by

lhglalnbp123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

REVIEW TESTING

The lab session will provide the following examples

 Unit testing in C# with xUnit Framework

A CLASS TO TEST

public class Calculator


{
public int Add(int a, int b)
{
return a + b;
}

public int Subtract(int a, int b)


{
return a - b;
}

public int Multiply(int a, int b)


{
return a * b;
}

public int Divide(int a, int b)


{
if (b == 0)
{
throw new ArgumentException("Cannot divide by zero.", nameof(b));
}
return a / b;
}
}

FACT

[Fact] is an attribute used to define a test method. It is part of the xUnit testing framework's attribute-
based model for writing tests. A test method marked with the [Fact] attribute is a simple, standalone test
that doesn't take any parameters.
public class CalculatorTests
{
[Fact]
public void Add_WhenGivenTwoIntegers_ShouldReturnTheSum()
{
// Arrange
Calculator calculator = new Calculator();

// Act
int result = calculator.Add(3, 5);

// Assert
Assert.Equal(8, result);
}

[Fact]
public void Subtract_WhenGivenTwoIntegers_ShouldReturnTheDifference()
{
// Arrange
Calculator calculator = new Calculator();

// Act
int result = calculator.Subtract(8, 3);

// Assert
Assert.Equal(5, result);
}

[Fact]
public void Multiply_WhenGivenTwoIntegers_ShouldReturnTheProduct()
{
// Arrange
Calculator calculator = new Calculator();

// Act
int result = calculator.Multiply(4, 6);

// Assert
Assert.Equal(24, result);
}

[Fact]
public void Divide_WhenGivenTwoIntegers_ShouldReturnTheQuotient()
{
// Arrange
Calculator calculator = new Calculator();
// Act
int result = calculator.Divide(10, 2);

// Assert
Assert.Equal(5, result);
}

[Fact]
public void Divide_WhenDividingByZero_ShouldThrowException()
{
// Arrange
Calculator calculator = new Calculator();

// Act and Assert


Assert.Throws<ArgumentException>(() => calculator.Divide(10, 0));
}
}

THEORY AND MEMBERDATA

Both [Theory] with [InlineData] and [MemberData] offer ways to write parameterized tests, and you can
choose the one that fits your preferences or requirements. They help you avoid duplicating test logic and
make it easier to add new test cases without creating additional methods

public class CalculatorTests


{
public static IEnumerable<object[]> AddTestData =>
new List<object[]>
{
new object[] { 3, 5, 8 },
new object[] { -2, 7, 5 },
new object[] { 0, 0, 0 },
new object[] { int.MaxValue, 1, int.MinValue }, // Overflow case
// Add more test cases as needed
};

public static IEnumerable<object[]> SubtractTestData =>


new List<object[]>
{
new object[] { 8, 3, 5 },
new object[] { 5, 7, -2 },
new object[] { 0, 0, 0 },
new object[] { int.MinValue, 1, int.MaxValue }, // Overflow case
// Add more test cases as needed
};

public static IEnumerable<object[]> DivideTestData =>


new List<object[]>
{
new object[] { 8, 2, 4 },
new object[] { 10, -2, -5 },
new object[] { 0, 5, 0 },
// Add more test cases as needed
};

[Theory]
[MemberData(nameof(AddTestData))]
public void Add_ShouldReturnTheSum(int a, int b, int expected)
{
// Arrange
Calculator calculator = new Calculator();

// Act
int result = calculator.Add(a, b);

// Assert
Assert.Equal(expected, result);
}

[Theory]
[MemberData(nameof(SubtractTestData))]
public void Subtract_ShouldReturnTheDifference(int a, int b, int
expected)
{
// Arrange
Calculator calculator = new Calculator();

// Act
int result = calculator.Subtract(a, b);

// Assert
Assert.Equal(expected, result);
}

[Theory]
[MemberData(nameof(DivideTestData))]
public void Divide_ShouldReturnTheQuotient(int a, int b, int expected)
{
// Arrange
Calculator calculator = new Calculator();

// Act
int result = calculator.Divide(a, b);
// Assert
Assert.Equal(expected, result);
}

[Theory]
[InlineData(int.MaxValue, 0)] // Divide by zero case
public void Divide_WhenDividingByZero_ShouldThrowException(int a, int b)
{
// Arrange
Calculator calculator = new Calculator();

// Act and Assert


Assert.Throws<ArgumentException>(() => calculator.Divide(a, b));
}

EXERCISE

OBJECTIVE

Implement the body for the empty functions below

public class EmailAnalyzer


{
public bool IsValidEmail(string email)
{
// Implement logic to check if the input string is a valid email
address.
}

public string ExtractDomain(string email)


{
// Implement logic to extract the domain from the email address.

public bool IsKnownDomain(string email, string[] knownDomains)


{
// Implement logic to check if the email address belongs to a known
domain.

}
}

INSTRUCTIONS:

1. Complete the unfinished functions.


2. Write the unit tests for those three functions once the functions completed
3. Make sure you use both [Fact] and [Theory] attributes

You might also like