/// <summary> /// Asserts two values are equal. /// </summary> /// <typeparam name="T">Type of values.</typeparam> /// <param name="manager">The test manager.</param> /// <param name="expected">The expected value.</param> /// <param name="actual">The actual value.</param> /// <param name="context">The description of the context under which both values are compared.</param> public static void AssertAreEqual <T>(IProtocolTestsManager manager, T expected, T actual, string context) { manager.Assert( Object.Equals(expected, actual), string.Format("expected \'{0}\', actual \'{1}\' ({2})", MessageRuntimeHelper.Describe(expected), MessageRuntimeHelper.Describe(actual), context) ); }
/// <summary> /// Asserts a variable's equality to a value or bind the variable to a value if it hasn't been bound yet. /// </summary> /// <typeparam name="T">Type of the variable and value.</typeparam> /// <param name="manager">The test manager.</param> /// <param name="var">The variable.</param> /// <param name="actual">The actual value.</param> /// <param name="context">The description of the context under which the comparison or binding happens.</param> public static void AssertBind <T>(IProtocolTestsManager manager, IVariable <T> var, T actual, string context) { if (var.IsBound) { AssertAreEqual <T>(manager, var.Value, actual, context + "; expected value originates from previous binding"); } else { var.Value = actual; } }
/// <summary> /// Asserts equality of two variables, or bind one variable to another if only one of them is bound. /// If neither of the two variables are bound, this API does nothing. /// </summary> /// <typeparam name="T">Type of the variables.</typeparam> /// <param name="manager">The test manager.</param> /// <param name="v1">The first variable.</param> /// <param name="v2">The second variable.</param> /// <param name="context">The context under which the comparison or binding happens.</param> public static void AssertBind <T>(IProtocolTestsManager manager, IVariable <T> v1, IVariable <T> v2, string context) { if ((v1.IsBound && v2.IsBound)) { AssertAreEqual <T>(manager, v1.Value, v2.Value, context + "; values originate from previous binding"); return; } if (v1.IsBound) { v2.Value = v1.Value; } else { if (v2.IsBound) { v1.Value = v2.Value; } } }
/// <summary> /// Asserts two values are equal. /// </summary> /// <typeparam name="T">Type of values.</typeparam> /// <param name="manager">The test manager.</param> /// <param name="expected">The expected value.</param> /// <param name="actual">The actual value.</param> /// <param name="context">The description of the context under which both values are compared.</param> public static void AssertAreEqual <T>(IProtocolTestsManager manager, IList <T> expected, IList <T> actual, string context) { bool listEqual = false; if (expected != null && actual != null) { listEqual = expected.All(actual.Contains) && expected.Count == actual.Count; } else if (expected == null && actual == null) { listEqual = true; } StringBuilder expectedDescribeBuilder = new StringBuilder(); foreach (var item in expected) { expectedDescribeBuilder.AppendFormat("{0},", MessageRuntimeHelper.Describe <object>(item)); } StringBuilder actualDescribeBuilder = new StringBuilder(); foreach (var item in actual) { actualDescribeBuilder.AppendFormat("{0},", MessageRuntimeHelper.Describe <object>(item)); } if (expectedDescribeBuilder.Length > 0) { expectedDescribeBuilder.Remove(expectedDescribeBuilder.Length - 1, 1); } if (actualDescribeBuilder.Length > 0) { actualDescribeBuilder.Remove(actualDescribeBuilder.Length - 1, 1); } manager.Assert( listEqual, string.Format("expected \'{0}\', actual \'{1}\' ({2})", expectedDescribeBuilder.ToString(), actualDescribeBuilder.ToString(), context) ); }
/// <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; } } } }
/// <summary> /// Asserts a value is not null. /// </summary> /// <param name="manager">The test manager.</param> /// <param name="actual">The value under check.</param> /// <param name="context">The context under which the value is checked.</param> public static void AssertNotNull(IProtocolTestsManager manager, object actual, string context) { manager.Assert(actual != null, string.Format("expected non-null value ({0})", context)); }
/// <summary> /// To clean up the test manager /// </summary> public virtual void CleanupTestManager() { // FIXME: check for ERROR entries in queues manager = null; }
/// <summary> /// To initialize the test manager /// </summary> public virtual void InitializeTestManager() { this.manager = new ProtocolTestsManager(this, observationBound, observationBound); }
internal Variable(string name, IProtocolTestsManager manager) { this.name = name; this.manager = manager; }