<PackageReference Include="NUnit" Version="4.3.0" />

DefaultTestAssemblyBuilder

DefaultTestAssemblyBuilder loads a single assembly and builds a TestSuite containing test fixtures present in the assembly.
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Builders; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; namespace NUnit.Framework.Api { [NullableContext(1)] [Nullable(0)] public class DefaultTestAssemblyBuilder : ITestAssemblyBuilder { private static readonly Logger Log = InternalTrace.GetLogger(typeof(DefaultTestAssemblyBuilder)); private readonly ISuiteBuilder _defaultSuiteBuilder; public DefaultTestAssemblyBuilder() { _defaultSuiteBuilder = new DefaultSuiteBuilder(); } public ITest Build(Assembly assembly, IDictionary<string, object> options) { Log.Debug("Loading {0} in AppDomain {1}", assembly.FullName, AppDomain.CurrentDomain.FriendlyName); string assemblyPath = AssemblyHelper.GetAssemblyPath(assembly); string assemblyNameOrPath = assemblyPath.Equals("<Unknown>") ? AssemblyHelper.GetAssemblyName(assembly).FullName : assemblyPath; return Build(assembly, assemblyNameOrPath, options); } public ITest Build(string assemblyNameOrPath, IDictionary<string, object> options) { Log.Debug("Loading {0} in AppDomain {1}", assemblyNameOrPath, AppDomain.CurrentDomain.FriendlyName); try { Assembly assembly = AssemblyHelper.Load(assemblyNameOrPath); return Build(assembly, assemblyNameOrPath, options); } catch (Exception exception) { TestSuite testSuite = new TestAssembly(assemblyNameOrPath); testSuite.MakeInvalid(ExceptionHelper.BuildMessage(exception, true)); return testSuite; } } private TestSuite Build(Assembly assembly, string assemblyNameOrPath, IDictionary<string, object> options) { try { if (options.TryGetValue("DefaultTestNamePattern", out object value)) TestNameGenerator.DefaultTestNamePattern = (string)value; if (options.TryGetValue("WorkDirectory", out object value2)) TestContext.DefaultWorkDirectory = (value2 as string); else TestContext.DefaultWorkDirectory = Directory.GetCurrentDirectory(); if (options.TryGetValue("TestParametersDictionary", out object value3)) { Dictionary<string, string> dictionary = value3 as Dictionary<string, string>; if (dictionary != null) { foreach (KeyValuePair<string, string> item in dictionary) { TestContext.Parameters.Add(item.Key, item.Value); } goto IL_0144; } } if (options.TryGetValue("TestParameters", out object value4)) { string text = (string)value4; if (!string.IsNullOrEmpty(text)) { foreach (string item2 in text.Tokenize(';', false)) { int num = item2.IndexOf('='); if (num > 0 && num < item2.Length - 1) { string name = item2.Substring(0, num); string value5 = item2.Substring(num + 1); TestContext.Parameters.Add(name, value5); } } } } goto IL_0144; IL_0144: PreFilter preFilter = new PreFilter(); if (options.TryGetValue("LOAD", out object value6)) { foreach (string item3 in (IList)value6) { preFilter.Add(item3); } } IList<Test> fixtures = GetFixtures(assembly, preFilter); return BuildTestAssembly(assembly, assemblyNameOrPath, fixtures); } catch (Exception exception) { TestSuite testSuite = new TestAssembly(assemblyNameOrPath); testSuite.MakeInvalid(ExceptionHelper.BuildMessage(exception, true)); return testSuite; } } private IList<Test> GetFixtures(Assembly assembly, PreFilter filter) { List<Test> list = new List<Test>(); Log.Debug("Examining assembly for test fixtures"); IList<Type> candidateFixtureTypes = GetCandidateFixtureTypes(assembly, filter); Log.Debug("Found {0} classes to examine", candidateFixtureTypes.Count); Stopwatch stopwatch = Stopwatch.StartNew(); int num = 0; foreach (Type item in candidateFixtureTypes) { TypeWrapper typeInfo = new TypeWrapper(item); if (_defaultSuiteBuilder.CanBuildFrom(typeInfo)) { Test test = _defaultSuiteBuilder.BuildFrom(typeInfo, filter); list.Add(test); num += test.TestCaseCount; } } Log.Debug("Found {0} fixtures with {1} test cases in {2}ms", list.Count, num, stopwatch.ElapsedMilliseconds); return list; } private IList<Type> GetCandidateFixtureTypes(Assembly assembly, PreFilter filter) { List<Type> list = new List<Type>(); Type[] types = assembly.GetTypes(); foreach (Type type in types) { if (filter.IsMatch(type)) list.Add(type); } return list; } private TestSuite BuildTestAssembly(Assembly assembly, string assemblyNameOrPath, IList<Test> fixtures) { TestSuite testSuite = new TestAssembly(assembly, assemblyNameOrPath); if (fixtures.Count == 0) testSuite.MakeInvalid("No test fixtures were found."); else { NamespaceTreeBuilder namespaceTreeBuilder = new NamespaceTreeBuilder(testSuite); namespaceTreeBuilder.Add(fixtures); testSuite = namespaceTreeBuilder.RootSuite; } testSuite.ApplyAttributesToTest(assembly); try { using (Process process = Process.GetCurrentProcess()) testSuite.Properties.Set("_PID", process.Id); } catch (PlatformNotSupportedException) { } testSuite.Properties.Set("_APPDOMAIN", AppDomain.CurrentDomain.FriendlyName); testSuite.Sort(); return testSuite; } } }