Search Results for

    Show / Hide Table of Contents

    DefaultFloatingPointTolerance

    DefaultFloatingPointToleranceAttribute sets the default tolerance for floating-point equality comparisons (float and double) unless a comparison explicitly specifies a tolerance.

    You can apply it at method, fixture, or assembly scope.

    Constructor

    DefaultFloatingPointToleranceAttribute(double tolerance)
    
    Parameter Type Description
    tolerance double The default tolerance used for floating-point comparisons when no explicit tolerance is specified.

    Applies To

    Test Methods Test Fixtures (Classes) Assembly
    ✅ ✅ ✅
    Note

    When applied at fixture or assembly level, this default tolerance applies to all contained tests unless overridden by a more specific scope or by an explicit tolerance in an assertion.

    Examples

    Fixture Default Tolerance

    [TestFixture]
    [DefaultFloatingPointTolerance(1)]
    public class ToleranceFixtureTests
    {
        [Test]
        public void ComparisonUsingFixtureDefaultTolerance()
        {
            Assert.That(1f, Is.EqualTo(2));
        }
    
        [Test]
        public void ExplicitToleranceOverridesDefault()
        {
            Assert.That(1f, Is.EqualTo(1.2f).Within(0.3));
        }
    }
    

    Method Override

    [TestFixture]
    [DefaultFloatingPointTolerance(1)]
    public class ToleranceMethodOverrideTests
    {
        [Test]
        [DefaultFloatingPointTolerance(2)]
        public void MethodToleranceOverridesFixtureTolerance()
        {
            Assert.That(2f, Is.EqualTo(4));
        }
    }
    

    Notes

    1. This attribute affects default tolerance for floating-point equality comparisons only.
    2. Integer comparisons are not affected.
    3. An explicit tolerance in an assertion (for example, .Within(...)) overrides the default tolerance.

    See Also

    • Assert.AreEqual
    • EqualConstraint — numeric Within tolerances and floating-point comparison behavior (including the Comparing floating-point values section)
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0