Search Results for

    Show / Hide Table of Contents

    TestOf

    TestOfAttribute is used to specify the class or type that a test fixture or test method is testing. This metadata helps document the relationship between tests and the code under test, and can be used by test runners and reporting tools to organize test results.

    Constructors

    TestOfAttribute(Type type)
    TestOfAttribute(string typeName)
    
    Parameter Type Description
    type Type The type that is being tested. The full type name is stored.
    typeName string The name of the type that is being tested.

    Applies To

    Test Methods Test Fixtures (Classes) Assembly
    ✅ ✅ ✅

    Alternative Syntax

    TestOf can also be specified as a named parameter on [TestFixture] or [Test] attributes:

    [TestFixture(TestOf = typeof(MyClass))]
    [Test(TestOf = typeof(MyClass))]
    

    Examples

    Using the Attribute

    [TestFixture]
    [TestOf(typeof(Calculator))]
    public class CalculatorTests
    {
        [Test]
        public void Add_TwoPositiveNumbers_ReturnsSum()
        {
            var calculator = new Calculator();
            Assert.That(calculator.Add(2, 3), Is.EqualTo(5));
        }
    
        [Test]
        [TestOf(typeof(AdvancedCalculator))]
        public void SquareRoot_PositiveNumber_ReturnsCorrectResult()
        {
            // This specific test is for AdvancedCalculator
            var calculator = new AdvancedCalculator();
            Assert.That(calculator.SquareRoot(4), Is.EqualTo(2.0));
        }
    }
    

    Using String Names

    [TestFixture]
    [TestOf("Snippets.NUnit.Attributes.Calculator")]
    public class CalculatorTestsWithStringName
    {
        [Test]
        [TestOf(nameof(AdvancedCalculator))]
        public void TestUsingNameof()
        {
            // Using nameof() provides compile-time safety
            var calculator = new AdvancedCalculator();
            Assert.That(calculator.Add(1, 1), Is.EqualTo(2));
        }
    }
    

    Using Named Parameter Syntax

    [TestFixture(TestOf = typeof(Calculator))]
    public class CalculatorTestsWithNamedParameter
    {
        [Test(TestOf = typeof(AdvancedCalculator))]
        public void TestUsingNamedParameterSyntax()
        {
            var calculator = new AdvancedCalculator();
            Assert.That(calculator.SquareRoot(9), Is.EqualTo(3.0));
        }
    }
    

    Notes

    1. This attribute inherits from PropertyAttribute and sets the TestOf property with the full type name.
    2. Using typeof() is preferred over string names as it provides compile-time type checking.
    3. For string names, consider using nameof() for compile-time safety when the type is accessible.
    4. Multiple TestOf attributes can be applied to the same element when a test covers multiple types.

    See Also

    • Property Attribute
    • Description Attribute
    • Category Attribute
    • 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