/// <summary> /// Reads the configuration setup. /// </summary> /// <param name="project">The project.</param> /// <param name="nUnitParameters">The n unit parameters.</param> /// <returns></returns> public DistributedConfigurationSetup ReadConfigurationSetup(TestProject project, NUnitParameters nUnitParameters) { var result = new DistributedConfigurationSetup(); var distributedConfigurationFileNames = GetDistributedConfigurationFileNames(project, nUnitParameters); foreach (var distributedConfigurationFileName in distributedConfigurationFileNames) { if (string.IsNullOrEmpty(distributedConfigurationFileName)) continue; FillSetupInstance(distributedConfigurationFileName, result); } return result; }
/// <summary> /// Parses the specified project. /// </summary> /// <param name="project">The project.</param> /// <param name="request">The request.</param> /// <returns></returns> public IList<TestUnitWithMetadata> Get(TestProject project, TestRunRequest request) { initializer.Initialize(); var projectFileNameOnly = Path.GetFileName(request.TestRun.NUnitParameters.AssembliesToTest[0]); var projectFileNameMapped = Path.Combine(project.Path, projectFileNameOnly); var package = GetTestPackage(projectFileNameMapped, request.TestRun.NUnitParameters); package.AutoBinPath = false; package.PrivateBinPath = NUnit.Util.DomainManager.GetPrivateBinPath( parameters.RootFolder ?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), new ArrayList(request.TestRun.NUnitParameters.AssembliesToTest) { Assembly.GetExecutingAssembly().Location }); package.BasePath = parameters.RootFolder; var domainManager = new NUnit.Util.DomainManager(); // A separate domain is used to free the assemblies after tests retrieval var domain = domainManager.CreateDomain(package); try { DomainManager.AddResolverForPaths(domain, DomainManager.GetNUnitFolders(parameters)); var testsRetrieverType = typeof (InAnotherDomainTestsRetriever); var testsRetriever = (InAnotherDomainTestsRetriever) domain.CreateInstanceAndUnwrap(testsRetrieverType.Assembly.FullName, testsRetrieverType.FullName); return testsRetriever.Get(package, request.TestRun); } finally { DomainManager.UnloadDomain(domain); } }
/// <summary> /// Gets the substituted configuration file. /// </summary> /// <param name="project">The project.</param> /// <param name="nUnitParameters">The n unit parameters.</param> /// <param name="configurationSubstitutions">The configuration substitutions.</param> /// <returns></returns> public string GetSubstitutedConfigurationFile(TestProject project, NUnitParameters nUnitParameters, DistributedConfigurationSubstitutions configurationSubstitutions) { var distributedConfigurationFileNames = GetDistributedConfigurationFileNames(project, nUnitParameters); if (configurationSubstitutions == null || configurationSubstitutions.Variables.Count == 0) return distributedConfigurationFileNames.FirstOrDefault(); string finalConfigurationFile = null; int hashCode = configurationSubstitutions.GetHashCode(); foreach (var configurationFileName in distributedConfigurationFileNames) { var configurationDocument = XDocument.Load(configurationFileName); SubstitutePlaceHolders(configurationDocument, configurationSubstitutions); var substitutedFileName = Path.ChangeExtension(configurationFileName, string.Format(".{0}.config", hashCode)); configurationDocument.Save(substitutedFileName); if (finalConfigurationFile == null) finalConfigurationFile = substitutedFileName; } return finalConfigurationFile; }
private IEnumerable<string> GetDistributedConfigurationFileNames(TestProject project, NUnitParameters nUnitParameters) { if (nUnitParameters.AssembliesToTest.Count == 1) { var localName = Path.Combine(project.Path, Path.GetFileName(nUnitParameters.AssembliesToTest[0])); if (NUnitProject.IsNUnitProjectFile(localName) && File.Exists(localName)) { var nUnitProject = new NUnitProject(localName); nUnitProject.Load(); var matchingConfig = nUnitProject.Configs.Cast<ProjectConfig>().Where(c => c.Name.Equals(nUnitParameters.Configuration)).FirstOrDefault(); if (matchingConfig == null) { var configFileNamedAsProject = Path.ChangeExtension(localName, ".config"); if (File.Exists(configFileNamedAsProject)) yield return configFileNamedAsProject; yield break; } yield return matchingConfig.ConfigurationFilePath; yield break; } } foreach (var assembly in nUnitParameters.AssembliesToTest) { var localName = Path.Combine(project.Path, Path.GetFileName(assembly)); string possibleConfigName = null; if (File.Exists(possibleConfigName = localName + ".config")) yield return possibleConfigName; else if (File.Exists(possibleConfigName = Path.ChangeExtension(localName, ".config"))) yield return possibleConfigName; } }
/// <summary> /// Gets the NUnit test result. /// </summary> /// <param name="test">The test.</param> /// <param name="project">The project.</param> /// <param name="configurationSubstitutions">The configuration substitutions.</param> /// <param name="isChild">if set to <c>true</c> [is child].</param> /// <returns></returns> public TestResult GetNUnitTestResult(TestUnit test, TestProject project, DistributedConfigurationSubstitutions configurationSubstitutions, bool isChild = false) { var nativeRunner = runnerCache.GetOrLoad(test.Run, configurationSubstitutions, () => { initializer.Initialize(); string configurationFileName = configurationOperator.GetSubstitutedConfigurationFile(project, test.Run. NUnitParameters, configurationSubstitutions); var mappedAssemblyFile = Path.Combine(project.Path, Path.GetFileName( test.Run.NUnitParameters. AssembliesToTest[0])); TestPackage package; if (!NUnitProject.IsNUnitProjectFile(mappedAssemblyFile)) package = new TestPackage(mappedAssemblyFile); else { var nunitProject = new NUnitProject(mappedAssemblyFile); nunitProject.Load(); if (!string.IsNullOrEmpty(test.Run.NUnitParameters.Configuration)) nunitProject.SetActiveConfig(test.Run.NUnitParameters.Configuration); package = nunitProject.ActiveConfig.MakeTestPackage(); } package.Settings["ShadowCopyFiles"] = true; package.AutoBinPath = true; if (!string.IsNullOrEmpty(configurationFileName)) { package.ConfigurationFile = configurationFileName; } var nativeTestRunner = new NDistribUnitProcessRunner(log); nativeTestRunner.Load(package); return nativeTestRunner; }); var testOptions = test.Run.NUnitParameters; TestResult testResult = null; try { Action runTest = ()=> { try { testResult = nativeRunner.Run(new NullListener(), new NUnitTestsFilter( testOptions.IncludeCategories, testOptions.ExcludeCategories, test.UniqueTestId).NativeFilter); nativeRunner.CleanupAfterRun(); } //TODO: remove this. This is for tracking purposes only catch(AppDomainUnloadedException ex) { log.Warning("AppDomainUnloadedException is still being thrown", ex); if (!isChild) { runnerCache.Remove(test.Run, configurationSubstitutions); testResult = GetNUnitTestResult(test, project, configurationSubstitutions, isChild: true); } else throw; } }; if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA && !Thread.CurrentThread.TrySetApartmentState(ApartmentState.STA)) { var thread = new Thread(()=> exceptionCatcher.Run(runTest)); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); } else { runTest(); } } catch (Exception ex) { log.Error("Error while running test on agent", ex); throw; } return testResult; }
public void Init() { retriever = new TestsRetriever(new NUnitInitializer(), new BootstrapperParameters(), new ConsoleLog()); string targetAssembly = typeof(TestFixtureWithCategoriesOnTests).Assembly.Location; project = new TestProject(Path.GetDirectoryName(targetAssembly)); request = new TestRunRequest(new TestRun { NUnitParameters = new NUnitParameters() }); request.TestRun.NUnitParameters.AssembliesToTest.Add(targetAssembly); }