public FacadeTestRunState Run(IFacadeTestListener testListener, string assemblyPath, string cref, FacadeOptions facadeOptions)
 {
     try
     {
         return RunImpl(testListener, assemblyPath, cref, facadeOptions);
     }
     catch (Exception ex)
     {
         throw ServerExceptionUtils.Wrap(ex);
     }
 }
        /// <inheritdoc />
        protected override FacadeTestRunState RunImpl(IFacadeTestListener testListener, string assemblyPath, string cref, FacadeOptions facadeOptions)
        {
            string versionLabel = VersionPolicy.GetVersionLabel(Assembly.GetExecutingAssembly());
            testListener.WriteLine(String.Format(Resources.RunnerNameAndVersion + "\n", versionLabel), FacadeCategory.Info);

            switch (facadeOptions.FilterCategoryMode)
            {
                case FacadeFilterCategoryMode.Include:
                    testListener.WriteLine(String.Format("Included categories: {0}\n", CombineCategoryNameList(facadeOptions.FilterCategoryNames)), FacadeCategory.Info);
                    break;

                case FacadeFilterCategoryMode.Exclude:
                    testListener.WriteLine(String.Format("Excluded categories: {0}\n", CombineCategoryNameList(facadeOptions.FilterCategoryNames)), FacadeCategory.Info);
                    break;
            }

            EnsureTestRunnerIsCreated();
            return testRunner.Run(testListener, assemblyPath, cref, facadeOptions);
        }
        protected override FacadeTestRunState RunImpl(IFacadeTestListener testListener, string assemblyPath, string cref, FacadeOptions facadeOptions)
        {
            if (cref == null)
                return RunAssembly(testListener, assemblyPath, facadeOptions);

            if (cref.Length >= 2)
            {
                char descriptor = cref[0];

                switch (descriptor)
                {
                    case 'T':
                        return RunType(testListener, assemblyPath, cref.Substring(2), facadeOptions);

                    case 'N':
                        return RunNamespace(testListener, assemblyPath, cref.Substring(2), facadeOptions);

                    case 'M':
                    case 'F':
                    case 'P':
                    case 'E':
                        int paramsPos = cref.IndexOf('(');
                        if (paramsPos < 0)
                            paramsPos = cref.Length;

                        string memberNameWithType = cref.Substring(2, paramsPos - 2);
                        int memberPos = memberNameWithType.LastIndexOf('.');
                        if (memberPos < 0)
                            break;

                        string typeName = memberNameWithType.Substring(0, memberPos);
                        string memberName = memberNameWithType.Substring(memberPos + 1);
                        return RunMember(testListener, assemblyPath, typeName, memberName, facadeOptions);
                }
            }

            return FacadeTestRunState.NoTests;
        }
        public void RunReturnsCorrectResultCode(int resultCode, FacadeTestRunState expectedRunState)
        {
            StubbedLocalTestRunner tr = new StubbedLocalTestRunner();

            Assembly assembly = typeof(TDNetAddInTests).Assembly;
            string assemblyPath = AssemblyUtils.GetAssemblyLocalPath(assembly);
            tr.SetRunLauncherAction(delegate
            {
                TestLauncherResult result = new TestLauncherResult(new Report());
                result.SetResultCode(resultCode);
                return result;
            });

            FacadeOptions facadeOptions = new FacadeOptions();
            FacadeTestRunState runResult = tr.Run(MockRepository.GenerateStub<IFacadeTestListener>(), assemblyPath, null, facadeOptions);
            Assert.AreEqual(expectedRunState, runResult);
        }
        public void RunWithExclusionCategoryFilterPassesCorrectOptionsToTheLauncher()
        {
            StubbedLocalTestRunner tr = new StubbedLocalTestRunner();

            Assembly assembly = typeof(TDNetAddInTests).Assembly;
            string assemblyPath = AssemblyUtils.GetAssemblyLocalPath(assembly);
            string @namespace = "Foo";

            tr.SetRunLauncherAction(delegate(TestLauncher launcher)
            {
                AssertTestLauncherOptions(launcher, AssemblyUtils.GetAssemblyLocalPath(assembly),
                    string.Format("exclude Category: {0}, {1} include Namespace: {2}", "abc", "def", @namespace));

                return new TestLauncherResult(new Report());
            });

            FacadeOptions facadeOptions = new FacadeOptions()
            {
                FilterCategoryMode = FacadeFilterCategoryMode.Exclude,
                FilterCategoryNames = new string[] { "abc", "def" }
            };
            tr.Run(MockRepository.GenerateStub<IFacadeTestListener>(), assemblyPath, "N:" + @namespace, facadeOptions);
        }
        public void RunNamespacePassesCorrectOptionsToTheLauncher()
        {
            StubbedLocalTestRunner tr = new StubbedLocalTestRunner();

            Assembly assembly = typeof(TDNetAddInTests).Assembly;
            string assemblyPath = AssemblyUtils.GetAssemblyLocalPath(assembly);
            string @namespace = "Foo";

            tr.SetRunLauncherAction(delegate(TestLauncher launcher)
            {
                AssertTestLauncherOptions(launcher, AssemblyUtils.GetAssemblyLocalPath(assembly),
                    string.Format("Namespace: {0}", @namespace));

                return new TestLauncherResult(new Report());
            });

            FacadeOptions facadeOptions = new FacadeOptions();
            tr.Run(MockRepository.GenerateStub<IFacadeTestListener>(), assemblyPath, "N:" + @namespace, facadeOptions);
        }
        public void RunMemberWithMethodPassesCorrectOptionsToTheLauncher()
        {
            StubbedLocalTestRunner tr = new StubbedLocalTestRunner();

            MethodBase method = Reflector.GetExecutingFunction().Resolve(true);
            Assembly assembly = method.DeclaringType.Assembly;
            string assemblyPath = AssemblyUtils.GetAssemblyLocalPath(assembly);

            tr.SetRunLauncherAction(delegate(TestLauncher launcher)
            {
                AssertTestLauncherOptions(launcher, AssemblyUtils.GetAssemblyLocalPath(assembly),
                    string.Format("(Type: {0} and Member: {1})", method.DeclaringType.FullName, method.Name));

                return new TestLauncherResult(new Report());
            });

            FacadeOptions facadeOptions = new FacadeOptions();
            tr.Run(MockRepository.GenerateStub<IFacadeTestListener>(), assemblyPath,
                "M:" + method.DeclaringType.FullName + "." + method.Name, facadeOptions);
        }
        public void RunMemberWithTypePassesCorrectOptionsToTheLauncher()
        {
            StubbedLocalTestRunner tr = new StubbedLocalTestRunner();

            Type type = typeof(TDNetAddInTests);
            Assembly assembly = type.Assembly;
            string assemblyPath = AssemblyUtils.GetAssemblyLocalPath(assembly);

            tr.SetRunLauncherAction(delegate(TestLauncher launcher)
            {
                AssertTestLauncherOptions(launcher, assemblyPath,
                    string.Format("Type: {0}", type.FullName));

                return new TestLauncherResult(new Report());
            });

            FacadeOptions facadeOptions = new FacadeOptions();
            tr.Run(MockRepository.GenerateStub<IFacadeTestListener>(), assemblyPath, "T:" + type.FullName, facadeOptions);
        }
        public void RunAssemblyPassesCorrectOptionsToTheLauncher()
        {
            StubbedLocalTestRunner tr = new StubbedLocalTestRunner();

            Assembly assembly = typeof(TDNetAddInTests).Assembly;
            string assemblyPath = AssemblyUtils.GetAssemblyLocalPath(assembly);

            tr.SetRunLauncherAction(delegate(TestLauncher launcher)
            {
                AssertTestLauncherOptions(launcher, assemblyPath, "*");

                return new TestLauncherResult(new Report());
            });

            FacadeOptions facadeOptions = new FacadeOptions();
            tr.Run(MockRepository.GenerateStub<IFacadeTestListener>(), assemblyPath, null, facadeOptions);
        }
 protected abstract FacadeTestRunState RunImpl(IFacadeTestListener testListener, string assemblyPath, string cref, FacadeOptions facadeOptions);
 private FacadeTestRunState RunAssembly(IFacadeTestListener testListener, string assemblyPath, FacadeOptions facadeOptions)
 {
     return Run(testListener, assemblyPath, new AnyFilter<ITestDescriptor>(), facadeOptions);
 }
        private FacadeTestRunState Run(IFacadeTestListener testListener, string assemblyPath, Filter<ITestDescriptor> filter, FacadeOptions facadeOptions)
        {
            if (testListener == null)
                throw new ArgumentNullException(@"testListener");
            if (assemblyPath == null)
                throw new ArgumentNullException("assemblyPath");
            if (facadeOptions == null)
                throw new ArgumentNullException("facadeOptions");

            ILogger logger = new FilteredLogger(new TDNetLogger(testListener), LogSeverity.Info);

            try
            {
                RuntimeAccessor.Instance.AddLogListener(logger);
                var filterRules = new List<FilterRule<ITestDescriptor>>();

                switch (facadeOptions.FilterCategoryMode)
                {
                    case FacadeFilterCategoryMode.Disabled:
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Inclusion, filter));
                        break;

                    case FacadeFilterCategoryMode.Include:
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Inclusion,
                            new AndFilter<ITestDescriptor>(new[] { filter, ToCategoryFilter(facadeOptions.FilterCategoryNames) })));
                        break;

                    case FacadeFilterCategoryMode.Exclude:
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Exclusion, ToCategoryFilter(facadeOptions.FilterCategoryNames)));
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Inclusion, filter));
                        break;
                }

                var filterSet = new FilterSet<ITestDescriptor>(filterRules);
                launcher.Logger = logger;
                launcher.ProgressMonitorProvider = new LogProgressMonitorProvider(logger);
                launcher.TestExecutionOptions.FilterSet = filterSet;
                launcher.TestProject.TestRunnerFactoryName = StandardTestRunnerFactoryNames.IsolatedAppDomain;
                launcher.TestProject.AddTestRunnerExtension(new TDNetExtension(testListener)); // This monitor will inform the user in real-time what's going on
                launcher.TestProject.TestPackage.AddFile(new FileInfo(assemblyPath));
                string assemblyDirectory = Path.GetDirectoryName(assemblyPath);
                launcher.TestProject.TestPackage.ApplicationBaseDirectory = new DirectoryInfo(assemblyDirectory);
                launcher.TestProject.TestPackage.WorkingDirectory = new DirectoryInfo(assemblyDirectory);
                TestLauncherResult result = RunLauncher(launcher);
                string reportDirectory = GetReportDirectory(logger);

                if (reportDirectory != null)
                {
                    var reportFormatterOptions = new ReportFormatterOptions();
                    var preferenceManager = (TDNetPreferenceManager)RuntimeAccessor.ServiceLocator.ResolveByComponentId("TDNetRunner.PreferenceManager");
                    var reportFormat = preferenceManager.ReportSettings.DetermineReportFormat(result.Report);
                    result.GenerateReports(reportDirectory, Path.GetFileName(assemblyPath), ReportArchive.Normal,
                        new[] { reportFormat }, reportFormatterOptions,
                        RuntimeAccessor.ServiceLocator.Resolve<IReportManager>(), NullProgressMonitor.CreateInstance());

                    // This will generate a link to the generated report
                    if (result.ReportDocumentPaths.Count != 0)
                    {
                        Uri rawUrl = new Uri(result.ReportDocumentPaths[0]);
                        string displayUrl = "file:///" + rawUrl.LocalPath.Replace(" ", "%20").Replace(@"\", "/");

                        // TDNet just prints the link on its own but it's not always clear to users what it represents.
                        // testListener.TestResultsUrl(displayUrl);
                        testListener.WriteLine("\nTest Report: " + displayUrl, FacadeCategory.Info);
                    }
                }

                // Inform no tests run, if necessary.
                if (result.ResultCode == ResultCode.NoTests)
                {
                    InformNoTestsWereRun(testListener, Resources.MbUnitTestRunner_NoTestsFound);
                }
                else if (result.Statistics.TestCount == 0)
                {
                    InformNoTestsWereRun(testListener, null);
                }

                return GetTestRunState(result);
            }
            finally
            {
                RuntimeAccessor.Instance.RemoveLogListener(logger);
            }
        }
 private FacadeTestRunState RunMember(IFacadeTestListener testListener, string assemblyPath, string typeName, string memberName, FacadeOptions facadeOptions)
 {
     return Run(testListener, assemblyPath, new AndFilter<ITestDescriptor>(new Filter<ITestDescriptor>[] 
     { 
         new TypeFilter<ITestDescriptor>(new EqualityFilter<string>(typeName), true), 
         new MemberFilter<ITestDescriptor>(new EqualityFilter<string>(memberName)) 
     }), facadeOptions);
 }
 private FacadeTestRunState RunNamespace(IFacadeTestListener testListener, string assemblyPath, string @namespace, FacadeOptions facadeOptions)
 {
     return Run(testListener, assemblyPath, new AndFilter<ITestDescriptor>(new Filter<ITestDescriptor>[]
     {
         new NamespaceFilter<ITestDescriptor>(new EqualityFilter<string>(@namespace))
     }), facadeOptions);
 }