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

AttributeExistsConstraint

AttributeExistsConstraint tests for the presence of a specified attribute on a Type.
using System; using System.Reflection; namespace NUnit.Framework.Constraints { public class AttributeExistsConstraint : Constraint { private Type expectedType; public override string Description => "type with attribute " + MsgUtils.FormatValue(expectedType); public AttributeExistsConstraint(Type type) : base(type) { expectedType = type; if (!typeof(Attribute).IsAssignableFrom(expectedType)) throw new ArgumentException($"""{expectedType}""", "type"); } public override ConstraintResult ApplyTo<TActual>(TActual actual) { ICustomAttributeProvider customAttributeProvider = actual as ICustomAttributeProvider; if (customAttributeProvider == null) throw new ArgumentException($"""{actual}""", "actual"); ConstraintResult constraintResult = new ConstraintResult(this, actual); constraintResult.Status = ((customAttributeProvider.GetCustomAttributes(expectedType, true).Length > 0) ? ConstraintStatus.Success : ConstraintStatus.Failure); return constraintResult; } } }