public void Ex_PassingTestShouldGetExecutedOnlyOnce()
        {
            // Arrange
            var mockPassingTestMethod = new Mock <ITestMethod>();

            var args = It.IsAny <object[]>();
            var passingTestResult = new TestResult()
            {
                Outcome = UnitTestOutcome.Passed
            };

            mockPassingTestMethod.Setup(tm => tm.Invoke(args)).Returns(passingTestResult);

            const int RETRY_COUNT = 5;

            mockPassingTestMethod.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr = { new RetryAttribute(RETRY_COUNT) };
                return(attr);
            }
                                                                                  );

            var retriableTestMethod = new TestMethodExAttribute();

            // Act
            var tr = retriableTestMethod.Execute(mockPassingTestMethod.Object);

            // Assert
            mockPassingTestMethod.Verify(tm => tm.Invoke(args), Times.Once);
            Assert.AreEqual(1, tr.Length);
            Assert.AreEqual(UnitTestOutcome.Passed, tr[0].Outcome);
        }
        public void TestCanHaveSingleSmokeTestAttributeAndIsExecutedOnlyOnce()
        {
            // Arrange
            var mockPassingTestMethod = new Mock <ITestMethod>();

            var args = It.IsAny <object[]>();
            var passingTestResult = new TestResult()
            {
                Outcome = UnitTestOutcome.Passed
            };

            mockPassingTestMethod.Setup(tm => tm.Invoke(args)).Returns(passingTestResult);

            mockPassingTestMethod.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr =
                {
                    new SmokeTestAttribute()
                };
                return(attr);
            });

            var tmx = new TestMethodExAttribute();

            // Act
            var tr = tmx.Execute(mockPassingTestMethod.Object);

            // Assert
            mockPassingTestMethod.Verify(tm => tm.Invoke(args), Times.Once);
            Assert.AreEqual(1, tr.Length);
            Assert.AreEqual(UnitTestOutcome.Passed, tr.First().Outcome);
        }
        public void Ex_TestWithUnexpectedExceptionExecutedOnlyOnce()
        {
            // Arrange
            var mockTestMethodWithException = new Mock <ITestMethod>();

            const int RETRY_COUNT = 5;

            mockTestMethodWithException.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr = { new RetryAttribute(RETRY_COUNT) };
                return(attr);
            }
                                                                                        );

            var args = It.IsAny <object[]>();

            mockTestMethodWithException.Setup(tm => tm.Invoke(args)).Returns(() =>
            {
                throw new Exception();
            }
                                                                             );

            var retriableTestMethod = new TestMethodExAttribute();

            // Act
            var tr = retriableTestMethod.Execute(mockTestMethodWithException.Object);

            // Assert
            mockTestMethodWithException.Verify(tm => tm.Invoke(args), Times.Once);
            Assert.AreEqual(0, tr.Length);
        }
        public void Ex_FailingTestShouldBeExecutedRetryCountNumberOfTimesAndReturnFailure()
        {
            // Arrange
            var mockPassingTestMethod = new Mock <ITestMethod>();

            var args = It.IsAny <object[]>();
            var failingTestResult = new TestResult()
            {
                Outcome = UnitTestOutcome.Failed
            };

            mockPassingTestMethod.Setup(tm => tm.Invoke(args)).Returns(failingTestResult);

            const int RETRY_COUNT = 5;

            mockPassingTestMethod.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr = { new RetryAttribute(RETRY_COUNT) };
                return(attr);
            }
                                                                                  );

            var retriableTestMethod = new TestMethodExAttribute();

            // Act
            var tr = retriableTestMethod.Execute(mockPassingTestMethod.Object);

            // Assert
            mockPassingTestMethod.Verify(tm => tm.Invoke(args), Times.Exactly(RETRY_COUNT));
            Assert.AreEqual(RETRY_COUNT, tr.Length);
            for (int i = 0; i < RETRY_COUNT; i++)
            {
                Assert.AreEqual(UnitTestOutcome.Failed, tr[i].Outcome);
            }
        }
        public void Ex_FlakyTestShouldBeExecutedtMultipleTimesTillItPasses()
        {
            // Arrange
            var mockFlakyTestMethod = new Mock <ITestMethod>();
            var args = It.IsAny <object[]>();
            var passingTestResult = new TestResult()
            {
                Outcome = UnitTestOutcome.Passed
            };
            var failingTestResult = new TestResult()
            {
                Outcome = UnitTestOutcome.Failed
            };

            int       count   = 0;
            const int TRIGGER = 3;

            mockFlakyTestMethod.Setup(tm => tm.Invoke(args)).Returns(() =>
            {
                count++;
                if (count < TRIGGER)
                {
                    return(failingTestResult);
                }
                return(passingTestResult);
            }
                                                                     );

            const int RETRY_COUNT = 5;

            mockFlakyTestMethod.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr = { new RetryAttribute(RETRY_COUNT) };
                return(attr);
            }
                                                                                );
            var retriableTestMethod = new TestMethodExAttribute();

            // Act
            var tr = retriableTestMethod.Execute(mockFlakyTestMethod.Object);

            // Assert
            mockFlakyTestMethod.Verify(tm => tm.Invoke(args), Times.Exactly(TRIGGER));
            Assert.AreEqual(TRIGGER, tr.Length);
            int i = 0;

            for (i = 0; i < TRIGGER - 1; i++)
            {
                Assert.AreEqual(UnitTestOutcome.Failed, tr[i].Outcome);
            }
            Assert.AreEqual(UnitTestOutcome.Passed, tr[i].Outcome);
        }
        public void RepeatWithRetryTestForAllTestOutcomes(
            UnitTestOutcome requiredTestOutCome,
            int requestedRepeatCount,
            int requestedRetryCount,
            int expectedExecutionAttempts)
        {
            // Arrange
            TestResult[] expected =
            {
                new TestResult()
                {
                    Outcome = requiredTestOutCome
                }
            };

            var mockTestMethod = new Mock <ITestMethod>();

            mockTestMethod.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr =
                {
                    new RepeatAttribute(requestedRepeatCount),
                    new RetryAttribute(requestedRetryCount),
                };
                return(attr);
            }
                                                                           );

            var args = It.IsAny <object[]>();

            mockTestMethod.Setup(tm => tm.Invoke(args)).Returns(() =>
            {
                return(expected[0]);
            }
                                                                );


            // Act
            var retriableTestMethod = new TestMethodExAttribute();
            var tr = retriableTestMethod.Execute(mockTestMethod.Object);

            // Assert
            mockTestMethod.Verify(tm => tm.Invoke(args), Times.Exactly(expectedExecutionAttempts));
            Assert.AreEqual(tr.Length, expectedExecutionAttempts);
            Assert.IsTrue((tr.All((r) => r.Outcome == requiredTestOutCome)));
        }
