Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NUnitTestRunner.cs « NUnitRunner « MonoDevelop.UnitTesting.NUnit « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8059c6d934dcd2f5b0806cd24d035985e139a4f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// ExternalTestRunner.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.Linq;
using System.Reflection;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

using NUnit.Core;
using NUnit.Framework;
using NUnit.Core.Filters;

namespace MonoDevelop.UnitTesting.NUnit.External
{
	public class NUnitTestRunner: MarshalByRefObject
	{
		public NUnitTestRunner ()
		{
			PreloadAssemblies ();
		}

		public static void PreloadAssemblies ()
		{
			// Note: We need to load all nunit.*.dll assemblies before we do *anything* else in this class
			// This is to ensure that we always load the assemblies from the monodevelop directory and not
			// from the directory of the assembly under test. For example we wnat to load
			// /Applications/MonoDevelop/lib/Addins/nunit.framework.dll and not /user/app/foo/bin/debug/nunit.framework.dll

			// Force the loading of the NUnit.Framework assembly.
			// It's needed since that dll is not located in the test dll directory.
			var path = Path.GetDirectoryName (typeof(NUnitTestRunner).Assembly.Location);
			string nunitPath = Path.Combine (path, "nunit.framework.dll");
			string nunitCorePath = Path.Combine (path, "nunit.core.dll");
			string nunitCoreInterfacesPath = Path.Combine (path, "nunit.core.interfaces.dll");

			Assembly.LoadFrom (nunitCoreInterfacesPath);
			Assembly.LoadFrom (nunitCorePath);
			Assembly.LoadFrom (nunitPath);
		}

		public void Initialize ()
		{
			// Initialize ExtensionHost if not already done
			if ( !CoreExtensions.Host.Initialized )
				CoreExtensions.Host.InitializeService();
		}
		
		public TestResult Run (EventListener listener, string[] nameFilter, string path, string suiteName, string[] supportAssemblies, string testRunnerType, string testRunnerAssembly)
		{
			InitSupportAssemblies (supportAssemblies);

			ITestFilter filter = TestFilter.Empty;
			if (nameFilter != null && nameFilter.Length > 0)
				filter = new TestNameFilter (nameFilter);

			TestRunner tr;
			if (!string.IsNullOrEmpty (testRunnerType)) {
				Type runnerType;
				if (string.IsNullOrEmpty (testRunnerAssembly))
					runnerType = Type.GetType (testRunnerType, true);
				else {
					var asm = Assembly.LoadFrom (testRunnerAssembly);
					runnerType = asm.GetType (testRunnerType);
				}
				tr = (TestRunner)Activator.CreateInstance (runnerType);
			} else
				tr = new RemoteTestRunner ();

			TestPackage package = new TestPackage (path);
			if (!string.IsNullOrEmpty (suiteName))
				package.TestName = suiteName;
			tr.Load (package);
			return tr.Run (listener, filter, false, LoggingThreshold.All);
		}
		
		public NunitTestInfo GetTestInfo (string path, string[] supportAssemblies)
		{
			InitSupportAssemblies (supportAssemblies);
			TestSuite rootTS = new TestSuiteBuilder ().Build (new TestPackage (path));
			return BuildTestInfo (rootTS);
		}
		
		internal NunitTestInfo BuildTestInfo (Test test)
		{
			NunitTestInfo ti = new NunitTestInfo ();
			// The name of inherited tests include the base class name as prefix.
			// That prefix has to be removed
			string tname = test.TestName.Name;
			// Find the last index of the dot character that is not a part of the test parameters
			// Parameterized methods can contain '.' as class name & they don't seem to prefix base class name, so it's safe to skip them
			if (!(test.Parent is ParameterizedMethodSuite)) {
				int j = tname.IndexOf ('(');
				int i = tname.LastIndexOf ('.', (j == -1) ? (tname.Length - 1) : j);
				if (i != -1)
					tname = tname.Substring (i + 1);
			}

			if (test.FixtureType != null) {
				ti.FixtureTypeName = test.FixtureType.Name;
				ti.FixtureTypeNamespace = test.FixtureType.Namespace;
			} else if (test.TestType == "ParameterizedTest") {
				ti.FixtureTypeName = test.Parent.FixtureType.Name;
				ti.FixtureTypeNamespace = test.Parent.FixtureType.Namespace;
			}
			ti.Name = tname;
			ti.TestId = test.TestName.FullName;

			// Trim short name from end of full name to get the path
			string testNameWithDelimiter = "." + tname;
			if (test.TestName.FullName.EndsWith (testNameWithDelimiter)) {
				int pathLength = test.TestName.FullName.Length - testNameWithDelimiter.Length;
				ti.PathName = test.TestName.FullName.Substring(0, pathLength );
			}
			else
				ti.PathName = null;
			
			if (test.Tests != null && test.Tests.Count > 0) {
				ti.Tests = new NunitTestInfo [test.Tests.Count];
				for (int n=0; n<test.Tests.Count; n++)
					ti.Tests [n] = BuildTestInfo ((Test)test.Tests [n]);
			}
			ti.IsExplicit = test.RunState == RunState.Explicit;
			return ti;
		}
		
		void InitSupportAssemblies (string[] supportAssemblies)
		{
			// Preload support assemblies (they may not be in the test assembly directory nor in the gac)
			foreach (string asm in supportAssemblies)
				Assembly.LoadFrom (asm);
		}
		
		public override object InitializeLifetimeService ()
		{
			return null;
		}
	}
		
	[Serializable]
	public class TestNameFilter: ITestFilter
	{
		string[] names;
		
		public TestNameFilter (params string[] names)
		{
			this.names = names;
		}
		
		#region ITestFilter implementation 
		
		public bool Pass (ITest test)
		{
			if (!test.IsSuite && names.Any (n => test.TestName.FullName == n))
				return true;
			if (test.Tests != null) {
				foreach (ITest ct in test.Tests) {
					if (Pass (ct))
						return true;
				}
			}
			return false;
		}
		
		public bool Match (ITest test)
		{
			return Pass (test);
		}
		
		public bool IsEmpty {
			get {
				return false;
			}
		}
		
		#endregion 
	}
}