public static void Succeed(params Action[] assertions) { var errors = new List<Exception>(); foreach (var assertion in assertions) try { assertion(); } catch (Exception ex) { errors.Add(ex); } if (errors.Any()) { var ex = new AssertionException( string.Join(Environment.NewLine, errors.Select(e => e.Message)), errors.First()); // Use stack trace from the first exception to ensure first failed Assert is one click away ReplaceStackTrace(ex, errors.First().StackTrace); throw ex; } }
private void AddRemove(int startKey, string s) { try { AddRemoveKeyValues(startKey, s, _count/_numOfThreads); } catch (AssertionException e) { error = e; throw; } }
public void FailGivenInnerExceptionsShouldThrowAggregateAssertionException() { var exception = new AssertionException("Test Message"); try { Specify.Failure(new[] { exception }); } catch (AggregateAssertionException e) { Assert.AreSame(exception, e.InnerException); Assert.AreSame(exception, e.InnerExceptions.FirstOrDefault()); return; } Assert.Fail("Specify.Failure did not throw an AggregateAssertFailedException"); }
public void FailGivenInnerExceptionsAndMessageShouldThrowAggregateAssertionExceptionWithMessage() { var message = "Test Message"; var exception = new AssertionException(message); try { Specify.Failure(new[] { exception }, message); } catch (AggregateAssertionException e) { StringAssert.Contains(message, e.Message); Assert.AreSame(exception, e.InnerException); Assert.AreSame(exception, e.InnerExceptions.FirstOrDefault()); return; } Assert.Fail("Specify.Failure did not throw an AggregateAssertFailedException"); }
public void PrevPageButtonTest(string url) { GoTo(string.Format("{0}{1}", url, "/1")); var nextPageButton = _browser.FindElementById("nextPage"); if (nextPageButton != null) { UrlTest(string.Format("{0}{1}", url, "/2")); var prevPage = _browser.FindElementById("prevPage"); if (prevPage != null) { UrlTest(string.Format("{0}{1}", url, "/1")); } else { var err = new AssertionException("There is no prevPageButton in page."); throw err; } } }
/** * Useful to keep output concise when expecting many failures to be reported by this Test case */ private static void printshortStackTrace(TextWriter ps, AssertionException e) { ps.WriteLine(e.Message); ps.WriteLine(e.StackTrace); /*StackTraceElement[] stes = e.GetStackTrace(); int startIx = 0; // skip any top frames inside junit.framework.Assert while(startIx<stes.Length) { if(!stes[startIx].GetClassName().Equals(typeof(Assert).Name)) { break; } startIx++; } // skip bottom frames (part of junit framework) int endIx = startIx+1; while(endIx < stes.Length) { if (stes[endIx].GetClassName().Equals(typeof(Assert).Name)) { break; } endIx++; } if(startIx >= endIx) { // something went wrong. just print the whole stack trace e.printStackTrace(ps); } endIx -= 4; // skip 4 frames of reflection invocation ps.println(e.ToString()); for(int i=startIx; i<endIx; i++) { ps.println("\tat " + stes[i].ToString()); }*/ }
static void Main2() { // why is this here? because some dumbass forgot to install a decent test-runner before going to the airport var epicFail = new List<string>(); var testTypes = from type in typeof(Program).Assembly.GetTypes() where Attribute.IsDefined(type, typeof(TestFixtureAttribute)) && !Attribute.IsDefined(type, typeof(IgnoreAttribute)) let methods = type.GetMethods() select new { Type = type, Methods = methods, ActiveMethods = methods.Where(x => Attribute.IsDefined(x, typeof(ActiveTestAttribute))).ToArray(), Setup = methods.SingleOrDefault(x => Attribute.IsDefined(x, typeof(TestFixtureSetUpAttribute))), TearDown = methods.SingleOrDefault(x => Attribute.IsDefined(x, typeof(TestFixtureTearDownAttribute))) }; int pass = 0, fail = 0; bool activeOnly = testTypes.SelectMany(x => x.ActiveMethods).Any(); TaskScheduler.UnobservedTaskException += (sender, args) => { args.SetObserved(); //if (args.Exception is AggregateException) //{ // foreach (var ex in ((AggregateException)args.Exception).InnerExceptions) // { // Console.WriteLine(ex.Message); // } //} //else //{ // Console.WriteLine(args.Exception.Message); //} }; foreach (var type in testTypes) { var tests = (from method in (activeOnly ? type.ActiveMethods : type.Methods) where Attribute.IsDefined(method, typeof(TestAttribute)) && !Attribute.IsDefined(method, typeof(IgnoreAttribute)) select method).ToArray(); if (tests.Length == 0) continue; Console.WriteLine(type.Type.FullName); object obj; try { obj = Activator.CreateInstance(type.Type); if (obj == null) throw new InvalidOperationException("the world has gone mad"); } catch (Exception ex) { Console.WriteLine(ex.Message); continue; } using (obj as IDisposable) { if (type.Setup != null) { try { type.Setup.Invoke(obj, null); } catch (Exception ex) { Console.WriteLine("Test fixture startup failed: " + ex.Message); fail++; epicFail.Add(type.Setup.DeclaringType.FullName + "." + type.Setup.Name); continue; } } foreach (var test in tests) { var expectedFail = Attribute.GetCustomAttribute(test, typeof(ExpectedExceptionAttribute)) as ExpectedExceptionAttribute; Console.Write(test.Name + ": "); Exception err = null; try { int count = 1; if (activeOnly) { var ata = test.GetCustomAttribute(typeof(ActiveTestAttribute)) as ActiveTestAttribute; if (ata != null) count = ata.Count; } while (count-- > 0) { test.Invoke(obj, null); } } catch (TargetInvocationException ex) { err = ex.InnerException; } catch (Exception ex) { err = ex; } if (err is AggregateException && ((AggregateException)err).InnerExceptions.Count == 1) { err = ((AggregateException)err).InnerExceptions[0]; } if (expectedFail != null) { if (err == null) { err = new NUnit.Framework.AssertionException("failed to fail"); } else { int issues = 0; if (expectedFail.ExpectedException != null && !expectedFail.ExpectedException.IsAssignableFrom(err.GetType())) { issues++; } if (expectedFail.ExpectedExceptionName != null && err.GetType().FullName != expectedFail.ExpectedExceptionName) { issues++; } if (expectedFail.ExpectedMessage != null && err.Message != expectedFail.ExpectedMessage) { issues++; } if (issues == 0) err = null; else { err = new InvalidOperationException("Failed in a different way", err); } } } if (err == null) { Console.WriteLine("pass"); pass++; } else { Console.WriteLine(err.Message); fail++; epicFail.Add(test.DeclaringType.FullName + "." + test.Name); } } if (type.TearDown != null) { try { type.TearDown.Invoke(obj, null); } catch (Exception ex) { Console.WriteLine("Test fixture teardown failed: " + ex.Message); fail++; epicFail.Add(type.TearDown.DeclaringType.FullName + "." + type.TearDown.Name); } } } } Console.WriteLine("Passed: {0}; Failed: {1}", pass, fail); foreach (var msg in epicFail) Console.WriteLine(msg); //#if DEBUG // Console.WriteLine(); // Console.WriteLine("Callbacks: {0:###,###,##0} sync, {1:###,###,##0} async", // BookSleeve.RedisConnectionBase.AllSyncCallbacks, BookSleeve.RedisConnectionBase.AllAsyncCallbacks); //#endif }
static void Main2() { // why is this here? because some dumbass forgot to install a decent test-runner before going to the airport var epicFail = new List <string>(); var testTypes = from type in typeof(Program).Assembly.GetTypes() where Attribute.IsDefined(type, typeof(TestFixtureAttribute)) && !Attribute.IsDefined(type, typeof(IgnoreAttribute)) let methods = type.GetMethods() select new { Type = type, Methods = methods, ActiveMethods = methods.Where(x => Attribute.IsDefined(x, typeof(ActiveTestAttribute))).ToArray(), Setup = methods.SingleOrDefault(x => Attribute.IsDefined(x, typeof(TestFixtureSetUpAttribute))), TearDown = methods.SingleOrDefault(x => Attribute.IsDefined(x, typeof(TestFixtureTearDownAttribute))) }; int pass = 0, fail = 0; bool activeOnly = testTypes.SelectMany(x => x.ActiveMethods).Any(); TaskScheduler.UnobservedTaskException += (sender, args) => { args.SetObserved(); //if (args.Exception is AggregateException) //{ // foreach (var ex in ((AggregateException)args.Exception).InnerExceptions) // { // Console.WriteLine(ex.Message); // } //} //else //{ // Console.WriteLine(args.Exception.Message); //} }; foreach (var type in testTypes) { var tests = (from method in (activeOnly ? type.ActiveMethods : type.Methods) where Attribute.IsDefined(method, typeof(TestAttribute)) && !Attribute.IsDefined(method, typeof(IgnoreAttribute)) select method).ToArray(); if (tests.Length == 0) { continue; } Console.WriteLine(type.Type.FullName); object obj; try { obj = Activator.CreateInstance(type.Type); if (obj == null) { throw new InvalidOperationException("the world has gone mad"); } } catch (Exception ex) { Console.WriteLine(ex.Message); continue; } using (obj as IDisposable) { if (type.Setup != null) { try { type.Setup.Invoke(obj, null); } catch (Exception ex) { Console.WriteLine("Test fixture startup failed: " + ex.Message); fail++; epicFail.Add(type.Setup.DeclaringType.FullName + "." + type.Setup.Name); continue; } } foreach (var test in tests) { var expectedFail = Attribute.GetCustomAttribute(test, typeof(ExpectedExceptionAttribute)) as ExpectedExceptionAttribute; Console.Write(test.Name + ": "); Exception err = null; try { int count = 1; if (activeOnly) { var ata = test.GetCustomAttribute(typeof(ActiveTestAttribute)) as ActiveTestAttribute; if (ata != null) { count = ata.Count; } } while (count-- > 0) { test.Invoke(obj, null); } } catch (TargetInvocationException ex) { err = ex.InnerException; } catch (Exception ex) { err = ex; } if (err is AggregateException && ((AggregateException)err).InnerExceptions.Count == 1) { err = ((AggregateException)err).InnerExceptions[0]; } if (expectedFail != null) { if (err == null) { err = new NUnit.Framework.AssertionException("failed to fail"); } else { int issues = 0; if (expectedFail.ExpectedException != null && !expectedFail.ExpectedException.IsAssignableFrom(err.GetType())) { issues++; } if (expectedFail.ExpectedExceptionName != null && err.GetType().FullName != expectedFail.ExpectedExceptionName) { issues++; } if (expectedFail.ExpectedMessage != null && err.Message != expectedFail.ExpectedMessage) { issues++; } if (issues == 0) { err = null; } else { err = new InvalidOperationException("Failed in a different way", err); } } } if (err == null) { Console.WriteLine("pass"); pass++; } else { Console.WriteLine(err.Message); fail++; epicFail.Add(test.DeclaringType.FullName + "." + test.Name); } } if (type.TearDown != null) { try { type.TearDown.Invoke(obj, null); } catch (Exception ex) { Console.WriteLine("Test fixture teardown failed: " + ex.Message); fail++; epicFail.Add(type.TearDown.DeclaringType.FullName + "." + type.TearDown.Name); } } } } Console.WriteLine("Passed: {0}; Failed: {1}", pass, fail); foreach (var msg in epicFail) { Console.WriteLine(msg); } #if DEBUG Console.WriteLine(); Console.WriteLine("Callbacks: {0:###,###,##0} sync, {1:###,###,##0} async", BookSleeve.RedisConnectionBase.AllSyncCallbacks, BookSleeve.RedisConnectionBase.AllAsyncCallbacks); #endif }
private void AvoidUnusedVariableCompilerWarning(AssertionException e) { string msg = e.Message; }
/** * Useful to keep output concise when expecting many failures to be reported by this test case */ private static void printshortStackTrace(TextWriter ps, AssertionException e) { //StackTraceElement[] stes = e.StackTrace; //int startIx = 0; //// skip any top frames inside junit.framework.Assert //while (startIx < stes.Length) //{ // if (!stes[startIx].ClassName.Equals(typeof(Assert).Name)) // { // break; // } // startIx++; //} //// skip bottom frames (part of junit framework) //int endIx = startIx + 1; //while (endIx < stes.Length) //{ // if (stes[endIx].ClassName.Equals(typeof(TestCase).Name)) // { // break; // } // endIx++; //} //if (startIx >= endIx) //{ // // something went wrong. just print the whole stack trace // e.PrintStackTrace(ps); //} //endIx -= 4; // skip 4 frames of reflection invocation //ps.Println(e.ToString()); //for (int i = startIx; i < endIx; i++) //{ // ps.Println("\tat " + stes[i].ToString()); //} }