/// <summary> /// Constructor uses the default test suit name /// </summary> protected TestClassBase() { IProtocolTestsManager manager = ProtocolTestsManagerFactory.TestsManager; if (!suiteNameCache.ContainsKey(this.GetType())) { suiteNameCache[this.GetType()] = staticTestSuiteName; } //switch test site while test running testSuiteName = suiteNameCache[this.GetType()]; testSite = manager.GetTestSite(testSuiteName); ptfTestNotify = manager.GetProtocolTestNotify(testSuiteName); if (testSite == null) { StringBuilder errorMsg = new StringBuilder(); errorMsg.AppendFormat("Cannot get the test site {0}.", testSuiteName) .AppendLine() .Append("If you are running test suite from Visual Studio, please make sure that the TestSettings file is selected in TEST\\Test Settings menu."); throw new InvalidOperationException(errorMsg.ToString()); } }
/// <summary> /// Initializes the test suite base class with explicitly given test suite name. /// This method must be called by class initialize method in your test class. /// </summary> /// <param name="testContext">VSTS test context.</param> /// <param name="testSuiteName">The name of the test suite. The test site uses this name to find configuration files.</param> public static void Initialize(TestContext testContext, string testSuiteName) { Microsoft.Protocols.TestTools.ExtendedLogging.ExtendedLoggerConfig.TestSuiteName = testSuiteName; executionStartTime = DateTime.Now; if (testContext == null) { throw new InvalidOperationException("TestContext should not be null in UnitTestClassBase."); } classCount++; staticTestSuiteName = testSuiteName; IProtocolTestsManager manager = ProtocolTestsManagerFactory.TestsManager; if (null == manager.GetTestSite(staticTestSuiteName)) { string testAssemblyName; IConfigurationData config = ConfigurationDataProvider.GetConfigurationData( testContext.TestDeploymentDir, testSuiteName); if (isUseDefaultSuiteName) { testAssemblyName = testSuiteName; isUseDefaultSuiteName = false; } else { testAssemblyName = Assembly.GetCallingAssembly().GetName().Name; } manager.Initialize(config, new VstsTestContext(testContext), testSuiteName, testAssemblyName); baseTestSite = manager.GetTestSite(testSuiteName); if (IsCommandlineConsoleRulePresent(config.Profiles)) { AllocConsole(); IntPtr hWnd = GetConsoleWindow(); bool visible = IsWindowVisible(hWnd); if (!visible) { ShowWindow(hWnd, 9); // 9 SW_RESTORE. Make the console visible. } Console.WriteLine("Test Results:"); Console.WriteLine("=============="); string consoleWidth = baseTestSite.Properties.Get("ConsoleWidth"); if (consoleWidth != null) { Console.WindowWidth = Convert.ToInt32(consoleWidth); } string consoleHeight = baseTestSite.Properties.Get("ConsoleHeight"); if (consoleHeight != null) { Console.WindowHeight = Convert.ToInt32(consoleHeight); } string consoleBufferHeight = baseTestSite.Properties.Get("ConsoleBufferHeight"); if (consoleBufferHeight != null) { Console.BufferHeight = Convert.ToInt32(consoleBufferHeight); } } ITestSite site = manager.GetTestSite(testSuiteName); //registry all checkers RegisterChecker(site); } else { baseTestSite = manager.GetTestSite(testSuiteName); } /********************* Display expected runtime of the testsuite ********************** * Log expected execution time of the test suite in the log file * **************************************************************************************/ baseTestSite.Log.Add(LogEntryKind.Comment, "Expected execution time of the test suite (in seconds) is: " + baseTestSite.Properties.Get("ExpectedExecutionTime")); //************* Automatic network message capture.************* if (Convert.ToBoolean(baseTestSite.Properties.Get("PTF.NetworkCapture.Enabled"))) { string assemblyFile = baseTestSite.Properties.Get("PTF.NetworkCapture.Assembly"); if (assemblyFile == null) { // Use logman to capture by default. autoCapture = new LogmanCapture(); } else { Assembly assembly = Assembly.LoadFrom(assemblyFile); string className = baseTestSite.Properties.Get("PTF.NetworkCapture.Class"); if (className != null) { autoCapture = (IAutoCapture)assembly.CreateInstance(className); } else { foreach (Type type in assembly.GetTypes()) { if (type.IsClass && typeof(IAutoCapture).IsAssignableFrom(type)) { autoCapture = (IAutoCapture)Activator.CreateInstance(type); } } } } try { if (autoCapture != null) { autoCapture.Initialize(baseTestSite.Properties, testSuiteName); } } catch (AutoCaptureException e) { baseTestSite.Log.Add(LogEntryKind.Warning, "Auto capture initialize Error: " + e.Message); if (e.StopRunning) { throw; } } } }