Пример #1
0
        /// <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)
        {
            executionStartTime = DateTime.Now;
            if (testContext == null)
            {
                throw new InvalidOperationException("TestContext should not be null in UnitTestClassBase.");
            }
            classCount++;
            staticTestSuiteName = testSuiteName;

            if (null == ProtocolTestsManager.GetTestSite(staticTestSuiteName))
            {
                VstsTestContext    vstsTestContext = new VstsTestContext(testContext);
                IConfigurationData config          = ConfigurationDataProvider.GetConfigurationData(
                    vstsTestContext.PtfconfigDir, testSuiteName);

                string testAssemblyName;

                if (isUseDefaultSuiteName)
                {
                    testAssemblyName      = testSuiteName;
                    isUseDefaultSuiteName = false;
                }
                else
                {
                    testAssemblyName = Assembly.GetCallingAssembly().GetName().Name;
                }

                ProtocolTestsManager.Initialize(config, vstsTestContext, testSuiteName, testAssemblyName);

                baseTestSite = ProtocolTestsManager.GetTestSite(testSuiteName);

                ITestSite site = ProtocolTestsManager.GetTestSite(testSuiteName);

                //registry all checkers
                RegisterChecker(site);
            }
            else
            {
                baseTestSite = ProtocolTestsManager.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"));
        }
        private static void RegisterChecker(ITestSite testSite)
        {
            IDictionary <CheckerKinds, IChecker> checkers = new Dictionary <CheckerKinds, IChecker>();
            ICheckerConfig checkerConfig;

            if (ConfigurationDataProvider.TryGetCheckerConfig <ICheckerConfig>(out checkerConfig))
            {
                IChecker assertChecker = VsCheckerFactory.GetChecker(CheckerKinds.AssertChecker, testSite, checkerConfig);
                IChecker assumeChecker = VsCheckerFactory.GetChecker(CheckerKinds.AssumeChecker, testSite, checkerConfig);
                IChecker debugChecker  = VsCheckerFactory.GetChecker(CheckerKinds.DebugChecker, testSite, checkerConfig);
                checkers.Add(CheckerKinds.AssertChecker, assertChecker);
                checkers.Add(CheckerKinds.AssumeChecker, assumeChecker);
                checkers.Add(CheckerKinds.DebugChecker, debugChecker);
                testSite.RegisterCheckers(checkers);
            }
            else
            {
                throw new InvalidOperationException("Cannot retrieve the checker configuration from configuration data.");
            }
        }
        /// <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;
                    }
                }
            }
        }