// run a set of test private static FullRunDescription RunFailingTests(bool log) { var report = new FullRunDescription(); // get all test fixtures foreach (var type in typeof(GenerateErrorMessages).GetTypeInfo().Assembly.GetExportedTypes()) { try { // enumerate testmethods with expectedexception attribute with an FluentException type var tests = type.GetMethods() .Where(method => method.GetCustomAttributes(typeof(TestAttribute), false).Any()) .Where( method => { //var attributes = method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false); //if (attributes.Length == 1) //{ // var attrib = attributes[0] as ExpectedExceptionAttribute; // return attrib == null || attrib.ExpectedException == typeof(FluentCheckException); //} throw new NotImplementedException("we have to find a new way since NUnit's ExceptedException has disapeared."); }); RunnerHelper.RunThoseTests(tests, type, report, log); } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}" + Environment.NewLine + "{1}", type.FullName, e)); } } return(report); }
public void ScanAssembliesForCheckAndGenerateReport() { var report = new FullRunDescription(); // scan all assemblies foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { // get all test fixtures foreach (var type in assembly.GetTypes() .Where( type => ((type.GetInterface("IForkableFluentAssertion") != null && (type.Attributes & TypeAttributes.Abstract) == 0) || type.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0) && ((type.Attributes & TypeAttributes.Public) == TypeAttributes.Public))) { try { // enumerate public methods IEnumerable <MethodInfo> tests = type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly) .Where( method => method.MemberType == MemberTypes.Method && ((method.Attributes & MethodAttributes.SpecialName) == 0)); var publicMethods = tests as IList <MethodInfo> ?? tests.ToList(); if (publicMethods.Count > 0) { // run all tests foreach (var checkMethod in publicMethods) { CheckDescription desc = RunnerHelper.AnalyzeSignature(checkMethod); if (desc != null) { RunnerHelper.Log(string.Format("Method :{0}.{1}({2})", type.Name, checkMethod.Name, desc.CheckedType.Name)); report.AddEntry(desc); } } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } } const string Name = "FluentChecks.xml"; report.Save(Name); Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name))); }
public void ScanAssembliesForCheckAndGenerateReport() { var report = new FullRunDescription(); // scan all assemblies foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { // get all test fixtures foreach (var type in assembly.GetTypes() .Where( type => ((type.GetInterface("IForkableFluentAssertion") != null && (type.Attributes & TypeAttributes.Abstract) == 0) || type.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0) && ((type.Attributes & TypeAttributes.Public) == TypeAttributes.Public))) { try { // enumerate public methods IEnumerable<MethodInfo> tests = type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly) .Where( method => method.MemberType == MemberTypes.Method && ((method.Attributes & MethodAttributes.SpecialName) == 0)); var publicMethods = tests as IList<MethodInfo> ?? tests.ToList(); if (publicMethods.Count > 0) { // run all tests foreach (var checkMethod in publicMethods) { CheckDescription desc = RunnerHelper.AnalyzeSignature(checkMethod); if (desc != null) { RunnerHelper.Log(string.Format("Method :{0}.{1}({2})", type.Name, checkMethod.Name, desc.CheckedType.Name)); report.AddEntry(desc); } } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } } const string Name = "FluentChecks.xml"; report.Save(Name); Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name))); }
public static void RunThoseTests(IEnumerable<MethodInfo> tests, Type type, FullRunDescription report, bool log) { var specificTests = tests as IList<MethodInfo> ?? tests.ToList(); if (specificTests.Count <= 0) { return; } Log(string.Format("TestFixture :{0}", type.FullName)); #if !CORE // creates an instance var test = type.InvokeMember( string.Empty, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { }); // run TestFixtureSetup RunAllMethodsWithASpecificAttribute(type, typeof(TestFixtureSetUpAttribute), test); try { // run all tests foreach (var specificTest in specificTests.Where( specificTest => specificTest.GetCustomAttributes(typeof(ExplicitAttribute), false).Length == 0 && specificTest.GetCustomAttributes(typeof(IgnoreAttribute), false).Length == 0)) { RunAllMethodsWithASpecificAttribute(type, typeof(SetUpAttribute), test); try { // we have to caputre eceptions RunMethod(specificTest, test, report, log); } finally { RunAllMethodsWithASpecificAttribute(type, typeof(TearDownAttribute), test); } } } finally { RunAllMethodsWithASpecificAttribute(type, typeof(TestFixtureTearDownAttribute), test); } #endif }
public static void RunThoseTests(IEnumerable <MethodInfo> tests, Type type, FullRunDescription report, bool log) { var specificTests = tests as IList <MethodInfo> ?? tests.ToList(); if (specificTests.Count <= 0) { return; } Log(string.Format("TestFixture :{0}", type.FullName)); // creates an instance var test = type.InvokeMember( string.Empty, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { }); // run TestFixtureSetup RunAllMethodsWithASpecificAttribute(type, typeof(TestFixtureSetUpAttribute), test); try { // run all tests foreach (var specificTest in specificTests.Where( specificTest => specificTest.GetCustomAttributes(typeof(ExplicitAttribute), false).Length == 0 && specificTest.GetCustomAttributes(typeof(IgnoreAttribute), false).Length == 0)) { RunAllMethodsWithASpecificAttribute(type, typeof(SetUpAttribute), test); try { // we have to caputre eceptions RunMethod(specificTest, test, report, log); } finally { RunAllMethodsWithASpecificAttribute(type, typeof(TearDownAttribute), test); } } } finally { RunAllMethodsWithASpecificAttribute(type, typeof(TestFixtureTearDownAttribute), test); } }
public void ScanUnitTestsAndGenerateReport() { var report = new FullRunDescription(); // get all test fixtures foreach ( var type in Assembly.GetExecutingAssembly() .GetTypes()) { try { // enumerate testmethods with expectedexception attribute with an FluentException type IEnumerable <MethodInfo> tests = type.GetMethods() .Where(method => method.GetCustomAttributes(typeof(TestAttribute), false).Length > 0) .Where( method => { object[] attributes = method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false); if (attributes.Length == 1) { var attrib = attributes[0] as ExpectedExceptionAttribute; return(attrib == null || attrib.ExpectedException == typeof(FluentAssertionException)); } return(false); }); RunnerHelper.RunThoseTests(tests, type, report); } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } const string Name = "FluentReport.xml"; report.Save(Name); Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name))); }
public static void RunThoseTests(IEnumerable <MethodInfo> tests, Type type, FullRunDescription report, bool log) { var specificTests = tests as IList <MethodInfo> ?? tests.ToList(); if (specificTests.Count <= 0) { return; } Log($"TestFixture :{type.FullName}"); var constructor = type.GetTypeInfo().GetConstructor(new Type[0]); // creates an instance var test = constructor?.Invoke(new object[0]); // run TestFixtureSetup RunAllMethodsWithASpecificAttribute(type, typeof(OneTimeSetUpAttribute), test); try { // run all tests foreach (var specificTest in specificTests.Where( specificTest => !specificTest.GetCustomAttributes(typeof(ExplicitAttribute), false).Any() && !specificTest.GetCustomAttributes(typeof(IgnoreAttribute), false).Any())) { RunAllMethodsWithASpecificAttribute(type, typeof(SetUpAttribute), test); try { // we have to capture exceptions RunMethod(specificTest, test, report, log); } finally { RunAllMethodsWithASpecificAttribute(type, typeof(TearDownAttribute), test); } } } finally { RunAllMethodsWithASpecificAttribute(type, typeof(OneTimeTearDownAttribute), test); } }
// run a set of test private static FullRunDescription RunFailingTests(bool log) { var report = new FullRunDescription(); // get all test fixtures foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { try { // enumerate testmethods with expectedexception attribute with an FluentException type var tests = type.GetMethods() .Where(method => method.GetCustomAttributes(typeof(TestAttribute), false).Length > 0) .Where( method => { var attributes = method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false); if (attributes.Length == 1) { var attrib = attributes[0] as ExpectedExceptionAttribute; return(attrib == null || attrib.ExpectedException == typeof(FluentCheckException)); } return(false); }); RunnerHelper.RunThoseTests(tests, type, report, log); } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } return(report); }
public static void RunMethod(MethodInfo specificTest, object test, FullRunDescription report) { try { specificTest.Invoke(test, new object[] { }); } catch (Exception e) { if (specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false).Length == 0) { throw; } Type expectedType = ((ExpectedExceptionAttribute) specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false)[0]).ExpectedException; if (e.InnerException != null) { if (e.InnerException is FluentAssertionException) { var fluExc = e.InnerException as FluentAssertionException; Type testedtype; CheckDescription desc = GetCheckAndType(fluExc); MethodBase method = desc.Check; testedtype = desc.CheckedType; desc.ErrorSampleMessage = fluExc.Message; // are we building a report if (report != null) { Log( string.Format( "Check.That({1} sut).{0} failure message\n****\n{2}\n****", method.Name, testedtype.Name, fluExc.Message)); report.AddEntry(desc); } return; } if (report != null) { Log( string.Format( "{0} did not generate a FLUENT ASSERTION:\n{1}", specificTest.Name, e.InnerException.Message)); } if (e.InnerException.GetType() != expectedType && expectedType != null) { throw; } } else { if (report != null) { Log(string.Format("{0} failed to run:\n{1}", specificTest.Name, e)); } throw; } } }
private static void RunMethod(MethodBase specificTest, object test, FullRunDescription report, bool log) { try { specificTest.Invoke(test, new object[] { }); } catch (Exception e) { if (specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false).Length == 0) { if (CheckContext.DefaulNegated == false) { return; } throw; } Type expectedType = ((ExpectedExceptionAttribute) specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false)[0]).ExpectedException; if (e.InnerException != null) { if (e.InnerException is FluentCheckException) { var fluExc = e.InnerException as FluentCheckException; var desc = GetCheckAndType(fluExc); if (desc != null) { var method = desc.Check; var testedtype = desc.CheckedType; desc.ErrorSampleMessage = fluExc.Message; // are we building a report if (log) { Log( string.Format( "Check.That({1} sut).{0} failure message\n****\n{2}\n****", method.Name, testedtype.Name, fluExc.Message)); } if (report != null) { report.AddEntry(desc); } if (CheckContext.DefaulNegated == false) { Log(string.Format("(Forced) Negated test '{0}' should have succeeded, but it failed (method {1}).", specificTest.Name, desc.Signature)); } } else { Log(string.Format("Failed to parse the method signature {0}", specificTest.Name)); } return; } if (report != null) { Log( string.Format( "{0} did not generate a fluent check:\n{1}", specificTest.Name, e.InnerException.Message)); } if (e.InnerException.GetType() != expectedType && expectedType != null) { throw; } } else { if (report != null) { Log(string.Format("{0} failed to run:\n{1}", specificTest.Name, e)); } throw; } } }
// run a set of test private static FullRunDescription RunFailingTests(bool log) { var report = new FullRunDescription(); // get all test fixtures foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { try { // enumerate testmethods with expectedexception attribute with an FluentException type var tests = type.GetMethods() .Where(method => method.GetCustomAttributes(typeof(TestAttribute), false).Length > 0) .Where( method => { var attributes = method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false); if (attributes.Length == 1) { var attrib = attributes[0] as ExpectedExceptionAttribute; return attrib == null || attrib.ExpectedException == typeof(FluentCheckException); } return false; }); RunnerHelper.RunThoseTests(tests, type, report, log); } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } return report; }
public void ScanAssembliesForCheckAndGenerateReport() { var report = new FullRunDescription(); // scan all assemblies foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (assembly.GetName().Name == "MonoDevelop.NUnit") { // skip MonoDevelop.NUnit assembly because GetTypes fails continue; } try { // get all test fixtures foreach (var type in assembly.GetTypes() .Where( type => ((type.GetInterface("IForkableCheck") != null && (type.Attributes & TypeAttributes.Abstract) == 0) || type.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0) && ((type.Attributes & TypeAttributes.Public) == TypeAttributes.Public))) { try { // enumerate public methods IEnumerable<MethodInfo> tests = type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly) .Where( method => method.MemberType == MemberTypes.Method && ((method.Attributes & MethodAttributes.SpecialName) == 0)); var publicMethods = tests as IList<MethodInfo> ?? tests.ToList(); if (publicMethods.Count > 0) { // scan all methods foreach (var checkMethod in publicMethods) { try { if (checkMethod.Name == "ForkInstance") { // skip forkinstance continue; } var desc = CheckDescription.AnalyzeSignature(checkMethod); if (desc != null) { if (desc.CheckedType == null) { RunnerHelper.Log(string.Format("Failed to identify checked type on test {0}", checkMethod.Name)); } else { RunnerHelper.Log(string.Format("Method :{0}.{1}({2})", type.Name, checkMethod.Name, desc.CheckedType.Name)); report.AddEntry(desc); } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while assessing test {2} on type:{0}\n{1}", type.FullName, e, checkMethod.Name)); } } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on assembly:{0}\n{1}", assembly.FullName, e)); throw new ApplicationException(string.Format("Exception while working on assembly:{0}\n{1}", assembly.FullName, e)); } } // xml save const string Name = "FluentChecks.xml"; report.Save(Name); const string Name2 = "FluentChecks.csv"; // csv file using (var writer = new StreamWriter(Name2, false)) { foreach (var typeChecks in report.RunDescription) { foreach (var checkList in typeChecks.Checks) { foreach (var signature in checkList.CheckSignatures) { var message = string.Format( "{0};{1};{2}", typeChecks.CheckedType.TypeToStringProperlyFormated(true), checkList.CheckName, signature.Signature); writer.WriteLine(message); } } } } Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name))); Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name2))); }
private static void RunMethod(MethodBase specificTest, object test, FullRunDescription report, bool log) { try { specificTest.Invoke(test, new object[] { }); } catch (Exception e) { throw; // throw new NotImplementedException("we have to find a new way since NUnit's ExceptedException has disapeared."); //if (specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false).Length == 0) //{ // if (CheckContext.DefaulNegated == false) // { // return; // } // throw; //} //Type expectedType = ((ExpectedExceptionAttribute) specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false)[0]).ExpectedException; //if (e.InnerException != null) //{ // if (e.InnerException is FluentCheckException) // { // var fluExc = e.InnerException as FluentCheckException; // var desc = GetCheckAndType(fluExc); // if (desc != null) // { // var method = desc.Check; // var testedtype = desc.CheckedType; // desc.ErrorSampleMessage = fluExc.Message; // // are we building a report // if (log) // { // Log( // string.Format( // "Check.That({1} sut).{0} failure message" + Environment.NewLine + "****" + Environment.NewLine + "{2}" + Environment.NewLine + "****", // method.Name, // testedtype.Name, // fluExc.Message)); // } // if (report != null) // { // report.AddEntry(desc); // } // if (CheckContext.DefaulNegated == false) // { // Log(string.Format("(Forced) Negated test '{0}' should have succeeded, but it failed (method {1}).", specificTest.Name, desc.Signature)); // } // } // else // { // Log(string.Format("Failed to parse the method signature {0}", specificTest.Name)); // } // return; // } // if (report != null) // { // Log( // string.Format( // "{0} did not generate a fluent check:" + Environment.NewLine + "{1}", // specificTest.Name, // e.InnerException.Message)); // } // if (e.InnerException.GetType() != expectedType && expectedType != null) // { // throw; // } //} // else // { // if (report != null) // { // Log(string.Format("{0} failed to run:" + Environment.NewLine + "{1}", specificTest.Name, e)); // } // throw; // } } }
public void ScanUnitTestsAndGenerateReport() { var report = new FullRunDescription(); // get all test fixtures foreach ( var type in Assembly.GetExecutingAssembly() .GetTypes()) { try { // enumerate testmethods with expectedexception attribute with an FluentException type IEnumerable<MethodInfo> tests = type.GetMethods() .Where(method => method.GetCustomAttributes(typeof(TestAttribute), false).Length > 0) .Where( method => { object[] attributes = method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false); if (attributes.Length == 1) { var attrib = attributes[0] as ExpectedExceptionAttribute; return attrib == null || attrib.ExpectedException == typeof(FluentAssertionException); } return false; }); RunnerHelper.RunThoseTests(tests, type, report); } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } const string Name = "FluentReport.xml"; report.Save(Name); Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name))); }
public void ScanAssembliesForCheckAndGenerateReport() { var report = new FullRunDescription(); // scan all assemblies foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (assembly.GetName().Name == "MonoDevelop.NUnit") { // skip MonoDevelop.NUnit assembly because GetTypes fails continue; } try { // get all test fixtures foreach (var type in assembly.GetTypes() .Where( type => ((type.GetInterface("IForkableCheck") != null && (type.Attributes & TypeAttributes.Abstract) == 0) || type.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0) && ((type.Attributes & TypeAttributes.Public) == TypeAttributes.Public))) { try { // enumerate public methods IEnumerable <MethodInfo> tests = type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly) .Where( method => method.MemberType == MemberTypes.Method && ((method.Attributes & MethodAttributes.SpecialName) == 0)); var publicMethods = tests as IList <MethodInfo> ?? tests.ToList(); if (publicMethods.Count > 0) { // scan all methods foreach (var checkMethod in publicMethods) { try { if (checkMethod.Name == "ForkInstance") { // skip forkinstance continue; } var desc = CheckDescription.AnalyzeSignature(checkMethod); if (desc != null) { if (desc.CheckedType == null) { RunnerHelper.Log(string.Format("Failed to identify checked type on test {0}", checkMethod.Name)); } else { RunnerHelper.Log(string.Format("Method :{0}.{1}({2})", type.Name, checkMethod.Name, desc.CheckedType.Name)); report.AddEntry(desc); } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while assessing test {2} on type:{0}\n{1}", type.FullName, e, checkMethod.Name)); } } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e)); } } } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on assembly:{0}\n{1}", assembly.FullName, e)); throw new ApplicationException(string.Format("Exception while working on assembly:{0}\n{1}", assembly.FullName, e)); } } // xml save const string Name = "FluentChecks.xml"; report.Save(Name); const string Name2 = "FluentChecks.csv"; // csv file using (var writer = new StreamWriter(Name2, false)) { foreach (var typeChecks in report.RunDescription) { foreach (var checkList in typeChecks.Checks) { foreach (var signature in checkList.CheckSignatures) { var message = string.Format( "{0};{1};{2}", typeChecks.CheckedType.TypeToStringProperlyFormated(true), checkList.CheckName, signature.Signature); writer.WriteLine(message); } } } } Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name))); Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name2))); }
// run a set of test private static FullRunDescription RunFailingTests(bool log) { var report = new FullRunDescription(); #if !CORE // get all test fixtures foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { try { // enumerate testmethods with expectedexception attribute with an FluentException type var tests = type.GetMethods() .Where(method => method.GetCustomAttributes(typeof(TestAttribute), false).Length > 0) .Where( method => { //var attributes = method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false); //if (attributes.Length == 1) //{ // var attrib = attributes[0] as ExpectedExceptionAttribute; // return attrib == null || attrib.ExpectedException == typeof(FluentCheckException); //} throw new NotImplementedException("we have to find a new way since NUnit's ExceptedException has disapeared."); return false; }); RunnerHelper.RunThoseTests(tests, type, report, log); } catch (Exception e) { RunnerHelper.Log(string.Format("Exception while working on type:{0}" + Environment.NewLine + "{1}", type.FullName, e)); } } #endif return report; }