PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/TestSuiteBuilder.cs

#
C# | 132 lines | 95 code | 20 blank | 17 comment | 16 complexity | 5ae06fffbc296bcdff7d0c4d1aa25e04 MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You
  3// may obtain a copy of the license as well as information regarding
  4// copyright ownership at http://nunit.org.
  5// ****************************************************************
  6
  7namespace NUnit.Core
  8{
  9	using NUnit.Core.Builders;
 10	using System.Collections;
 11	using System.Reflection;
 12
 13	/// <summary>
 14	/// This is the master suite builder for NUnit. It builds a test suite from
 15	/// one or more assemblies using a list of internal and external suite builders 
 16	/// to create fixtures from the qualified types in each assembly. It implements
 17	/// the ISuiteBuilder interface itself, allowing it to be used by other classes
 18	/// for queries and suite construction.
 19	/// </summary>D:\Dev\NUnit\nunit20\src\NUnitFramework\core\TestBuilderAttribute.cs
 20	public class TestSuiteBuilder
 21	{
 22		#region Instance Variables
 23
 24		private ArrayList builders = new ArrayList();
 25
 26		#endregion
 27
 28		#region Properties
 29		public IList Assemblies
 30		{
 31			get 
 32			{
 33				ArrayList assemblies = new ArrayList();
 34				foreach( TestAssemblyBuilder builder in builders )
 35					assemblies.Add( builder.Assembly );
 36				return assemblies; 
 37			}
 38		}
 39
 40		public IList AssemblyInfo
 41		{
 42			get
 43			{
 44				ArrayList info = new ArrayList();
 45				foreach( TestAssemblyBuilder builder in this.builders )
 46					info.Add( builder.AssemblyInfo );
 47
 48				return info;
 49			}
 50		}
 51		#endregion
 52
 53		#region Build Methods
 54		/// <summary>
 55		/// Build a suite based on a TestPackage
 56		/// </summary>
 57		/// <param name="package">The TestPackage</param>
 58		/// <returns>A TestSuite</returns>
 59		public TestSuite Build( TestPackage package )
 60		{
 61			bool autoNamespaceSuites = package.GetSetting( "AutoNamespaceSuites", true );
 62			bool mergeAssemblies = package.GetSetting( "MergeAssemblies", false );
 63            TestExecutionContext.CurrentContext.TestCaseTimeout = package.GetSetting("DefaultTimeout", 0);
 64
 65			if ( package.IsSingleAssembly )
 66				return BuildSingleAssembly( package );
 67			string targetAssemblyName = null;
 68			if( package.TestName != null && package.Assemblies.Contains( package.TestName ) )
 69			{
 70				targetAssemblyName = package.TestName;
 71				package.TestName = null;
 72			}
 73			
 74			TestSuite rootSuite = new ProjectRootSuite( package.FullName );
 75			NamespaceTreeBuilder namespaceTree = 
 76				new NamespaceTreeBuilder( rootSuite );
 77
 78			builders.Clear();
 79			foreach(string assemblyName in package.Assemblies)
 80			{
 81				if ( targetAssemblyName == null || targetAssemblyName == assemblyName )
 82				{
 83					TestAssemblyBuilder builder = new TestAssemblyBuilder();
 84					builders.Add( builder );
 85
 86					Test testAssembly =  builder.Build( assemblyName, package.TestName, autoNamespaceSuites && !mergeAssemblies );
 87
 88					if ( testAssembly != null )
 89					{
 90						if (!mergeAssemblies)
 91						{
 92							rootSuite.Add(testAssembly);
 93						}
 94						else if (autoNamespaceSuites)
 95						{
 96							namespaceTree.Add(testAssembly.Tests);
 97							rootSuite = namespaceTree.RootSuite;
 98						}
 99						else
100						{
101							foreach (Test test in testAssembly.Tests)
102								rootSuite.Add(test);
103						}
104					}
105				}
106			}
107
108            ProviderCache.Clear();
109            
110            if (rootSuite.Tests.Count == 0)
111				return null;
112
113			return rootSuite;
114		}
115
116		private TestSuite BuildSingleAssembly( TestPackage package )
117		{
118			TestAssemblyBuilder builder = new TestAssemblyBuilder();
119			builders.Clear();
120			builders.Add( builder );
121
122			TestSuite suite = (TestSuite)builder.Build( 
123				package.FullName, 
124				package.TestName, package.GetSetting( "AutoNamespaceSuites", true ) );
125
126            ProviderCache.Clear();
127
128            return suite;
129		}
130		#endregion
131	}
132}