Beispiel #1
0
        /// <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();
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
		/// <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();
								}
							}
						}
					}
				}
			}
		}