public void AddingMethodReport() { Type type = typeof(TestClass); MethodInfo succeededMethod = type.GetMethod("TestName"); MethodInfo failedMethod = type.GetMethod("SecondTest"); ClassReport report = new SimpleClassReport(type); MethodReport methodReport1 = new MethodReport(succeededMethod); report.Add(methodReport1); AssertEx.That(report.Status is ReportStatus.Success, Is.True()); AssertEx.That(report.SucceededTests, Is.EqualTo(1)); AssertEx.That(report.RunnedTests, Is.EqualTo(1)); MethodReport methodReport2 = new MethodReport(failedMethod, new AssertException("Exception Text")); report.Add(methodReport2); AssertEx.That(report.Status is ReportStatus.Failed, Is.True()); AssertEx.That(report.FailedTests, Is.EqualTo(1)); AssertEx.That(report.RunnedTests, Is.EqualTo(2)); //This tests, that new reports of the same method will be replaced MethodReport methodReport3 = new MethodReport(succeededMethod, new NullReferenceException()); report.Add(methodReport3); AssertEx.That(report.Status is ReportStatus.Error, Is.True()); AssertEx.That(report.FailedTests, Is.EqualTo(2)); AssertEx.That(report.RunnedTests, Is.EqualTo(2)); //Reports for methods of another class aren't allowed MethodInfo methodInfo = typeof(AnotherTestClass).GetMethod("AnotherTest"); MethodReport reportOfMethodNotInClass = new MethodReport(methodInfo); AssertEx.That(() => report.Add(reportOfMethodNotInClass), Throws.An<ArgumentException>()); }
private static string CreateReportText(MethodReport report) { StringBuilder builder = new StringBuilder(report.Text); builder.Append("\n\n"); builder.Append(report.RaisedException.StackTrace); return builder.ToString(); }
public void EqualsAndHashCode() { Report report = new MethodReport(typeof(TestClass).GetMethod("TestName"), new AssertException("Exception test.")); //Using IsTrue/IsFalse to cover all paths (aren't covered, when using Equals) //Equal tests Assert.IsTrue(report.Equals(report), "An object should allways be equal to itself (reference)."); AssertEx.That(report.GetHashCode(), Is.EqualTo(report.GetHashCode()), "Equal objects should have equal hashcodes."); Report equal = new MethodReport(typeof(TestClass).GetMethod("TestName"), new AssertException("Exception test.")); Assert.IsTrue(report.Equals(equal), "An object should be equal to an object with the same attributes."); AssertEx.That(report.GetHashCode(), Is.EqualTo(equal.GetHashCode()), "Equal objects should have equal hashcodes."); //Not equal tests Assert.IsFalse(report.Equals(null), "An object shouldn't be equal to null."); object unequal = new object(); Assert.IsFalse(report.Equals(unequal), "An object shouldn't be equal to an object of an other type."); AssertEx.That(report.GetHashCode(), Is.Not(unequal.GetHashCode()), "Unequal objects shouldn't have equal hashcodes."); unequal = new MethodReport(typeof(TestClass).GetMethod("UnequalMethod"), new AssertException("Exception test.")); Assert.IsFalse(report.Equals(unequal), "An object shouldn't be equal to an object of an other type."); AssertEx.That(report.GetHashCode(), Is.Not(unequal.GetHashCode()), "Unequal objects shouldn't have equal hashcodes."); unequal = new MethodReport(typeof(TestClass).GetMethod("TestName"), new NullReferenceException("Exception test.")); Assert.IsFalse(report.Equals(unequal), "An object shouldn't be equal to an object with different attributes."); AssertEx.That(report.GetHashCode(), Is.Not(unequal.GetHashCode()), "Unequal objects shouldn't have equal hashcodes."); }
public void RunSpecificTestOfJUUTTestClass() { TestClassSession session = new TestClassSession(typeof(TestClassMock)); session.Add(typeof(TestClassMock).GetMethod("Foo")); TestRunner runner = new SimpleTestRunner(); ClassReport classReport = runner.Run(session); AssertThatTheMethodsAreCalledInTheCorrectOrderAfterRunningASpecificTest(); //Checking the returned test report Report report = GetFirstMethodReportFrom(classReport.MethodReports); Report expectedReport = new MethodReport(typeof(TestClassMock).GetMethod("Foo")); AssertEx.That(report, Is.EqualTo(expectedReport)); }
public void MessageCreation() { MethodInfo testMethod = typeof(TestClass).GetMethod("TestName"); Exception raisedException = new AssertException("Exception Text"); Report report = new MethodReport(testMethod, raisedException); AssertEx.That(report.Text, Is.EqualTo("The TestName-Method failed: " + raisedException.Message)); raisedException = new NullReferenceException("Null reference"); report = new MethodReport(testMethod, raisedException); AssertEx.That(report.Text, Is.EqualTo("The TestName-Method threw an unexpected exception: " + raisedException.Message)); report = new MethodReport(testMethod); AssertEx.That(report.Text, Is.EqualTo("The TestName-Method passed successfully.")); }
public void Creation() { MethodInfo testMethod = typeof(TestClass).GetMethod("TestName"); Exception raisedException = new AssertException("Exception Text"); Report report = new MethodReport(testMethod, raisedException); AssertEx.That(report.TestClass, Is.EqualTo(typeof(TestClass))); AssertEx.That(report.Status is ReportStatus.Failed, Is.True()); //Check that a report can be created with other JUUTMethodAttributes report = new MethodReport(typeof(TestClass).GetMethod("SetUp"), raisedException); //This allows to create a test method report without a raised exception report = new MethodReport(testMethod); AssertEx.That(report.TestClass, Is.EqualTo(typeof(TestClass))); AssertEx.That(report.Status is ReportStatus.Success, Is.True()); testMethod = new DynamicMethod("Foo", null, null); AssertEx.That(() => { new MethodReport(testMethod, raisedException); }, Throws.An<ArgumentException>()); AssertEx.That(() => { new MethodReport(testMethod); }, Throws.An<ArgumentException>()); AssertEx.That(() => { new MethodReport(null, raisedException); }, Throws.An<ArgumentException>()); AssertEx.That(() => { new MethodReport(null); }, Throws.An<ArgumentException>()); }
public void Add(MethodReport report) { if (report.TestClass != TestClass) { throw new ArgumentException("The declaring type of the given report isn't equal to the declaring type of this class report."); } Status = report.Status.IsWorseThan(Status) ? report.Status : Status; AdjustCounters(report); Reports[report.Method] = report; }
private void AdjustCounters(MethodReport newReport) { if (Reports.ContainsKey(newReport.Method)) { Reports[newReport.Method].Status.DecrementCounterFor(this); } newReport.Status.IncrementCounterFor(this); }
public void RunTestsOfJUUTTestClassWithFailingClassSetUp() { TestClassSession session = new TestClassSession(typeof(TestClassMockWithFailingClassSetUp)); session.Add(typeof(TestClassMockWithFailingClassSetUp).GetMethod("Bar")); //Testing the run of a specific testMethod TestRunner runner = new SimpleTestRunner(); ClassReport classReport = runner.Run(session); AssertThatTheMethodsAreCalledInTheCorrectOrderAfterRunningATestWithFailingClassSetUp(); //Checking the returned test report Report report = GetFirstMethodReportFrom(classReport.MethodReports); Exception raisedException = new NullReferenceException("Failing class set up."); Report expectedReport = new MethodReport(typeof(TestClassMockWithFailingClassSetUp).GetMethod("ClassSetUp"), raisedException); AssertEx.That(report, Is.EqualTo(expectedReport)); //Testing the run of all tests MethodCallOrder = new List<string>(); session.AddAll(); classReport = runner.Run(session); AssertThatTheMethodsAreCalledInTheCorrectOrderAfterRunningATestWithFailingClassSetUp(); //Checking the returned test reports ICollection<MethodReport> reports = classReport.MethodReports; raisedException = new NullReferenceException("Failing class set up."); expectedReport = new MethodReport(typeof(TestClassMockWithFailingClassSetUp).GetMethod("ClassSetUp"), raisedException); AssertEx.That(reports.Count, Is.EqualTo(1)); AssertEx.That(GetFirstMethodReportFrom(reports), Is.EqualTo(expectedReport)); }
private void AssertThatTheReportIsEqualToFooOrBar(Report report) { Report fooReport = new MethodReport(typeof(TestClassMock).GetMethod("Foo")); Report barReport = new MethodReport(typeof(TestClassMock).GetMethod("Bar")); AssertEx.That(report, Matches.AnyOf(Is.EqualTo(fooReport), Is.EqualTo(barReport))); }
public void TextCreation() { Type type = typeof(TestClass); MethodInfo succeededMethod = type.GetMethod("TestName"); MethodInfo failedMethod = type.GetMethod("SecondTest"); ClassReport report = new SimpleClassReport(type); AssertEx.That(report.Text, Is.EqualTo("No tests have been runned.")); MethodReport methodReport1 = new MethodReport(succeededMethod); report.Add(methodReport1); AssertEx.That(report.Text, Is.EqualTo("1 test runned: 0 failed, 1 succeeded")); MethodReport methodReport2 = new MethodReport(failedMethod, new AssertException("Exception Text")); report.Add(methodReport2); AssertEx.That(report.Text, Is.EqualTo("2 tests runned: 1 failed, 1 succeeded\n" + "\n" + "The SecondTest-Method failed.")); MethodReport methodReport3 = new MethodReport(succeededMethod, new NullReferenceException()); report.Add(methodReport3); AssertEx.That(report.Text, Is.EqualTo("2 tests runned: 2 failed, 0 succeeded\n" + "\n" + "The TestName-Method threw an unexpected exception.\n" + "\n" + "The SecondTest-Method failed.")); }
public void ReportToString() { Exception raisedException = new NullReferenceException("Exception text"); Report report = new MethodReport(typeof(TestClass).GetMethod("TestName"), raisedException); AssertEx.That(report.ToString(), Is.EqualTo(report.Text)); }
//////////////////////////////////////////////////////// // Generated GetHashCode and Equals // //////////////////////////////////////////////////////// private bool Equals(MethodReport other) { return Status.Equals(other.Status) && Method.Equals(other.Method); }