TestAttribute
Adding this attribute to a method makes the method callable from the NUnit test runner.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Builders;
using System;
namespace NUnit.Framework
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class TestAttribute : NUnitAttribute, ISimpleTestBuilder, IApplyToTest, IImplyFixture
{
private object _expectedResult;
private bool _hasExpectedResult;
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;
}
}
public void ApplyToTest(Test 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") && TestOf != (Type)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.");
}
public TestMethod BuildFrom(IMethodInfo method, Test suite)
{
TestCaseParameters testCaseParameters = null;
if (_hasExpectedResult) {
testCaseParameters = new TestCaseParameters();
testCaseParameters.ExpectedResult = ExpectedResult;
}
return _builder.BuildTestMethod(method, suite, testCaseParameters);
}
}
}