TestSuite
TestSuite represents a composite test, which contains other tests.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal
{
[NullableContext(1)]
[Nullable(0)]
public class TestSuite : Test
{
private readonly List<ITest> _tests = new List<ITest>();
public override IList<ITest> Tests => _tests;
public override int TestCaseCount {
get {
int num = 0;
IList<ITest> tests = Tests;
for (int i = 0; i < tests.Count; i++) {
num += tests[i].TestCaseCount;
}
return num;
}
}
[Nullable(new byte[] {
1,
2
})]
[field: Nullable(new byte[] {
1,
2
})]
public override object[] Arguments {
[return: Nullable(new byte[] {
1,
2
})]
get;
}
protected bool MaintainTestOrder { get; set; }
public IMethodInfo[] OneTimeSetUpMethods { get; set; }
public IMethodInfo[] OneTimeTearDownMethods { get; set; }
public override bool HasChildren => _tests.Count > 0;
public override string XmlElementName => "test-suite";
public TestSuite(string name)
: base(name)
{
Arguments = TestParameters.NoArguments;
OneTimeSetUpMethods = Array.Empty<IMethodInfo>();
OneTimeTearDownMethods = Array.Empty<IMethodInfo>();
}
public TestSuite([Nullable(2)] string parentSuiteName, string name)
: base(parentSuiteName, name)
{
Arguments = TestParameters.NoArguments;
OneTimeSetUpMethods = Array.Empty<IMethodInfo>();
OneTimeTearDownMethods = Array.Empty<IMethodInfo>();
}
public TestSuite(ITypeInfo fixtureType, [Nullable(2)] object[] arguments = null)
: base(fixtureType)
{
Arguments = (arguments ?? TestParameters.NoArguments);
OneTimeSetUpMethods = Array.Empty<IMethodInfo>();
OneTimeTearDownMethods = Array.Empty<IMethodInfo>();
}
public TestSuite(Type fixtureType)
: base(new TypeWrapper(fixtureType))
{
Arguments = TestParameters.NoArguments;
OneTimeSetUpMethods = Array.Empty<IMethodInfo>();
OneTimeTearDownMethods = Array.Empty<IMethodInfo>();
}
public TestSuite(TestSuite suite, ITestFilter filter)
: this(suite.Name)
{
base.FullName = suite.FullName;
base.Method = suite.Method;
base.RunState = suite.RunState;
Fixture = suite.Fixture;
foreach (string key in suite.Properties.Keys) {
foreach (object item in suite.Properties[key]) {
base.Properties.Add(key, item);
}
}
foreach (ITest test in suite._tests) {
if (filter.Pass(test)) {
if (test.IsSuite) {
TestSuite testSuite = ((TestSuite)test).Copy(filter);
testSuite.Parent = this;
_tests.Add(testSuite);
} else
_tests.Add(test);
}
}
}
public void Sort()
{
if (!MaintainTestOrder) {
_tests.Sort();
foreach (Test test in Tests) {
(test as TestSuite)?.Sort();
}
}
}
public void Add(Test test)
{
test.Parent = this;
_tests.Add(test);
}
public virtual TestSuite Copy(ITestFilter filter)
{
return new TestSuite(this, filter);
}
public void ApplyAttributesToTestSuite(Type type)
{
foreach (Type item in GetNestedTypes(type).Reverse()) {
ApplyAttributesToTestSuite((ICustomAttributeProvider)item);
}
}
private void ApplyAttributesToTestSuite(ICustomAttributeProvider provider)
{
IApplyToTestSuite[] attributes = provider.GetAttributes<IApplyToTestSuite>(true);
ApplyAttributesToTestSuite(attributes);
}
private void ApplyAttributesToTestSuite(IEnumerable<IApplyToTestSuite> attributes)
{
foreach (IApplyToTestSuite attribute in attributes) {
attribute.ApplyToTestSuite(this);
}
}
public override TestResult MakeTestResult()
{
return new TestSuiteResult(this);
}
public override TNode AddToXml(TNode parentNode, bool recursive)
{
TNode tNode = parentNode.AddElement("test-suite");
tNode.AddAttribute("type", TestType);
PopulateTestNode(tNode, recursive);
tNode.AddAttribute("testcasecount", TestCaseCount.ToString());
if (recursive) {
IList<ITest> tests = Tests;
for (int i = 0; i < tests.Count; i++) {
ITest test = tests[i];
test.AddToXml(tNode, recursive);
}
}
return tNode;
}
protected void CheckSetUpTearDownMethods(IMethodInfo[] methods)
{
foreach (IMethodInfo methodInfo in methods) {
MethodInfoCache.TestMethodMetadata testMethodMetadata = MethodInfoCache.Get(methodInfo);
if (methodInfo.IsAbstract)
MakeInvalid("An abstract SetUp and TearDown methods cannot be run: " + methodInfo.Name);
else if (!methodInfo.IsPublic && !methodInfo.MethodInfo.IsFamily) {
MakeInvalid("SetUp and TearDown methods must be public or protected: " + methodInfo.Name);
} else if (testMethodMetadata.Parameters.Length != 0) {
MakeInvalid("SetUp and TearDown methods must not have parameters: " + methodInfo.Name);
} else if (testMethodMetadata.IsAsyncOperation) {
if (methodInfo.ReturnType.Type == typeof(void))
MakeInvalid("SetUp and TearDown methods must not be async void: " + methodInfo.Name);
else if (!Reflect.IsVoidOrUnit(AwaitAdapter.GetResultType(methodInfo.ReturnType.Type))) {
MakeInvalid("SetUp and TearDown methods must return void or an awaitable type with a void result: " + methodInfo.Name);
}
} else if (!testMethodMetadata.IsVoidOrUnit) {
MakeInvalid("SetUp and TearDown methods must return void or an awaitable type with a void result: " + methodInfo.Name);
}
}
}
}
}