TestSuite
TestSuite represents a composite test, which contains other tests.
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
namespace NUnit.Framework.Internal
{
public class TestSuite : Test
{
private List<ITest> tests = new List<ITest>();
public override IList<ITest> Tests => tests;
public override int TestCaseCount {
get {
int num = 0;
foreach (Test test in Tests) {
num += test.TestCaseCount;
}
return num;
}
}
public override object[] Arguments { get; }
protected bool MaintainTestOrder { get; set; }
public MethodInfo[] OneTimeSetUpMethods { get; set; }
public MethodInfo[] OneTimeTearDownMethods { get; set; }
public override bool HasChildren => tests.Count > 0;
public override string XmlElementName => "test-suite";
public TestSuite(string name)
: base(name)
{
Arguments = new object[0];
OneTimeSetUpMethods = new MethodInfo[0];
OneTimeTearDownMethods = new MethodInfo[0];
}
public TestSuite(string parentSuiteName, string name)
: base(parentSuiteName, name)
{
Arguments = new object[0];
OneTimeSetUpMethods = new MethodInfo[0];
OneTimeTearDownMethods = new MethodInfo[0];
}
public TestSuite(ITypeInfo fixtureType, object[] arguments = null)
: base(fixtureType)
{
Arguments = (arguments ?? new object[0]);
OneTimeSetUpMethods = new MethodInfo[0];
OneTimeTearDownMethods = new MethodInfo[0];
}
public TestSuite(Type fixtureType)
: base(new TypeWrapper(fixtureType))
{
Arguments = new object[0];
OneTimeSetUpMethods = new MethodInfo[0];
OneTimeTearDownMethods = new MethodInfo[0];
}
public TestSuite(TestSuite suite, ITestFilter filter)
: base(suite.Name)
{
base.FullName = suite.FullName;
base.Method = suite.Method;
base.RunState = suite.RunState;
Fixture = suite.Fixture;
foreach (ITest test in suite.tests) {
if (filter.Pass(test)) {
if (test.IsSuite) {
TestSuite item = new TestSuite(test as TestSuite, filter) {
Parent = this
};
tests.Add(item);
} 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 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) {
foreach (Test test in Tests) {
test.AddToXml(tNode, recursive);
}
return tNode;
}
return tNode;
}
protected void CheckSetUpTearDownMethods(MethodInfo[] methods)
{
int num = 0;
MethodInfo methodInfo;
while (true) {
if (num >= methods.Length)
return;
methodInfo = methods[num];
if (methodInfo.IsAbstract || (!methodInfo.IsPublic && !methodInfo.IsFamily) || methodInfo.GetParameters().Length != 0 || ((object)methodInfo.ReturnType != typeof(void) && (object)methodInfo.ReturnType != typeof(Task)))
break;
num++;
}
MakeInvalid($"""{methodInfo.Name}");
}
}
}