TestCaseAttribute
public class TestCaseAttribute : NUnitAttribute, ITestBuilder, ITestCaseData, ITestData, IImplyFixture
Marks a method as a parameterized test suite and provides arguments for each test case.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace NUnit.Framework
{
[NullableContext(2)]
[Nullable(0)]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class TestCaseAttribute : NUnitAttribute, ITestBuilder, ITestCaseData, ITestData, IImplyFixture
{
private object _expectedResult;
private RunState _originalRunState;
private DateTimeOffset? _untilDate;
private Type _testOf;
public string TestName { get; set; }
public RunState RunState { get; set; }
[Nullable(new byte[] {
1,
2
})]
[field: Nullable(new byte[] {
1,
2
})]
public object[] Arguments {
[return: Nullable(new byte[] {
1,
2
})]
get;
}
[Nullable(1)]
[field: Nullable(1)]
public IPropertyBag Properties {
[NullableContext(1)]
get;
}
public object ExpectedResult {
get {
return _expectedResult;
}
set {
_expectedResult = value;
HasExpectedResult = true;
}
}
public bool HasExpectedResult { get; set; }
public string Description {
get {
return Properties.Get("Description") as string;
}
[param: DisallowNull]
set {
ArgumentNullException.ThrowIfNull(value, "value");
Properties.Set("Description", value);
}
}
public string Author {
get {
return Properties.Get("Author") as string;
}
[param: DisallowNull]
set {
ArgumentNullException.ThrowIfNull(value, "value");
Properties.Set("Author", value);
}
}
public Type TestOf {
get {
return _testOf;
}
[param: DisallowNull]
set {
ArgumentNullException.ThrowIfNull(value, "value");
_testOf = value;
Properties.Set("TestOf", value.FullName());
}
}
public string Ignore {
get {
return IgnoreReason;
}
[param: DisallowNull]
set {
ArgumentNullException.ThrowIfNull(value, "value");
IgnoreReason = value;
}
}
public bool Explicit {
get {
return RunState == RunState.Explicit;
}
set {
RunState = ((!value) ? RunState.Runnable : RunState.Explicit);
}
}
public string Reason {
get {
return Properties.Get("_SKIPREASON") as string;
}
[param: DisallowNull]
set {
ArgumentNullException.ThrowIfNull(value, "value");
Properties.Set("_SKIPREASON", value);
}
}
public string IgnoreReason {
get {
return Reason;
}
[param: DisallowNull]
set {
ArgumentNullException.ThrowIfNull(value, "value");
_originalRunState = RunState;
RunState = RunState.Ignored;
Reason = value;
}
}
public string IncludePlatform { get; set; }
public string ExcludePlatform { get; set; }
[Nullable(new byte[] {
2,
1
})]
[field: Nullable(new byte[] {
2,
1
})]
public Type[] TypeArgs {
[return: Nullable(new byte[] {
2,
1
})]
get;
[param: Nullable(new byte[] {
2,
1
})]
set;
}
public string Category {
get {
return Properties.Get("Category") as string;
}
[param: DisallowNull]
set {
ArgumentNullException.ThrowIfNull(value, "value");
foreach (string item in value.Tokenize(',', false)) {
Properties.Add("Category", item);
}
}
}
[StringSyntax("DateTimeFormat")]
public string Until {
get {
return Properties.Get("IgnoreUntilDate") as string;
}
[param: DisallowNull]
set {
if (!string.IsNullOrEmpty(IgnoreReason)) {
_untilDate = DateTimeOffset.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
Properties.Set("IgnoreUntilDate", _untilDate.Value.ToString("u"));
} else
RunState = RunState.NotRunnable;
}
}
public TestCaseAttribute(params object[] arguments)
{
RunState = RunState.Runnable;
Arguments = ((arguments == null) ? new object[1] : arguments);
Properties = new PropertyBag();
}
public TestCaseAttribute(object arg)
{
RunState = RunState.Runnable;
Arguments = new object[1] {
arg
};
Properties = new PropertyBag();
}
public TestCaseAttribute(object arg1, object arg2)
{
RunState = RunState.Runnable;
Arguments = new object[2] {
arg1,
arg2
};
Properties = new PropertyBag();
}
public TestCaseAttribute(object arg1, object arg2, object arg3)
{
RunState = RunState.Runnable;
Arguments = new object[3] {
arg1,
arg2,
arg3
};
Properties = new PropertyBag();
}
[NullableContext(1)]
private TestCaseParameters GetParametersForTestCase(IMethodInfo method)
{
try {
TestCaseParameters testCaseParameters = new TestCaseParameters(this) {
TypeArgs = TypeArgs
};
testCaseParameters.AdjustArgumentsForMethod(method);
return testCaseParameters;
} catch (Exception exception) {
return new TestCaseParameters(exception);
}
}
[NullableContext(1)]
[IteratorStateMachine(typeof(<BuildFrom>d__69))]
public IEnumerable<TestMethod> BuildFrom(IMethodInfo method, [Nullable(2)] Test suite)
{
<BuildFrom>d__69 <BuildFrom>d__ = new <BuildFrom>d__69(-2);
<BuildFrom>d__.<>4__this = this;
<BuildFrom>d__.<>3__method = method;
<BuildFrom>d__.<>3__suite = suite;
return <BuildFrom>d__;
}
}
}