TestAttribute
Marks the method as callable from the NUnit test runner.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Builders;
using System;
using System.Runtime.CompilerServices;
namespace NUnit.Framework
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class TestAttribute : NUnitAttribute, ISimpleTestBuilder, IApplyToTest, IImplyFixture
{
private object _expectedResult;
private bool _hasExpectedResult;
[System.Runtime.CompilerServices.Nullable(1)]
private readonly NUnitTestCaseBuilder _builder = new NUnitTestCaseBuilder();
public string Description { get; set; }
public string Author { get; set; }
public Type TestOf { get; set; }
public object ExpectedResult {
get {
return _expectedResult;
}
set {
_expectedResult = value;
_hasExpectedResult = true;
}
}
[System.Runtime.CompilerServices.NullableContext(1)]
public void ApplyToTest(Test test)
{
Guard.ArgumentValid(test.Method != null, "This attribute must only be applied to tests that have an associated method.", "test");
if (!test.Properties.ContainsKey("Description") && Description != null)
test.Properties.Set("Description", Description);
if (!test.Properties.ContainsKey("Author") && Author != null)
test.Properties.Set("Author", Author);
if (!test.Properties.ContainsKey("TestOf") && (object)TestOf != null)
test.Properties.Set("TestOf", TestOf.FullName());
if (_hasExpectedResult && test.Method.GetParameters().Length != 0)
test.MakeInvalid("The 'TestAttribute.ExpectedResult' property may not be used on parameterized methods.");
}
[System.Runtime.CompilerServices.NullableContext(1)]
public TestMethod BuildFrom(IMethodInfo method, [System.Runtime.CompilerServices.Nullable(2)] Test suite)
{
TestCaseParameters parms = null;
if (_hasExpectedResult)
parms = new TestCaseParameters {
ExpectedResult = ExpectedResult
};
return _builder.BuildTestMethod(method, suite, parms);
}
}
}