Example #7
0
        public void BugAttributeDoesNotAlterExecutionForAllTestOutcomes(
            UnitTestOutcome RequiredTestOutCome)
        {
            // Arrange
            TestResult[] expected =
            {
                new TestResult()
                {
                    Outcome = RequiredTestOutCome
                }
            };

            var mockTestMethod = new Mock <ITestMethod>();

            mockTestMethod.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr =
                {
                    new BugAttribute(177)     // 177 is just some int representing a bug ID
                };
                return(attr);
            }
                                                                           );

            var args = It.IsAny <object[]>();

            mockTestMethod.Setup(tm => tm.Invoke(args)).Returns(() =>
            {
                return(expected[0]);
            }
                                                                );

            // Act
            var retriableTestMethod = new TestMethodExAttribute();
            var tr = retriableTestMethod.Execute(mockTestMethod.Object);

            // Assert
            mockTestMethod.Verify(tm => tm.Invoke(args), Times.Once);
            Assert.AreEqual(tr.Length, 1);
            Assert.IsTrue((tr.All((r) => r.Outcome == RequiredTestOutCome)));
        }
        public void Ex_TestWithExpectedExceptionExecutedRetryCountNumberOfTimes()
        {
            // Arrange
            var mockTestMethodWithException = new Mock <ITestMethod>();

            const int RETRY_COUNT = 5;

            mockTestMethodWithException.Setup(tm => tm.GetAllAttributes(false)).Returns(() =>
            {
                Attribute[] attr =
                {
                    new RetryAttribute(RETRY_COUNT),
                    new ExpectedExceptionAttribute(typeof(System.DivideByZeroException))
                };
                return(attr);
            }
                                                                                        );

            var args = It.IsAny <object[]>();

            mockTestMethodWithException.Setup(tm => tm.Invoke(args)).Returns(() =>
            {
                int numerator   = 1;
                int denominator = 0;
                int val         = numerator / denominator; // raise a DivideByZeroException
                return(new TestResult()
                {
                    Outcome = UnitTestOutcome.Passed
                });
            }
                                                                             );

            var retriableTestMethod = new TestMethodExAttribute();

            // Act
            var tr = retriableTestMethod.Execute(mockTestMethodWithException.Object);

            // Assert
            mockTestMethodWithException.Verify(tm => tm.Invoke(args), Times.Exactly(RETRY_COUNT));
            Assert.AreEqual(0, tr.Length);
        }