<PackageReference Include="NUnit" Version="3.0.0-alpha-5" />

TestCaseAttribute

TestCaseAttribute is used to mark parameterized test cases and provide them with their arguments.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Builders; using System; using System.Collections.Generic; using System.Globalization; using System.Reflection; namespace NUnit.Framework { [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)] public class TestCaseAttribute : TestCaseBuilderAttribute, ITestBuilder, ITestCaseData, IImplyFixture { private object _expectedResult; private Type _typeOf; private IPropertyBag _properties; private readonly PlatformHelper _platformHelper = new PlatformHelper(); public object[] Arguments { get; set; } 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; } set { Properties.Set("Description", value); } } public string Author { get { return Properties.Get("Author") as string; } set { Properties.Set("Author", value); } } public Type TestOf { get { return _typeOf; } set { _typeOf = value; Properties.Set("TestOf", value.FullName); } } public string TestName { get; set; } public string Ignore { get { return IgnoreReason; } set { IgnoreReason = value; } } public bool Explicit { get { return RunState == RunState.Explicit; } set { RunState = ((!value) ? RunState.Runnable : RunState.Explicit); } } public RunState RunState { get; set; } public string Reason { get { return Properties.Get("_SKIPREASON") as string; } set { Properties.Set("_SKIPREASON", value); } } public string IgnoreReason { get { return Reason; } set { RunState = RunState.Ignored; Reason = value; } } public string IncludePlatform { get; set; } public string ExcludePlatform { get; set; } public string Category { get { return Properties.Get("Category") as string; } set { string[] array = value.Split(new char[1] { ',' }); foreach (string value2 in array) { Properties.Add("Category", value2); } } } public IPropertyBag Properties { get { if (_properties == null) _properties = new PropertyBag(); return _properties; } } public TestCaseAttribute(params object[] arguments) { RunState = RunState.Runnable; if (arguments == null) { object[] array2 = Arguments = new object[1]; } else Arguments = arguments; } public TestCaseAttribute(object arg) { RunState = RunState.Runnable; Arguments = new object[1] { arg }; } public TestCaseAttribute(object arg1, object arg2) { RunState = RunState.Runnable; Arguments = new object[2] { arg1, arg2 }; } public TestCaseAttribute(object arg1, object arg2, object arg3) { RunState = RunState.Runnable; Arguments = new object[3] { arg1, arg2, arg3 }; } private ParameterSet GetParametersForTestCase(MethodInfo method) { try { ParameterInfo[] parameters = method.GetParameters(); int num = parameters.Length; int num2 = Arguments.Length; ParameterSet parameterSet = new ParameterSet(this); if (num > 0 && num2 >= num - 1) { ParameterInfo parameterInfo = parameters[num - 1]; Type parameterType = parameterInfo.ParameterType; Type elementType = parameterType.GetElementType(); if (parameterType.IsArray && parameterInfo.IsDefined(typeof(ParamArrayAttribute), false)) { if (num2 == num) { Type type = parameterSet.Arguments[num2 - 1].GetType(); if (!parameterType.IsAssignableFrom(type)) { Array array = Array.CreateInstance(elementType, 1); array.SetValue(parameterSet.Arguments[num2 - 1], 0); parameterSet.Arguments[num2 - 1] = array; } } else { object[] array2 = new object[num]; for (int i = 0; i < num && i < num2; i++) { array2[i] = parameterSet.Arguments[i]; } int num3 = num2 - num + 1; Array array3 = Array.CreateInstance(elementType, num3); for (int j = 0; j < num3; j++) { array3.SetValue(parameterSet.Arguments[num + j - 1], j); } array2[num - 1] = array3; parameterSet.Arguments = array2; num2 = num; } } } if (num == 1 && method.GetParameters()[0].ParameterType == typeof(object[]) && (num2 > 1 || (num2 == 1 && parameterSet.Arguments[0].GetType() != typeof(object[])))) parameterSet.Arguments = new object[1] { parameterSet.Arguments }; if (num2 != num) return parameterSet; PerformSpecialConversions(parameterSet.Arguments, parameters); return parameterSet; } catch (Exception exception) { return new ParameterSet(exception); } } private static void PerformSpecialConversions(object[] arglist, ParameterInfo[] parameters) { for (int i = 0; i < arglist.Length; i++) { object obj = arglist[i]; Type parameterType = parameters[i].ParameterType; if (obj != null) { if (obj is SpecialValue && (SpecialValue)obj == SpecialValue.Null) arglist[i] = null; else if (!parameterType.IsAssignableFrom(obj.GetType())) { if (obj is DBNull) arglist[i] = null; else { bool flag = false; if (parameterType == typeof(short) || parameterType == typeof(byte) || parameterType == typeof(sbyte)) flag = (obj is int); else if (parameterType == typeof(decimal)) { flag = (obj is double || obj is string || obj is int); } else if (parameterType == typeof(DateTime) || parameterType == typeof(TimeSpan)) { flag = (obj is string); } if (flag) arglist[i] = Convert.ChangeType(obj, parameterType, CultureInfo.InvariantCulture); } } } } } public IEnumerable<TestMethod> BuildFrom(MethodInfo method, Test suite) { TestMethod testMethod = new NUnitTestCaseBuilder().BuildTestMethod(method, suite, GetParametersForTestCase(method)); if (testMethod.RunState != 0 && testMethod.RunState != RunState.Ignored && !_platformHelper.IsPlatformSupported(this)) { testMethod.RunState = RunState.Skipped; testMethod.Properties.Add("_SKIPREASON", _platformHelper.Reason); } return new TestMethod[1] { testMethod }; } } }