SetUpFixtureAttribute
Identifies a class as containing OneTimeSetUpAttribute or
OneTimeTearDownAttribute methods for all the test fixtures
under a given namespace.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace NUnit.Framework
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class SetUpFixtureAttribute : NUnitAttribute, IFixtureBuilder
{
[System.Runtime.CompilerServices.NullableContext(1)]
public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo)
{
SetUpFixture setUpFixture = new SetUpFixture(typeInfo);
if (setUpFixture.RunState != 0) {
string reason = null;
if (!IsValidFixtureType(typeInfo, ref reason))
setUpFixture.MakeInvalid(reason);
}
setUpFixture.ApplyAttributesToTest(typeInfo.Type);
return new TestSuite[1] {
setUpFixture
};
}
[System.Runtime.CompilerServices.NullableContext(1)]
private static bool IsValidFixtureType(ITypeInfo typeInfo, [System.Runtime.CompilerServices.Nullable(2)] [NotNullWhen(false)] ref string reason)
{
if (!typeInfo.IsStaticClass) {
if (typeInfo.IsAbstract) {
reason = typeInfo.FullName + " is an abstract class";
return false;
}
if (!typeInfo.HasConstructor(Array.Empty<Type>())) {
reason = typeInfo.FullName + " does not have a default constructor";
return false;
}
}
Type[] array = new Type[2] {
typeof(SetUpAttribute),
typeof(TearDownAttribute)
};
Type[] array2 = array;
foreach (Type type in array2) {
if (typeInfo.HasMethodWithAttribute(type)) {
reason = type.Name + " attribute not allowed in a SetUpFixture";
return false;
}
}
return true;
}
}
}