Skip to content

Commit

Permalink
using ITestOutputHelper for output more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fangdatto committed Jun 10, 2024
1 parent 29111bc commit 4770848
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Solution1/UnitTestProject/TestsCalculatorX.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
using Xunit;
using Xunit.Abstractions;
using System;
using TestLibrary;
namespace UnitTestProject
{
public class CalculatorTests
public class TestsCalculatorX
{
private readonly MyCalculator _calculator;
private readonly ITestOutputHelper _output;

public CalculatorTests()
public TestsCalculatorX(ITestOutputHelper output)
{
_calculator = new MyCalculator();
_output = output;
}

[Fact]
public void Add_SimpleValues_ShouldReturnCorrectSum()
{
// Arrange
int a = 5;
int b = 3;
_output.WriteLine($"Adding {a} and {b}");

// Act
int result = _calculator.Add(a, b);
_output.WriteLine($"Result of addition: {result}");

// Assert
Assert.Equal(8, result);
Expand All @@ -32,9 +36,11 @@ public void Subtract_SimpleValues_ShouldReturnCorrectDifference()
// Arrange
int a = 5;
int b = 3;
_output.WriteLine($"Subtracting {b} from {a}");

// Act
int result = _calculator.Subtract(a, b);
_output.WriteLine($"Result of subtraction: {result}");

// Assert
Assert.Equal(2, result);
Expand All @@ -46,9 +52,11 @@ public void Multiply_SimpleValues_ShouldReturnCorrectProduct()
// Arrange
int a = 5;
int b = 3;
_output.WriteLine($"Multiplying {a} and {b}");

// Act
int result = _calculator.Multiply(a, b);
_output.WriteLine($"Result of multiplication: {result}");

// Assert
Assert.Equal(15, result);
Expand All @@ -60,9 +68,11 @@ public void Divide_SimpleValues_ShouldReturnCorrectQuotient()
// Arrange
int a = 6;
int b = 3;
_output.WriteLine($"Dividing {a} by {b}");

// Act
double result = _calculator.Divide(a, b);
_output.WriteLine($"Result of division: {result}");

// Assert
Assert.Equal(2.0, result, 1);
Expand All @@ -74,9 +84,11 @@ public void Divide_ByZero_ShouldThrowDivideByZeroException()
// Arrange
int a = 6;
int b = 0;
_output.WriteLine($"Attempting to divide {a} by {b}");

// Act & Assert
Assert.Throws<DivideByZeroException>(() => _calculator.Divide(a, b));
var exception = Assert.Throws<DivideByZeroException>(() => _calculator.Divide(a, b));
_output.WriteLine($"Caught exception: {exception.Message}");
}
}
}

0 comments on commit 4770848

Please sign in to comment.