示例#1
0
        public bool Run(TestInfo test, int pass, int testNumber)
        {
            if (_showStatus)
            {
                Log("#@ Running {0} ({1})...\n", test.TestMethod.Name, Language.TwoLetterISOLanguageName);
            }

            if (_buildMode)
            {
                Log("{0,3}. {1,-46} ",
                    testNumber,
                    test.TestMethod.Name);
            }
            else
            {
                var time = DateTime.Now;
                Log("[{0}:{1}] {2,3}.{3,-3} {4,-46} ({5}) ",
                    time.Hour.ToString("D2"),
                    time.Minute.ToString("D2"),
                    pass,
                    testNumber,
                    test.TestMethod.Name,
                    Language.TwoLetterISOLanguageName);
            }

            // Create test class.
            var testObject = Activator.CreateInstance(test.TestClassType);

            // Set the TestContext.
            TestContext.Properties["AccessInternet"]     = AccessInternet.ToString();
            TestContext.Properties["RunPerfTests"]       = RunPerfTests.ToString();
            TestContext.Properties["TestSmallMolecules"] = TestSmallMolecules.ToString();
            TestContext.Properties["LiveReports"]        = LiveReports.ToString();
            if (test.SetTestContext != null)
            {
                var context = new object[] { TestContext };
                test.SetTestContext.Invoke(testObject, context);
            }

            // Switch to selected culture.
            var saveCulture   = Thread.CurrentThread.CurrentCulture;
            var saveUICulture = Thread.CurrentThread.CurrentUICulture;

            LocalizationHelper.CurrentCulture = LocalizationHelper.CurrentUICulture = Language;
            LocalizationHelper.InitThread();

            long crtLeakedBytes = 0;

            // Run the test and time it.
            Exception exception = null;
            var       stopwatch = new Stopwatch();

            stopwatch.Start();
            try
            {
                if (test.TestInitialize != null)
                {
                    test.TestInitialize.Invoke(testObject, null);
                }

                if (CheckCrtLeaks > 0)
                {
                    CrtDebugHeap.Checkpoint();
                }
                test.TestMethod.Invoke(testObject, null);
                if (CheckCrtLeaks > 0)
                {
                    crtLeakedBytes = CrtDebugHeap.DumpLeaks(true);
                }

                if (test.TestCleanup != null)
                {
                    test.TestCleanup.Invoke(testObject, null);
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            stopwatch.Stop();
            LastTestDuration = (int)(stopwatch.ElapsedMilliseconds / 1000);

            // Restore culture.
            Thread.CurrentThread.CurrentCulture   = saveCulture;
            Thread.CurrentThread.CurrentUICulture = saveUICulture;

            MemoryManagement.FlushMemory();

            const int mb            = 1024 * 1024;
            var       managedMemory = (double)GC.GetTotalMemory(true) / mb;

            if (exception == null)
            {
                // Test succeeded.
                Log(
                    "{0,3} failures, {1:F2}/{2:F1} MB, {3} sec.\r\n",
                    FailureCount,
                    managedMemory,
                    TotalMemory,
                    LastTestDuration);
                if (crtLeakedBytes > CheckCrtLeaks)
                {
                    Log("!!! {0} CRT-LEAKED {1} bytes", test.TestMethod.Name, crtLeakedBytes);
                }

                using (var writer = new FileStream("TestRunnerMemory.log", FileMode.Append, FileAccess.Write, FileShare.Read))
                    using (var stringWriter = new StreamWriter(writer))
                    {
                        stringWriter.WriteLine(TotalMemory.ToString("F1"));
                    }
                return(true);
            }

            // Save failure information.
            FailureCount++;
            if (FailureCounts.ContainsKey(test.TestMethod.Name))
            {
                FailureCounts[test.TestMethod.Name]++;
            }
            else
            {
                FailureCounts[test.TestMethod.Name] = 1;
            }
            var failureInfo = "# " + test.TestMethod.Name + "FAILED:\n" +
                              exception.InnerException.Message + "\n" +
                              exception.InnerException.StackTrace;

            if (ErrorCounts.ContainsKey(failureInfo))
            {
                ErrorCounts[failureInfo]++;
            }
            else
            {
                ErrorCounts[failureInfo] = 1;
            }

            Log(
                "{0,3} failures, {1:F2}/{2:F1} MB\r\n\r\n!!! {3} FAILED\r\n{4}\r\n{5}\r\n!!!\r\n\r\n",
                FailureCount, managedMemory, TotalMemory, test.TestMethod.Name,
                exception.InnerException.Message,
                exception.InnerException.StackTrace);
            return(false);
        }
示例#2
0
        public void Run(TestInfo test, CultureInfo culture, Stopwatch stopwatch = null)
        {
            // Delete test directory.
            int testDirectoryCount = 1;

            while (Directory.Exists(_testDir))
            {
                try
                {
                    // Try delete 4 times to give anti-virus software a chance to finish.
// ReSharper disable AccessToModifiedClosure
                    TryLoop.Try <IOException>(() => Directory.Delete(_testDir, true), 4);
// ReSharper restore AccessToModifiedClosure
                }
                catch (Exception e)
                {
                    Console.WriteLine("\n\n" + e.Message);
                    _testDir = SetTestDir(_testContext, ++testDirectoryCount, Process.GetCurrentProcess());
                }
            }

            // Create test class.
            var testObject = Activator.CreateInstance(test.TestClassType);

            // Set the TestContext.
            if (test.SetTestContext != null)
            {
                var context = new object[] { _testContext };
                test.SetTestContext.Invoke(testObject, context);
            }

            // Switch to selected culture.
            LocalizationHelper.CurrentCulture = culture;
            LocalizationHelper.InitThread();

            // Run the test and time it.
            Exception exception = null;

            if (stopwatch != null)
            {
                stopwatch.Start();
            }
            long totalLeakedBytes = 0;

            try
            {
                if (test.TestInitialize != null)
                {
                    test.TestInitialize.Invoke(testObject, null);
                }

                if (pass > 1 || repeatCounter > 1)
                {
                    CrtDebugHeap.Checkpoint();
                }
                test.TestMethod.Invoke(testObject, null);
                if (pass > 1 || repeatCounter > 1)
                {
                    long leakedBytes = CrtDebugHeap.DumpLeaks(true);
                    totalLeakedBytes += leakedBytes;
                }

                if (test.TestCleanup != null)
                {
                    test.TestCleanup.Invoke(testObject, null);
                }
            }
            catch (Exception e)
            {
                exception = e;
            }

            if (stopwatch != null)
            {
                stopwatch.Stop();
            }

            // Restore culture.
            Thread.CurrentThread.CurrentCulture   = saveCulture;
            Thread.CurrentThread.CurrentUICulture = saveUICulture;

            var managedMemory = GC.GetTotalMemory(false) / mb;

            process.Refresh();
            var totalMemory = process.PrivateMemorySize64 / mb;

            if (exception == null)
            {
                // Test succeeded.
                info = string.Format(
                    "{0,3} failures, {1:0.0}/{2:0.0} MB{3}, {4} sec.",
                    _failureCount,
                    managedMemory,
                    totalMemory,
                    totalLeakedBytes > 0 ? string.Format("  *** LEAKED {0} bytes ***", totalLeakedBytes) : "",
                    stopwatch.ElapsedMilliseconds / 1000);
                Console.WriteLine(info);
                log.WriteLine(info);
            }
            else
            {
                // Save failure information.
                _failureCount++;
                failureList[testName]++;
                info = testName + " {0} failures ({1:0.##}%)\n" +
                       exception.InnerException.Message + "\n" +
                       exception.InnerException.StackTrace;
                if (errorList.ContainsKey(info))
                {
                    errorList[info]++;
                }
                else
                {
                    errorList[info] = 1;
                }
                Console.WriteLine("*** FAILED {0:0.#}% ***", 100.0 * failureList[testName] / pass);
                log.WriteLine("{0,3} failures, {1:0.0}/{2:0.0} MB\n*** failure {3}\n{4}\n{5}\n***",
                              _failureCount, managedMemory, totalMemory, errorList[info], exception.InnerException.Message,
                              exception.InnerException.StackTrace);
            }
            log.Flush();

            if (totalLeakedBytes > 0)
            {
                Trace.WriteLine(string.Format("\n*** {0} leaked ***\n", testName));
            }
        }