/// <summary> /// Adds a test file to the package configuration, if not already added. /// </summary> /// <param name="file">The file to add.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="file"/> is null.</exception> public void AddFile(FileInfo file) { if (file == null) { throw new ArgumentNullException("file"); } testPackage.AddFile(file); }
protected TestModel PopulateTestTree(Assembly assembly) { TestModel testModel = new TestModel(); var testFrameworkManager = RuntimeAccessor.ServiceLocator.Resolve <ITestFrameworkManager>(); var logger = new MarkupStreamLogger(TestLog.Default); var testFrameworkSelector = new TestFrameworkSelector() { Filter = testFrameworkHandle => testFrameworkHandle.Id == TestFrameworkHandle.Id, FallbackMode = TestFrameworkFallbackMode.Strict }; ITestDriver testDriver = testFrameworkManager.GetTestDriver(testFrameworkSelector, logger); var testIsolationProvider = (ITestIsolationProvider)RuntimeAccessor.ServiceLocator.ResolveByComponentId("Gallio.LocalTestIsolationProvider"); var testIsolationOptions = new TestIsolationOptions(); using (ITestIsolationContext testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, logger)) { var testPackage = new TestPackage(); testPackage.AddFile(new FileInfo(AssemblyUtils.GetFriendlyAssemblyCodeBase(assembly))); var testExplorationOptions = new TestExplorationOptions(); var messageSink = TestModelSerializer.CreateMessageSinkToPopulateTestModel(testModel); new LogProgressMonitorProvider(logger).Run(progressMonitor => { testDriver.Explore(testIsolationContext, testPackage, testExplorationOptions, messageSink, progressMonitor); }); } return(testModel); }
private void RunAllTests(IRunContext runContext) { ITestRunnerManager runnerManager = RuntimeAccessor.ServiceLocator.Resolve <ITestRunnerManager>(); var runner = runnerManager.CreateTestRunner(StandardTestRunnerFactoryNames.IsolatedAppDomain); runner.RegisterExtension(new RunContextExtension(runContext)); ILogger logger = new RunContextLogger(runContext); TestRunnerOptions testRunnerOptions = new TestRunnerOptions(); try { RunWithProgressMonitor(delegate(IProgressMonitor progressMonitor) { runner.Initialize(testRunnerOptions, logger, progressMonitor); }); if (isCanceled) { return; } TestPackage testPackage = new TestPackage(); testPackage.AddExcludedTestFrameworkId("MSTestAdapter.TestFramework"); foreach (ITestElement testElement in runContext.RunConfig.TestElements) { GallioTestElement gallioTestElement = testElement as GallioTestElement; if (gallioTestElement != null) { testPackage.AddFile(new FileInfo(gallioTestElement.AssemblyPath)); } } TestExplorationOptions testExplorationOptions = new TestExplorationOptions(); TestExecutionOptions testExecutionOptions = new TestExecutionOptions(); List <Filter <string> > idFilters = new List <Filter <string> >(); foreach (ITestElement includedTestElement in runContext.RunConfig.TestElements) { GallioTestElement gallioTestElement = includedTestElement as GallioTestElement; if (gallioTestElement != null) { idFilters.Add(new EqualityFilter <string>(gallioTestElement.GallioTestId)); } } testExecutionOptions.FilterSet = new FilterSet <ITestDescriptor>(new IdFilter <ITestDescriptor>(new OrFilter <string>(idFilters))); RunWithProgressMonitor(delegate(IProgressMonitor progressMonitor) { runner.Run(testPackage, testExplorationOptions, testExecutionOptions, progressMonitor); }); } finally { runner.Dispose(NullProgressMonitor.CreateInstance()); } }
public void AddFiles(IProgressMonitor progressMonitor, IList <string> files) { using (progressMonitor.BeginTask(Resources.AddingFiles, (files.Count + 2))) { var validFiles = GetValidFiles(progressMonitor, files); GenericCollectionUtils.ForEach(validFiles, x => TestPackage.AddFile(new FileInfo(x))); projectTreeModel.NotifyTestProjectChanged(); progressMonitor.Worked(1); fileWatcher.Add(validFiles); } }
/// <summary> /// Initializes a test package with the contents of this structure. /// </summary> /// <param name="testPackage">The test package to populate.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="testPackage"/> is null.</exception> public void InitializeTestPackage(TestPackage testPackage) { if (testPackage == null) { throw new ArgumentNullException("testPackage"); } GenericCollectionUtils.ForEach(files, x => testPackage.AddFile(new FileInfo(x))); GenericCollectionUtils.ForEach(hintDirectories, x => testPackage.AddHintDirectory(new DirectoryInfo(x))); GenericCollectionUtils.ForEach(excludedFrameworkIds, x => testPackage.AddExcludedTestFrameworkId(x)); testPackage.ShadowCopy = ShadowCopy; testPackage.DebuggerSetup = Debug ? new DebuggerSetup() : null; testPackage.ApplicationBaseDirectory = ApplicationBaseDirectory != null ? new DirectoryInfo(ApplicationBaseDirectory) : null; testPackage.WorkingDirectory = WorkingDirectory != null ? new DirectoryInfo(WorkingDirectory) : null; testPackage.RuntimeVersion = RuntimeVersion; GenericCollectionUtils.ForEach(Properties, x => testPackage.AddProperty(x.Key, x.Value)); }
public void Run_Test() { var progressMonitor = MockProgressMonitor.Instance; var filter = new FilterSet <ITestDescriptor>(new NoneFilter <ITestDescriptor>()); filterService.Stub(fs => fs.GenerateFilterSetFromSelectedTests()).Return(filter); optionsController.Stub(oc => oc.TestRunnerExtensions).Return(new BindingList <string>(new List <string>())); StubTestRunnerFactory(); var testPackage = new TestPackage(); testPackage.AddFile(new FileInfo("test")); testController.SetTestPackage(testPackage); testController.Run(false, progressMonitor, new List <string>()); testRunner.AssertWasCalled(tr => tr.Run(Arg <TestPackage> .Matches(tpc => tpc.Files.Count == 1), Arg <TestExplorationOptions> .Is.Anything, Arg <TestExecutionOptions> .Matches(teo => ((teo.FilterSet == filter) && !teo.ExactFilter)), Arg.Is(progressMonitor))); }
private List <Test> getTests(string assembly) { var testIsolationProvider = (ITestIsolationProvider)RuntimeAccessor.ServiceLocator.ResolveByComponentId("Gallio.LocalTestIsolationProvider"); var testIsolationOptions = new TestIsolationOptions(); ITestIsolationContext testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, _logger); // Create a test package. // You can set a whole bunch of options here. var testPackage = new TestPackage(); testPackage.AddFile(new FileInfo(assembly)); testPackage.TestFrameworkFallbackMode = TestFrameworkFallbackMode.Strict; var testExplorationOptions = new TestExplorationOptions(); // This query you can answer by exploring tests and looking at their metadata for "TestsOn". // You can explore tests using the Explore method of TestDriver. It sends a bunch of // messages to an IMessageSink just like Run. Just scan these messages for tests that // match the pattern you like. // Alternately, you can build a TestModel once up front and traverse the resulting test tree // to find what you need. The Explore method of testDriver is just like Run, except that // it does not run the tests. // TestModelSerializer is a little helper we can use to build up a TestModel from test messages on the fly. // The TestModel is just a tree of test metadata. Pretty straightforward. TestModel testModel = new TestModel(); IMessageSink messageSink = TestModelSerializer.CreateMessageSinkToPopulateTestModel(testModel); var logProgressMonitorProvider = new LogProgressMonitorProvider(_logger); logProgressMonitorProvider.Run((progressMonitor) => { _testDriver.Explore(testIsolationContext, testPackage, testExplorationOptions, messageSink, progressMonitor); }); return(testModel.AllTests.Where(x => x.IsTestCase).ToList()); }
private FacadeTaskResult RunTests() { var logger = new FacadeLoggerWrapper(facadeLogger); var runner = TestRunnerUtils.CreateTestRunnerByName(StandardTestRunnerFactoryNames.IsolatedAppDomain); // Set parameters. var testPackage = new TestPackage(); foreach (var assemblyLocation in assemblyLocations) { testPackage.AddFile(new FileInfo(assemblyLocation)); } testPackage.ShadowCopy = facadeTaskExecutorConfiguration.ShadowCopy; if (facadeTaskExecutorConfiguration.AssemblyFolder != null) { testPackage.ApplicationBaseDirectory = new DirectoryInfo(facadeTaskExecutorConfiguration.AssemblyFolder); testPackage.WorkingDirectory = new DirectoryInfo(facadeTaskExecutorConfiguration.AssemblyFolder); } var testRunnerOptions = new TestRunnerOptions(); var testExplorationOptions = new TestExplorationOptions(); var filters = GenericCollectionUtils.ConvertAllToArray <string, Filter <string> >(explicitTestIds, testId => new EqualityFilter <string>(testId)); var filterSet = new FilterSet <ITestDescriptor>(new IdFilter <ITestDescriptor>(new OrFilter <string>(filters))); var testExecutionOptions = new TestExecutionOptions { FilterSet = filterSet }; // Install the listeners. runner.Events.TestStepStarted += TestStepStarted; runner.Events.TestStepFinished += TestStepFinished; runner.Events.TestStepLifecyclePhaseChanged += TestStepLifecyclePhaseChanged; // Run the tests. try { try { runner.Initialize(testRunnerOptions, logger, CreateProgressMonitor()); Report report = runner.Run(testPackage, testExplorationOptions, testExecutionOptions, CreateProgressMonitor()); if (sessionId != null) { SessionCache.SaveSerializedReport(sessionId, report); } return(FacadeTaskResult.Success); } catch (Exception ex) { if (sessionId != null) { SessionCache.ClearSerializedReport(sessionId); } logger.Log(LogSeverity.Error, "A fatal exception occurred during test execution.", ex); return(FacadeTaskResult.Exception); } finally { SubmitFailureForRemainingPendingTasks(); } } finally { runner.Dispose(CreateProgressMonitor()); } }
/// <summary> /// Initializes a test package with the contents of this structure. /// </summary> /// <param name="testPackage">The test package to populate.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="testPackage"/> is null.</exception> public void InitializeTestPackage(TestPackage testPackage) { if (testPackage == null) throw new ArgumentNullException("testPackage"); GenericCollectionUtils.ForEach(files, x => testPackage.AddFile(new FileInfo(x))); GenericCollectionUtils.ForEach(hintDirectories, x => testPackage.AddHintDirectory(new DirectoryInfo(x))); GenericCollectionUtils.ForEach(excludedFrameworkIds, x => testPackage.AddExcludedTestFrameworkId(x)); testPackage.ShadowCopy = ShadowCopy; testPackage.DebuggerSetup = Debug ? new DebuggerSetup() : null; testPackage.ApplicationBaseDirectory = ApplicationBaseDirectory != null ? new DirectoryInfo(ApplicationBaseDirectory) : null; testPackage.WorkingDirectory = WorkingDirectory != null ? new DirectoryInfo(WorkingDirectory) : null; testPackage.RuntimeVersion = RuntimeVersion; GenericCollectionUtils.ForEach(Properties, x => testPackage.AddProperty(x.Key, x.Value)); }
public IEnumerable <AutoTest.TestRunners.Shared.Results.TestResult> Run(RunSettings settings) { if (!_isInitialized) { return new AutoTest.TestRunners.Shared.Results.TestResult[] { getNotInitializedResult(settings) } } ; var tests = settings.Assembly.Tests.ToList(); var members = settings.Assembly.Members.ToList(); var namespaces = settings.Assembly.Namespaces.ToList(); var runAll = namespaces.Count == 0 && members.Count == 0 && tests.Count == 0; var steps = new List <TestStepData>(); var testResults = new List <AutoTest.TestRunners.Shared.Results.TestResult>(); // Get a test isolation context. Here we want to run tests in the same AppDomain. var testIsolationProvider = (ITestIsolationProvider)RuntimeAccessor.ServiceLocator.ResolveByComponentId("Gallio.LocalTestIsolationProvider"); var testIsolationOptions = new TestIsolationOptions(); ITestIsolationContext testIsolationContext = testIsolationProvider.CreateContext(testIsolationOptions, _logger); var testPackage = new TestPackage(); testPackage.AddFile(new FileInfo(settings.Assembly.Assembly)); testPackage.TestFrameworkFallbackMode = TestFrameworkFallbackMode.Strict; // Create some test exploration options. Nothing interesting for you here, probably. var testExplorationOptions = new TestExplorationOptions(); var messageSink = new MessageConsumer() .Handle <TestStepStartedMessage>((message) => { steps.Add(message.Step); }) .Handle <TestStepFinishedMessage>(message => { var test = steps.FirstOrDefault(x => x.Id.Equals(message.StepId) && x.IsTestCase); if (test == null) { return; } var fixture = string.Format("{0}.{1}", test.CodeReference.NamespaceName, steps.First(x => x.Id.Equals(test.ParentId)).Name); testResults.Add(new AutoTest.TestRunners.Shared.Results.TestResult( "MbUnit", settings.Assembly.Assembly, fixture, message.Result.Duration.TotalMilliseconds, string.Format("{0}.{1}", fixture, test.Name), convertState(message.Result.Outcome.Status), message.Result.Outcome.DisplayName)); }); // Provide a progress monitor. var logProgressMonitorProvider = new LogProgressMonitorProvider(_logger); var options = new TestExecutionOptions(); options.FilterSet = new Gallio.Model.Filters.FilterSet <ITestDescriptor>(new OrFilter <ITestDescriptor>(getTestFilter(namespaces, members, tests))); // Run the tests. logProgressMonitorProvider.Run((progressMonitor) => { _testDriver.Run(testIsolationContext, testPackage, testExplorationOptions, options, messageSink, progressMonitor); }); return(testResults); }