From af15be52ef27d091f5443d80d4d1ac8bc4f9496f Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Thu, 7 Apr 2016 16:20:48 -0700 Subject: [PATCH 1/2] Refactor in preparation for issue 47 --- src/NUnitTestAdapter/NUnit3TestAdapter.csproj | 1 + src/NUnitTestAdapter/NUnit3TestDiscoverer.cs | 27 ++-- src/NUnitTestAdapter/NUnit3TestExecutor.cs | 127 +++++++++--------- src/NUnitTestAdapter/NUnitEventListener.cs | 26 ++-- src/NUnitTestAdapter/NUnitTestAdapter.cs | 36 ++--- .../NUnitTestFilterBuilder.cs | 40 ++++++ src/NUnitTestAdapter/TFSTestFilter.cs | 12 +- src/NUnitTestAdapter/TestConverter.cs | 10 +- src/NUnitTestAdapter/TestLogger.cs | 62 ++++----- 9 files changed, 182 insertions(+), 159 deletions(-) create mode 100644 src/NUnitTestAdapter/NUnitTestFilterBuilder.cs diff --git a/src/NUnitTestAdapter/NUnit3TestAdapter.csproj b/src/NUnitTestAdapter/NUnit3TestAdapter.csproj index 9ea41f75..25cb5acb 100644 --- a/src/NUnitTestAdapter/NUnit3TestAdapter.csproj +++ b/src/NUnitTestAdapter/NUnit3TestAdapter.csproj @@ -83,6 +83,7 @@ + Code diff --git a/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs b/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs index f444d67b..0d7c2433 100644 --- a/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs +++ b/src/NUnitTestAdapter/NUnit3TestDiscoverer.cs @@ -32,14 +32,14 @@ public void DiscoverTests(IEnumerable sources, IDiscoveryContext discove #endif Initialize(discoveryContext, messageLogger); - Info("discovering tests", "started"); + TestLog.Info(string.Format("NUnit Adapter {0}: Test discovery starting", AdapterVersion)); // Ensure any channels registered by other adapters are unregistered CleanUpRegisteredChannels(); foreach (string sourceAssembly in sources) { - TestLog.SendDebugMessage("Processing " + sourceAssembly); + TestLog.Debug("Processing " + sourceAssembly); ITestRunner runner = null; @@ -60,44 +60,44 @@ public void DiscoverTests(IEnumerable sources, IDiscoveryContext discove using (var testConverter = new TestConverter(TestLog, sourceAssembly)) { int cases = ProcessTestCases(topNode, discoverySink, testConverter); - TestLog.SendDebugMessage(string.Format("Discovered {0} test cases", cases)); + TestLog.Debug(string.Format("Discovered {0} test cases", cases)); } } else { var msgNode = loadResult.SelectSingleNode("properties/property[@name='_SKIPREASON']"); if (msgNode != null && (new[] { "contains no tests", "Has no TestFixtures" }).Any(msgNode.GetAttribute("value").Contains)) - TestLog.SendInformationalMessage("Assembly contains no NUnit 3.0 tests: " + sourceAssembly); + TestLog.Info("Assembly contains no NUnit 3.0 tests: " + sourceAssembly); else - TestLog.NUnitLoadError(sourceAssembly); + TestLog.Info("NUnit failed to load " + sourceAssembly); } } catch (BadImageFormatException) { // we skip the native c++ binaries that we don't support. - TestLog.AssemblyNotSupportedWarning(sourceAssembly); + TestLog.Warning("Assembly not supported: " + sourceAssembly); } catch (FileNotFoundException ex) { // Either the NUnit framework was not referenced by the test assembly // or some other error occured. Not a problem if not an NUnit assembly. - TestLog.DependentAssemblyNotFoundWarning(ex.FileName, sourceAssembly); + TestLog.Warning("Dependent Assembly " + ex.FileName + " of " + sourceAssembly + " not found. Can be ignored if not a NUnit project."); } catch (FileLoadException ex) { // Attempts to load an invalid assembly, or an assembly with missing dependencies - TestLog.LoadingAssemblyFailedWarning(ex.FileName, sourceAssembly); + TestLog.Warning("Assembly " + ex.FileName + " loaded through " + sourceAssembly + " failed. Assembly is ignored. Correct deployment of dependencies if this is an error."); } catch (TypeLoadException ex) { if (ex.TypeName == "NUnit.Framework.Api.FrameworkController") - TestLog.SendWarningMessage(" Skipping NUnit 2.x test assembly"); + TestLog.Warning(" Skipping NUnit 2.x test assembly"); else - TestLog.SendErrorMessage("Exception thrown discovering tests in " + sourceAssembly, ex); + TestLog.Error("Exception thrown discovering tests in " + sourceAssembly, ex); } catch (Exception ex) { - TestLog.SendErrorMessage("Exception thrown discovering tests in " + sourceAssembly, ex); + TestLog.Error("Exception thrown discovering tests in " + sourceAssembly, ex); } finally { @@ -109,7 +109,8 @@ public void DiscoverTests(IEnumerable sources, IDiscoveryContext discove } } - Info("discovering test", "finished"); + TestLog.Info(string.Format("NUnit Adapter {0}: Test discovery complete", AdapterVersion)); + Unload(); } @@ -135,7 +136,7 @@ private int ProcessTestCases(XmlNode topNode, ITestCaseDiscoverySink discoverySi } catch (Exception ex) { - TestLog.SendErrorMessage("Exception converting " + testNode.GetAttribute("fullname"), ex); + TestLog.Error("Exception converting " + testNode.GetAttribute("fullname"), ex); } } diff --git a/src/NUnitTestAdapter/NUnit3TestExecutor.cs b/src/NUnitTestAdapter/NUnit3TestExecutor.cs index 6f1443f9..845454c9 100644 --- a/src/NUnitTestAdapter/NUnit3TestExecutor.cs +++ b/src/NUnitTestAdapter/NUnit3TestExecutor.cs @@ -23,12 +23,19 @@ namespace NUnit.VisualStudio.TestAdapter [ExtensionUri(ExecutorUri)] public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDisposable { - // TFS Filter in effect - may be empty - private TfsTestFilter _tfsFilter; // Fields related to the currently executing assembly - private ITestRunner _testRunner; - private TestFilter _nunitFilter = TestFilter.Empty; + private ITestRunner _activeRunner; + + #region Properties + + // Properties set when either of the RunTests methods is called + public IRunContext RunContext { get; private set; } + public IFrameworkHandle FrameworkHandle { get; private set; } + private TfsTestFilter TfsFilter { get; set; } + private NUnitTestFilterBuilder FilterBuilder { get; set; } + + #endregion #region ITestExecutor Implementation @@ -50,35 +57,26 @@ public void RunTests(IEnumerable sources, IRunContext runContext, IFrame try { - _tfsFilter = new TfsTestFilter(runContext); - TestLog.SendDebugMessage("Keepalive:" + runContext.KeepAlive); - var enableShutdown = (Settings.UseVsKeepEngineRunning) ? !runContext.KeepAlive : true; - if (!_tfsFilter.HasTfsFilterValue) - { - if (!(enableShutdown && !runContext.KeepAlive)) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012 - frameworkHandle.EnableShutdownAfterTestRun = enableShutdown; - } - foreach (var source in sources) { var assemblyName = source; if (!Path.IsPathRooted(assemblyName)) assemblyName = Path.Combine(Environment.CurrentDirectory, assemblyName); - TestLog.SendInformationalMessage("Running all tests in " + assemblyName); + TestLog.Info("Running all tests in " + assemblyName); - RunAssembly(assemblyName, frameworkHandle); + RunAssembly(assemblyName, TestFilter.Empty); } } catch (Exception ex) { if (ex is TargetInvocationException) ex = ex.InnerException; - TestLog.SendErrorMessage("Exception thrown executing tests", ex); + TestLog.Error("Exception thrown executing tests", ex); } finally { - Info("executing tests", "finished"); + TestLog.Info(string.Format("NUnit Adapter {0}: Test execution complete", AdapterVersion)); Unload(); } @@ -98,32 +96,28 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame #endif Initialize(runContext, frameworkHandle); - var enableShutdown = (Settings.UseVsKeepEngineRunning) ? !runContext.KeepAlive : true; - frameworkHandle.EnableShutdownAfterTestRun = enableShutdown; - Debug("executing tests", "EnableShutdown set to " + enableShutdown); - var assemblyGroups = tests.GroupBy(tc => tc.Source); foreach (var assemblyGroup in assemblyGroups) { var assemblyName = assemblyGroup.Key; if (Debugger.IsAttached) - TestLog.SendInformationalMessage("Debugging selected tests in " + assemblyName); + TestLog.Info("Debugging selected tests in " + assemblyName); else - TestLog.SendInformationalMessage("Running selected tests in " + assemblyName); + TestLog.Info("Running selected tests in " + assemblyName); - _nunitFilter = MakeTestFilter(assemblyGroup); + var filter = FilterBuilder.MakeTestFilter(assemblyGroup); - RunAssembly(assemblyName, frameworkHandle); + RunAssembly(assemblyName, filter); } - Info("executing tests", "finished"); + TestLog.Info(string.Format("NUnit Adapter {0}: Test execution complete", AdapterVersion)); Unload(); } void ITestExecutor.Cancel() { - if (_testRunner != null) - _testRunner.StopRun(true); + if (_activeRunner != null) + _activeRunner.StopRun(true); } #endregion @@ -139,37 +133,56 @@ public void Dispose() #region Helper Methods - // The TestExecutor is constructed using the default constructor. - // We don't have any info to initialize it until one of the - // ITestExecutor methods is called. - protected override void Initialize(IDiscoveryContext context, IMessageLogger messageLogger) + public void Initialize(IRunContext runContext, IFrameworkHandle frameworkHandle) { - base.Initialize(context, messageLogger); + base.Initialize(runContext, frameworkHandle); + + TestLog.Info(string.Format("NUnit Adapter {0}: Test execution started", AdapterVersion)); - Info("executing tests", "started"); + RunContext = runContext; + FrameworkHandle = frameworkHandle; + TfsFilter = new TfsTestFilter(runContext); + FilterBuilder = new NUnitTestFilterBuilder(TestEngine.Services.GetService()); // Ensure any channels registered by other adapters are unregistered CleanUpRegisteredChannels(); + + TestLog.Debug("Keepalive: " + runContext.KeepAlive); + TestLog.Debug("UseVsKeepEngineRunning: " + Settings.UseVsKeepEngineRunning); + + bool enableShutdown = true; + if (Settings.UseVsKeepEngineRunning ) + { + enableShutdown = !runContext.KeepAlive; + } + + if (TfsFilter.IsEmpty) + { + if (!(enableShutdown && !runContext.KeepAlive)) // Otherwise causes exception when run as commandline, illegal to enableshutdown when Keepalive is false, might be only VS2012 + frameworkHandle.EnableShutdownAfterTestRun = enableShutdown; + } + + TestLog.Debug("EnableShutdown: " + enableShutdown.ToString()); } - private void RunAssembly(string assemblyName, IFrameworkHandle frameworkHandle) + private void RunAssembly(string assemblyName, TestFilter filter) { #if LAUNCHDEBUGGER if (!Debugger.IsAttached) Debugger.Launch(); #endif - _testRunner = GetRunnerFor(assemblyName); + _activeRunner = GetRunnerFor(assemblyName); try { - var loadResult = _testRunner.Explore(TestFilter.Empty); + var loadResult = _activeRunner.Explore(TestFilter.Empty); if (loadResult.Name == "test-run") loadResult = loadResult.FirstChild; if (loadResult.GetAttribute("runstate") == "Runnable") { - TestLog.SendInformationalMessage(string.Format("Loading tests from {0}", assemblyName)); + TestLog.Info(string.Format("Loading tests from {0}", assemblyName)); var nunitTestCases = loadResult.SelectNodes("//test-case"); @@ -184,62 +197,48 @@ private void RunAssembly(string assemblyName, IFrameworkHandle frameworkHandle) loadedTestCases.Add(testConverter.ConvertTestCase(testNode)); // If we have a TFS Filter, convert it to an nunit filter - if (_tfsFilter != null && _tfsFilter.HasTfsFilterValue) + if (TfsFilter != null && !TfsFilter.IsEmpty) { - var filteredTestCases = _tfsFilter.CheckFilter(loadedTestCases); - var testCases = filteredTestCases as TestCase[] ?? filteredTestCases.ToArray(); - TestLog.SendInformationalMessage(string.Format("TFS Filter detected: LoadedTestCases {0}, Filterered Test Cases {1}", loadedTestCases.Count, testCases.Count())); - _nunitFilter = MakeTestFilter(testCases); + // NOTE This overwrites filter used in call + filter = FilterBuilder.ConvertTfsFilterToNUnitFilter(TfsFilter, loadedTestCases); } - using (var listener = new NUnitEventListener(frameworkHandle, testConverter)) + using (var listener = new NUnitEventListener(FrameworkHandle, testConverter)) { try { - _testRunner.Run(listener, _nunitFilter); + _activeRunner.Run(listener, filter); } catch (NullReferenceException) { // this happens during the run when CancelRun is called. - TestLog.SendDebugMessage("Nullref caught"); + TestLog.Debug("Nullref caught"); } } } } else - TestLog.NUnitLoadError(assemblyName); + TestLog.Info("NUnit failed to load " + assemblyName); } catch (BadImageFormatException) { // we skip the native c++ binaries that we don't support. - TestLog.AssemblyNotSupportedWarning(assemblyName); + TestLog.Warning("Assembly not supported: " + assemblyName); } catch (System.IO.FileNotFoundException ex) { // Probably from the GetExportedTypes in NUnit.core, attempting to find an assembly, not a problem if it is not NUnit here - TestLog.DependentAssemblyNotFoundWarning(ex.FileName, assemblyName); + TestLog.Warning("Dependent Assembly " + ex.FileName + " of " + assemblyName + " not found. Can be ignored if not a NUnit project."); } catch (Exception ex) { if (ex is TargetInvocationException) ex = ex.InnerException; - TestLog.SendErrorMessage("Exception thrown executing tests in " + assemblyName, ex); + TestLog.Error("Exception thrown executing tests in " + assemblyName, ex); } - _testRunner.Dispose(); - } - - private TestFilter MakeTestFilter(IEnumerable testCases) - { - ITestFilterService filterService = TestEngine.Services.GetService(); - if (filterService == null) - throw new NUnitEngineException("TestFilterService is not available. Engine in use is incorrect version."); - - ITestFilterBuilder filterBuilder = filterService.GetTestFilterBuilder(); - - foreach (TestCase testCase in testCases) - filterBuilder.AddTest(testCase.FullyQualifiedName); - return filterBuilder.GetFilter(); + _activeRunner.Dispose(); + _activeRunner = null; } #endregion diff --git a/src/NUnitTestAdapter/NUnitEventListener.cs b/src/NUnitTestAdapter/NUnitEventListener.cs index 89a16d7a..30621bac 100644 --- a/src/NUnitTestAdapter/NUnitEventListener.cs +++ b/src/NUnitTestAdapter/NUnitEventListener.cs @@ -19,8 +19,8 @@ namespace NUnit.VisualStudio.TestAdapter /// public class NUnitEventListener : MarshalByRefObject, ITestEventListener, IDisposable // Public for testing { - private readonly ITestExecutionRecorder testLog; - private readonly TestConverter testConverter; + private readonly ITestExecutionRecorder _recorder; + private readonly TestConverter _testConverter; public override object InitializeLifetimeService() { @@ -31,10 +31,10 @@ public override object InitializeLifetimeService() return null; } - public NUnitEventListener(ITestExecutionRecorder testLog, TestConverter testConverter) + public NUnitEventListener(ITestExecutionRecorder recorder, TestConverter testConverter) { - this.testLog = testLog; - this.testConverter = testConverter; + _recorder = recorder; + _testConverter = testConverter; } #region ITestEventListener @@ -89,18 +89,18 @@ protected virtual void Dispose(bool disposing) public void TestStarted(XmlNode testNode) { - TestCase ourCase = testConverter.GetCachedTestCase(testNode.GetAttribute("id")); + TestCase ourCase = _testConverter.GetCachedTestCase(testNode.GetAttribute("id")); // Simply ignore any TestCase not found in the cache if (ourCase != null) - testLog.RecordStart(ourCase); + _recorder.RecordStart(ourCase); } public void TestFinished(XmlNode resultNode) { - TestResult ourResult = testConverter.ConvertTestResult(resultNode); - this.testLog.RecordEnd(ourResult.TestCase, ourResult.Outcome); - this.testLog.RecordResult(ourResult); + TestResult ourResult = _testConverter.ConvertTestResult(resultNode); + _recorder.RecordEnd(ourResult.TestCase, ourResult.Outcome); + _recorder.RecordResult(ourResult); } public void SuiteFinished(XmlNode resultNode) @@ -113,17 +113,17 @@ public void SuiteFinished(XmlNode resultNode) { if (site == "SetUp" || site == "TearDown") { - testLog.SendMessage( + _recorder.SendMessage( TestMessageLevel.Error, string.Format("{0} failed for test fixture {1}", site, resultNode.GetAttribute("fullname"))); var messageNode = resultNode.SelectSingleNode("failure/message"); if (messageNode != null) - testLog.SendMessage(TestMessageLevel.Error, messageNode.InnerText); + _recorder.SendMessage(TestMessageLevel.Error, messageNode.InnerText); var stackNode = resultNode.SelectSingleNode("failure/stack-trace"); if (stackNode != null) - testLog.SendMessage(TestMessageLevel.Error, stackNode.InnerText); + _recorder.SendMessage(TestMessageLevel.Error, stackNode.InnerText); } } } diff --git a/src/NUnitTestAdapter/NUnitTestAdapter.cs b/src/NUnitTestAdapter/NUnitTestAdapter.cs index 9784dba5..81475220 100644 --- a/src/NUnitTestAdapter/NUnitTestAdapter.cs +++ b/src/NUnitTestAdapter/NUnitTestAdapter.cs @@ -54,7 +54,7 @@ public NUnitTestAdapter() public AdapterSettings Settings { get; private set; } // The adapter version - private string AdapterVersion { get; set; } + protected string AdapterVersion { get; set; } protected ITestEngine TestEngine { get; private set; } @@ -90,20 +90,20 @@ public static bool IsRunningUnderIDE // We don't have any info to initialize it until one of the // ITestDiscovery or ITestExecutor methods is called. The // each Discover or Execute method must call this method. - protected virtual void Initialize(IDiscoveryContext context, IMessageLogger messageLogger) + protected void Initialize(IDiscoveryContext context, IMessageLogger messageLogger) { + TestEngine = new TestEngineClass(); + TestLog = new TestLogger(messageLogger, Settings.Verbosity); + try { Settings.Load(context); } catch (Exception e) { - messageLogger.SendMessage(TestMessageLevel.Error, "Error initializing RunSettings. Default settings will be used"); - messageLogger.SendMessage(TestMessageLevel.Error, e.ToString()); + TestLog.Error("Error initializing RunSettings. Default settings will be used"); + TestLog.Error(e.ToString()); } - - TestEngine = new TestEngineClass(); - TestLog = new TestLogger(messageLogger, Settings.Verbosity); } protected ITestRunner GetRunnerFor(string assemblyName) @@ -116,8 +116,8 @@ protected ITestRunner GetRunnerFor(string assemblyName) } catch(Exception ex) { - TestLog.SendErrorMessage("Error: Unable to get runner for this assembly. Check installation, including any extensions."); - TestLog.SendErrorMessage(ex.GetType().Name + ": " + ex.Message); + TestLog.Error("Error: Unable to get runner for this assembly. Check installation, including any extensions."); + TestLog.Error(ex.GetType().Name + ": " + ex.Message); throw; } } @@ -129,13 +129,13 @@ private TestPackage CreateTestPackage(string assemblyName) if (Settings.ShadowCopyFiles) { package.Settings[PackageSettings.ShadowCopyFiles] = "true"; - TestLog.SendDebugMessage(" Setting ShadowCopyFiles to true"); + TestLog.Debug(" Setting ShadowCopyFiles to true"); } if (Debugger.IsAttached) { package.Settings[PackageSettings.NumberOfTestWorkers] = 0; - TestLog.SendDebugMessage(" Setting NumberOfTestWorkers to zero for Debugging"); + TestLog.Debug(" Setting NumberOfTestWorkers to zero for Debugging"); } else { @@ -175,20 +175,6 @@ private TestPackage CreateTestPackage(string assemblyName) return package; } - protected void Info(string method, string function) - { - var msg = string.Format("NUnit Adapter {0} {1} is {2}", AdapterVersion, method, function); - TestLog.SendInformationalMessage(msg); - } - - protected void Debug(string method, string function) - { -#if DEBUG - var msg = string.Format("NUnit Adapter {0} {1} is {2}", AdapterVersion, method, function); - TestLog.SendDebugMessage(msg); -#endif - } - protected static void CleanUpRegisteredChannels() { foreach (IChannel chan in ChannelServices.RegisteredChannels) diff --git a/src/NUnitTestAdapter/NUnitTestFilterBuilder.cs b/src/NUnitTestAdapter/NUnitTestFilterBuilder.cs new file mode 100644 index 00000000..90206c86 --- /dev/null +++ b/src/NUnitTestAdapter/NUnitTestFilterBuilder.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestPlatform.ObjectModel; +using NUnit.Engine; + +namespace NUnit.VisualStudio.TestAdapter +{ + public class NUnitTestFilterBuilder + { + private ITestFilterService _filterService; + + public NUnitTestFilterBuilder(ITestFilterService filterService) + { + if (filterService == null) + throw new NUnitEngineException("TestFilterService is not available. Engine in use is incorrect version."); + + _filterService = filterService; + } + + public TestFilter ConvertTfsFilterToNUnitFilter(TfsTestFilter tfsFilter, List loadedTestCases) + { + var filteredTestCases = tfsFilter.CheckFilter(loadedTestCases); + var testCases = filteredTestCases as TestCase[] ?? filteredTestCases.ToArray(); + //TestLog.Info(string.Format("TFS Filter detected: LoadedTestCases {0}, Filterered Test Cases {1}", loadedTestCases.Count, testCases.Count())); + return MakeTestFilter(testCases); + } + + public TestFilter MakeTestFilter(IEnumerable testCases) + { + ITestFilterBuilder filterBuilder = _filterService.GetTestFilterBuilder(); + + foreach (TestCase testCase in testCases) + filterBuilder.AddTest(testCase.FullyQualifiedName); + + return filterBuilder.GetFilter(); + } + } +} diff --git a/src/NUnitTestAdapter/TFSTestFilter.cs b/src/NUnitTestAdapter/TFSTestFilter.cs index cb818354..cde5bf6a 100644 --- a/src/NUnitTestAdapter/TFSTestFilter.cs +++ b/src/NUnitTestAdapter/TFSTestFilter.cs @@ -15,7 +15,9 @@ namespace NUnit.VisualStudio.TestAdapter public interface ITfsTestFilter { ITestCaseFilterExpression TfsTestCaseFilterExpression { get; } - bool HasTfsFilterValue { get; } + + bool IsEmpty { get; } + IEnumerable CheckFilter(IEnumerable tests); } @@ -73,13 +75,11 @@ public ITestCaseFilterExpression TfsTestCaseFilterExpression } } - public bool HasTfsFilterValue + public bool IsEmpty { - get - { - return TfsTestCaseFilterExpression != null && TfsTestCaseFilterExpression.TestCaseFilterValue != String.Empty; - } + get { return TfsTestCaseFilterExpression == null || TfsTestCaseFilterExpression.TestCaseFilterValue == string.Empty; } } + public IEnumerable CheckFilter(IEnumerable tests) { diff --git a/src/NUnitTestAdapter/TestConverter.cs b/src/NUnitTestAdapter/TestConverter.cs index 66ec2621..9b04497c 100644 --- a/src/NUnitTestAdapter/TestConverter.cs +++ b/src/NUnitTestAdapter/TestConverter.cs @@ -60,7 +60,7 @@ public TestCase GetCachedTestCase(string id) if (vsTestCaseMap.ContainsKey(id)) return vsTestCaseMap[id]; - logger.SendErrorMessage("Test " + id + " not found in cache"); + logger.Error("Test " + id + " not found in cache"); return null; } @@ -172,7 +172,7 @@ public DiaNavigationData GetNavigationData(string className, string methodName) } if (navData == null || navData.FileName == null) - logger.SendWarningMessage(string.Format("No source data found for {0}.{1}", className, methodName)); + logger.Warning(string.Format("No source data found for {0}.{1}", className, methodName)); return navData; } @@ -245,7 +245,7 @@ private AsyncMethodHelper TryCreateHelper(string sourceAssembly) { // If we can't load it for some reason, we issue a warning // and won't try to do it again for the assembly. - logger.SendWarningMessage("Unable to create AsyncMethodHelper\r\nSource data will not be available for some of the tests", ex); + logger.Warning("Unable to create AsyncMethodHelper\r\nSource data will not be available for some of the tests", ex); return null; } } @@ -276,8 +276,8 @@ private DiaSession DiaSession { // If this isn't a project type supporting DiaSession, // we just issue a warning. We won't try this again. - logger.SendWarningMessage("Unable to create DiaSession for " + sourceAssembly + "\r\nNo source location data will be available for this assembly."); - logger.SendDebugMessage(ex.Message); + logger.Warning("Unable to create DiaSession for " + sourceAssembly + "\r\nNo source location data will be available for this assembly."); + logger.Debug(ex.Message); } tryToCreateDiaSession = false; diff --git a/src/NUnitTestAdapter/TestLogger.cs b/src/NUnitTestAdapter/TestLogger.cs index 0664824d..c6b85f46 100644 --- a/src/NUnitTestAdapter/TestLogger.cs +++ b/src/NUnitTestAdapter/TestLogger.cs @@ -31,83 +31,79 @@ public TestLogger(IMessageLogger messageLogger, int verbosity) Verbosity = verbosity; } - public void AssemblyNotSupportedWarning(string sourceAssembly) - { - SendWarningMessage("Assembly not supported: " + sourceAssembly); - } - - public void DependentAssemblyNotFoundWarning(string dependentAssembly, string sourceAssembly) - { - SendWarningMessage("Dependent Assembly " + dependentAssembly + " of " + sourceAssembly + " not found. Can be ignored if not a NUnit project."); - } - - public void LoadingAssemblyFailedWarning(string dependentAssembly, string sourceAssembly) - { - SendWarningMessage("Assembly " + dependentAssembly + " loaded through " + sourceAssembly + " failed. Assembly is ignored. Correct deployment of dependencies if this is an error."); - } - - public void NUnitLoadError(string sourceAssembly) - { - SendInformationalMessage("NUnit failed to load " + sourceAssembly); - } + #region Error Messages - public void SendErrorMessage(string message) + public void Error(string message) { SendMessage(TestMessageLevel.Error, message); } - public void SendErrorMessage(string message, Exception ex) + public void Error(string message, Exception ex) { switch (Verbosity) { case 0: Type type = ex.GetType(); - SendErrorMessage(string.Format(EXCEPTION_FORMAT, type, message)); - SendErrorMessage(ex.Message); + Error(string.Format(EXCEPTION_FORMAT, type, message)); + Error(ex.Message); break; default: - SendErrorMessage(message); - SendErrorMessage(ex.ToString()); + Error(message); + Error(ex.ToString()); break; } } - public void SendWarningMessage(string message) + #endregion + + #region Warning Messages + + public void Warning(string message) { SendMessage(TestMessageLevel.Warning, message); } - public void SendWarningMessage(string message,Exception ex) + public void Warning(string message,Exception ex) { switch (Verbosity) { case 0: var type = ex.GetType(); - SendWarningMessage(string.Format(EXCEPTION_FORMAT, type, message)); - SendWarningMessage(ex.Message); + Warning(string.Format(EXCEPTION_FORMAT, type, message)); + Warning(ex.Message); break; default: - SendWarningMessage(message); - SendWarningMessage(ex.ToString()); + Warning(message); + Warning(ex.ToString()); break; } SendMessage(TestMessageLevel.Warning, message); } - public void SendInformationalMessage(string message) + #endregion + + #region Information Messages + + public void Info(string message) { SendMessage(TestMessageLevel.Informational, message); } - public void SendDebugMessage(string message) + #endregion + + #region Debug Messages + + public void Debug(string message) { #if DEBUG SendMessage(TestMessageLevel.Informational, message); #endif } + #endregion + public void SendMessage(TestMessageLevel testMessageLevel, string message) { if (MessageLogger != null) From 7f24a7d224d869f85101f8504c060842adc7a160 Mon Sep 17 00:00:00 2001 From: Charlie Poole Date: Fri, 8 Apr 2016 10:41:23 -0700 Subject: [PATCH 2/2] Fix error in tests --- src/NUnitTestAdapter/NUnit3TestExecutor.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/NUnitTestAdapter/NUnit3TestExecutor.cs b/src/NUnitTestAdapter/NUnit3TestExecutor.cs index 845454c9..5cc42105 100644 --- a/src/NUnitTestAdapter/NUnit3TestExecutor.cs +++ b/src/NUnitTestAdapter/NUnit3TestExecutor.cs @@ -33,7 +33,13 @@ public sealed class NUnit3TestExecutor : NUnitTestAdapter, ITestExecutor, IDispo public IRunContext RunContext { get; private set; } public IFrameworkHandle FrameworkHandle { get; private set; } private TfsTestFilter TfsFilter { get; set; } - private NUnitTestFilterBuilder FilterBuilder { get; set; } + + // NOTE: an earlier version of this code had a FilterBuilder + // property. This seemed to make sense, because we instantiate + // it in two different places. However, the existence of an + // NUnitTestFilterBuilder, containing a reference to an engine + // service caused our second-level tests of the test executor + // to throw an exception. So if you consider doing this, beware! #endregion @@ -105,7 +111,8 @@ public void RunTests(IEnumerable tests, IRunContext runContext, IFrame else TestLog.Info("Running selected tests in " + assemblyName); - var filter = FilterBuilder.MakeTestFilter(assemblyGroup); + var filterBuilder = new NUnitTestFilterBuilder(TestEngine.Services.GetService()); + var filter = filterBuilder.MakeTestFilter(assemblyGroup); RunAssembly(assemblyName, filter); } @@ -142,7 +149,6 @@ public void Initialize(IRunContext runContext, IFrameworkHandle frameworkHandle) RunContext = runContext; FrameworkHandle = frameworkHandle; TfsFilter = new TfsTestFilter(runContext); - FilterBuilder = new NUnitTestFilterBuilder(TestEngine.Services.GetService()); // Ensure any channels registered by other adapters are unregistered CleanUpRegisteredChannels(); @@ -200,7 +206,8 @@ private void RunAssembly(string assemblyName, TestFilter filter) if (TfsFilter != null && !TfsFilter.IsEmpty) { // NOTE This overwrites filter used in call - filter = FilterBuilder.ConvertTfsFilterToNUnitFilter(TfsFilter, loadedTestCases); + var filterBuilder = new NUnitTestFilterBuilder(TestEngine.Services.GetService()); + filter = filterBuilder.ConvertTfsFilterToNUnitFilter(TfsFilter, loadedTestCases); } using (var listener = new NUnitEventListener(FrameworkHandle, testConverter))