Unit Testing and xUnit

Ronald Rex 1,666 Reputation points
2023-07-24T13:42:23.0466667+00:00

Hi Friends.

I am looking at this example of a Unit Test but the article doesn't explain what the 1 represents... Assert.Equal (expectedValue, sum, 1);

Thanks !!!

 [Fact]  
        public void Task_Add_TwoNumber()  
        {  
            // Arrange  
            var num1 = 2.9;  
            var num2 = 3.1;  
            var expectedValue = 6;  
  
            // Act  
            var sum = MathOperation.Add(num1, num2);  
  
            //Assert  
            Assert.Equal(expectedValue, sum, 1);  
        }  
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2023-07-24T16:43:44.8733333+00:00

    When dealing with floating point numbers there's a chance that you'll encounter slightly inaccurate results due to how these numbers are stored in memory. Sometimes slight inaccuracy is tolerable, so this extra parameter (for Assert.Equal overloads that accept float/double that that predominantly affected by this) is to allow you to factor a small configurable margin of error when performing the comparison:

    User's image

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.