/// <summary> /// Run all methods in each of the listed assemblies /// </summary> public void Run(IORMToolServices services, IORMToolTestSuiteReport suiteReport) { suiteReport.BeginSuite(myName); IList <ReportAssembly> allAssemblies = myAssemblies; if (allAssemblies == null) { return; } int assemblyCount = allAssemblies.Count; for (int i = 0; i < assemblyCount; ++i) { ReportAssembly testAssembly = allAssemblies[i]; Assembly resolvedAssembly = testAssembly.Assembly; suiteReport.BeginTestAssembly(testAssembly.Location, resolvedAssembly == null); if (resolvedAssembly != null) { Type[] types = resolvedAssembly.GetTypes(); int typeCount = types.Length; for (int j = 0; j < typeCount; ++j) { Type type = types[j]; if (0 != type.GetCustomAttributes(typeof(ORMTestFixtureAttribute), true).Length) { RunTests(type, services, suiteReport); } } } } }
private static int Main(string[] args) { string suiteFile = args[0]; FileInfo suiteFileInfo = new FileInfo(suiteFile); string fullName = suiteFileInfo.FullName; string extension = suiteFileInfo.Extension; XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.CloseInput = false; using (FileStream fileStream = suiteFileInfo.OpenRead()) { XmlTextReader suitesReader = new XmlTextReader(new StreamReader(fileStream)); using (XmlReader reader = XmlReader.Create(suitesReader, readerSettings)) { reader.MoveToContent(); string LoadingSchemaNamespace = reader.NamespaceURI; ORMSuiteReportResult result = ORMSuiteReportResult.NoFailure; if (LoadingSchemaNamespace == SchemaNamespace) { //If the suite is not a report then we need to generate a report. IList <Suite> suites = Suite.LoadSuiteFile(suiteFile); if (suites != null) { int suiteCount = suites.Count; IORMToolServices services = Suite.CreateServices(); XmlWriterSettings reportSettings = new XmlWriterSettings(); reportSettings.Indent = true; reportSettings.IndentChars = "\t"; using (XmlWriter reportWriter = XmlTextWriter.Create(string.Concat(fullName.Substring(0, fullName.Length - extension.Length), ".Report", extension), reportSettings)) { IORMToolTestSuiteReport report = ((IORMToolTestSuiteReportFactory)services.ServiceProvider.GetService(typeof(IORMToolTestSuiteReportFactory))).Create(reportWriter); try { for (int i = 0; i < suiteCount; ++i) { suites[i].Run(services, report); } } finally { result = report.CloseSuiteReport(); } } } } else if (LoadingSchemaNamespace == ReportSchemaNamespace) { //TODO: where to go with a report } return((int)result); } } }
/// <summary> /// Run all tests for the given type that match the /// category filters specified for this suite /// </summary> /// <param name="testType">A type with a Test attribute</param> /// <param name="services">Services used to run the test. IORMToolTestServices can /// be retrieved from services.ServiceProvider.</param> /// <param name="suiteReport">The suite report callback</param> private void RunTests(Type testType, IORMToolServices services, IORMToolTestSuiteReport suiteReport) { IORMToolTestServices testServices = (IORMToolTestServices)services.ServiceProvider.GetService(typeof(IORMToolTestServices)); object testTypeInstance = null; object[] methodParams = null; MethodInfo[] methods = testType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); int methodCount = methods.Length; for (int i = 0; i < methodCount; ++i) { MethodInfo method = methods[i]; object[] testAttributes = method.GetCustomAttributes(typeof(ORMTestAttribute), false); Debug.Assert(testAttributes.Length < 2, "Single use attribute with inherit=false, should only pick up zero or one attributes"); // Make sure that the method is flagged as a Test method that can be run per the current category settings if (testAttributes.Length == 0 || !CheckCategoryFilters((ORMTestAttribute)testAttributes[0])) { continue; } // Make sure we've instantiated the test class if (testTypeInstance == null) { ConstructorInfo constructor; if (null != (constructor = testType.GetConstructor(new Type[] { typeof(IORMToolServices) }))) { testTypeInstance = constructor.Invoke(new object[] { services }); methodParams = new object[1]; } bool loadFailure = null == testTypeInstance; suiteReport.BeginTestClass(testType.Namespace, testType.Name, loadFailure); if (loadFailure) { return; } } ParameterInfo[] methodParamInfos = method.GetParameters(); if (!(methodParamInfos.Length == 1 && typeof(Store).IsAssignableFrom(methodParamInfos[0].ParameterType))) { // The test method does not match the signature we need, it should // not have been marked with the Test attribute suiteReport.ReportTestResults(method.Name, ORMTestResult.FailBind, null); } else { Store store = null; try { // Prepare the test services for a new test testServices.OpenReport(); // Populate a store. Automatically loads the starting // file from the test assembly if one is provided store = testServices.Load(method, null, null); // Run the method methodParams[0] = store; method.Invoke(testTypeInstance, methodParams); // Compare the current contents of the store with the // expected state testServices.Compare(store, method, null); testServices.LogValidationErrors(null); } finally { if (store != null) { ((IDisposable)store).Dispose(); } // Close the report and see if the report matches the expected results using (XmlReader reportReader = testServices.CloseReport(method)) { string methodName = method.Name; string resourceName = string.Concat(testType.FullName, ".", methodName, ".Report.xml"); Stream baselineStream = null; try { Assembly testAssembly = testType.Assembly; // Get the baseline that we're comparing to if (null != testAssembly.GetManifestResourceInfo(resourceName)) { baselineStream = testAssembly.GetManifestResourceStream(resourceName); } if (baselineStream != null) { bool hasDiff = false; // See if the data is different. XmlDiff diff = DiffEngine; XmlReaderSettings readerSettings = DetachableReaderSettings; XmlWriterSettings writerSettings = DetachableWriterSettings; using (MemoryStream diffStream = new MemoryStream()) { using (XmlReader baselineReader = XmlReader.Create(baselineStream, readerSettings)) { using (XmlWriter diffWriter = XmlWriter.Create(diffStream, writerSettings)) { hasDiff = !diff.Compare(baselineReader, reportReader, diffWriter); } } if (hasDiff) { // Record the diffgram in the suite report diffStream.Seek(0, SeekOrigin.Begin); using (XmlReader diffReader = XmlTextReader.Create(diffStream, readerSettings)) { suiteReport.ReportTestResults(methodName, ORMTestResult.FailReportDiffgram, diffReader); } } else { // Record a passing result suiteReport.ReportTestResults(methodName, ORMTestResult.Pass, null); } } } else { // Record the full report, we have no baseline to compare against suiteReport.ReportTestResults(methodName, ORMTestResult.FailReportBaseline, reportReader); } } finally { if (baselineStream != null) { ((IDisposable)baselineStream).Dispose(); } } } } } } }
/// <summary> /// Run all methods in each of the listed assemblies /// </summary> public void Run(IORMToolServices services, IORMToolTestSuiteReport suiteReport) { suiteReport.BeginSuite(myName); IList<ReportAssembly> allAssemblies = myAssemblies; if (allAssemblies == null) { return; } int assemblyCount = allAssemblies.Count; for (int i = 0; i < assemblyCount; ++i) { ReportAssembly testAssembly = allAssemblies[i]; Assembly resolvedAssembly = testAssembly.Assembly; suiteReport.BeginTestAssembly(testAssembly.Location, resolvedAssembly == null); if (resolvedAssembly != null) { Type[] types = resolvedAssembly.GetTypes(); int typeCount = types.Length; for (int j = 0; j < typeCount; ++j) { Type type = types[j]; if (0 != type.GetCustomAttributes(typeof(ORMTestFixtureAttribute), true).Length) { RunTests(type, services, suiteReport); } } } } }