Microsoft.Silverlight.Testing.Tools.TestRun.Run C# (CSharp) Method

Run() private method

private Run ( ) : bool
return bool
        public bool Run()
        {
            try
            {
                Failures = -1;
                string page = Options.Page;
                string path = Options.LocalPath;
                if (File.Exists(path))
                {
                    FileInfo fi = new FileInfo(path);
                    TestService.RootDirectory = fi.DirectoryName;
                }
                else if (Directory.Exists(path))
                {
                    TestService.RootDirectory = path;
                }

                TestService.TestRunPrefix = Options.DeviceInfo.DeviceId.ToString();
                TestService.Start();
                if (!string.IsNullOrEmpty(Options.TagExpression))
                {
                    TestService.TagExpression = Options.TagExpression;
                }
                if (!string.IsNullOrEmpty(Options.Log))
                {
                    TestService.LogFile = Options.Log;
                }

                TargetDevice = Options.DeviceInstance ?? new TargetDevice(Options.DeviceInfo);

                TargetDevice.Start(Options.ApplicationProductId, Options.ApplicationGenre,
                    Options.XapFile, Options.UpdateApplication);

                while (TestService.IsRunning && TargetDevice.IsRunning && TestService.Result == null)
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(PollingThreadSleepMilliseconds));
                }

                TestRunResult result = TestService.Result;
                if (result != null)
                {
                    Total = result.Total;
                    Log = result.Log;
                    Failures = result.Failures;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(FinalSleepDelayMilliseconds));
                TargetDevice.Close();
                TestService.Stop();
            }
            return (Failures == 0);
        }

Usage Example

Example #1
0
        public override bool Execute()
        {
            // There's nothing to do if we have no source files
            if (String.IsNullOrEmpty(XapFile))
            {
                return true;
            }

            if (String.IsNullOrEmpty(ApplicationProductId) && !String.IsNullOrEmpty(ApplicationManifest))
            {
                ApplicationProductId = GetApplicationProductId();

                if (!String.IsNullOrEmpty(ApplicationProductId))
                {
                    Log.LogMessage("ProductId extracted from manifest: {0}", ApplicationProductId);
                }
            }

            if (String.IsNullOrEmpty(ApplicationProductId))
            {
                Log.LogError("ApplicationProductId was not supplied and could not be found");
                return false;
            }

            // Process the files
            bool succeeded = true;
            try
            {

                string testPath = XapFile;
                FileInfo testApplication = new FileInfo(testPath);

                // Make sure they didn't pass a directory as an item
                if (Directory.Exists(testPath))
                {
                    Log.LogError("Cannot move item {0} because it is a directory!", testPath);
                    return false;
                }

                // Make sure the source exists
                if (!testApplication.Exists)
                {
                    Log.LogError("Cannot process file {0} that does not exist!", testPath);
                    return false;
                }

                string testName = GetTestName(testApplication.Directory);
                if (!string.IsNullOrEmpty(TagExpression))
                {
                    testName += " (" + TagExpression + ")";
                }

                string name = TestResultsFile;
                if (string.IsNullOrEmpty(name))
                {
                    int k = 1;
                    name = "TestResults.trx";
                    while (File.Exists(Path.Combine(testApplication.DirectoryName, name)))
                    {
                        name = string.Format(CultureInfo.InvariantCulture, "TestResults{0}.trx", k++);
                    }
                }
                FileInfo log = new FileInfo(Path.Combine(testApplication.DirectoryName, name));

                Log.LogMessage("Begin unit testing");

                TestRunOptions tro = new TestRunOptions
                {
                    XapFile = testApplication.FullName,
                    ApplicationProductId = new Guid(ApplicationProductId),
                    UpdateApplication = UpdateApplication,
                    DeviceInfo = CreateDeviceInfo(),
                    TagExpression = TagExpression,
                    Log = log.FullName,
                    LocalPath = Path.GetDirectoryName(log.FullName)
                };
                tro.Page = testApplication.Name;

                TestRun tr = new TestRun(
                    new TestServiceOptions(),
                    tro);
                tr.Run();

                // Interpret results
                string pass = null;
                string total = null;

                if (log.Exists)
                {
                    DisplayTestResults(log, ref total, ref pass);

                    if (DeleteLogFiles)
                    {
                        log.Delete();
                    }
                }
                else
                {
                    Log.LogError(
                        "The log file {0} was never written by the test service for the {1} test.",
                        log.FullName,
                        testName);
                }

                if (tr.Total == 0)
                {
                    Log.LogWarning(
                        "There were 0 reported scenarios executed. Check that the tag expression is appropriate.");
                }
                else if (tr.Failures == 0)
                {
                    Log.LogMessage(
                        MessageImportance.High,
                        "Unit tests ({0}): {1}{2}",
                        testName,
                        pass != null ? " " + pass + " passing tests" : "",
                        total != null ? " " + total + " total tests" : "");
                }
                else
                {
                    succeeded = false;
                    LogFailureMessage(
                        "Unit test failures in test " +
                        testName +
                        ", " +
                        tr.Failures.ToString(CultureInfo.CurrentUICulture) +
                        " failed scenarios out of " +
                        tr.Total.ToString(CultureInfo.CurrentUICulture) +
                        " total scenarios executed.");
                }
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                succeeded = false;
            }

            return succeeded;
        }
All Usage Examples Of Microsoft.Silverlight.Testing.Tools.TestRun::Run