コード例 #1
0
        public void RunTestsForMultipleTestShouldSendMultipleResults()
        {
            var testCase        = this.GetTestCase(typeof(DummyTestClass), "PassingTest");
            var failingTestCase = this.GetTestCase(typeof(DummyTestClass), "FailingTest");

            TestCase[] tests = new[] { testCase, failingTestCase };

            this.TestExecutionManager.RunTests(tests, this.runContext, this.frameworkHandle, this.cancellationToken);

            List <string> expectedTestCaseStartList = new List <string>()
            {
                "PassingTest", "FailingTest"
            };
            List <string> expectedTestCaseEndList = new List <string>()
            {
                "PassingTest:Passed", "FailingTest:Failed"
            };
            List <string> expectedResultList = new List <string>()
            {
                "PassingTest  Passed", "FailingTest  Failed\r\n  Message: (null)"
            };

            CollectionAssert.AreEqual(expectedTestCaseStartList, this.frameworkHandle.TestCaseStartList);
            CollectionAssert.AreEqual(expectedTestCaseEndList, this.frameworkHandle.TestCaseEndList);
            Assert.AreEqual("PassingTest  Passed", this.frameworkHandle.ResultsList[0]);
            StringAssert.Contains(
                this.frameworkHandle.ResultsList[1],
                "FailingTest  Failed\r\n  Message: Assert.Fail failed. \r\n  StackTrace:\r\n   at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestExecutionManagerTests.DummyTestClass.FailingTest()");
        }
コード例 #2
0
        public void ExecuteForClassInitializeThrowingExceptionShouldReturnUnitTestResultWithFailedOutcome()
        {
            // Arrange.
            var tai = new TestAssemblyInfo(typeof(DummyTestClass).Assembly);

            var constructorInfo     = typeof(DummyTestClass).GetConstructors().Single();
            var classAttribute      = new UTF.TestClassAttribute();
            var testContextProperty = typeof(DummyTestClass).GetProperty("TestContext");

            var tci = new TestClassInfo(
                type: typeof(DummyTestClass),
                constructor: constructorInfo,
                testContextProperty: testContextProperty,
                classAttribute: classAttribute,
                parent: tai)
            {
                ClassInitializeMethod = typeof(TestMethodRunnerTests).GetMethod(
                    "InitMethodThrowingException",
                    BindingFlags.Static | BindingFlags.NonPublic)
            };

            var testMethodInfo   = new TestableTestmethodInfo(this.methodInfo, tci, this.testMethodOptions, () => { throw new Exception("DummyException"); });
            var testMethodRunner = new TestMethodRunner(testMethodInfo, this.testMethod, this.testContextImplementation, false);

            // Act.
            var results = testMethodRunner.Execute();

            // Assert.
            Assert.AreEqual(AdapterTestOutcome.Failed, results[0].Outcome);
            StringAssert.Contains(results[0].ErrorMessage, "System.ArgumentException: Value does not fall within the expected range.");
        }
コード例 #3
0
        public void AreEqualShouldFailWhenNotEqualDecimalWithMessage()
        {
            var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.AreEqual(0.1M, 0.2M, "A Message"));

            Assert.IsNotNull(ex);
            StringAssert.Contains(ex.Message, "A Message");
        }
コード例 #4
0
        public void AreEqualShouldFailWhenFloatDoubleWithMessage()
        {
            var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.AreEqual(100E-2, 200E-2, "A Message"));

            Assert.IsNotNull(ex);
            StringAssert.Contains(ex.Message, "A Message");
        }
コード例 #5
0
        public void ThrowsExceptionWithLamdaExpressionsShouldThrowAssertionOnNoException()
        {
            var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.ThrowsException <ArgumentException>(() => { }));

            Assert.IsNotNull(ex);
            Assert.AreEqual(typeof(TestFrameworkV2.AssertFailedException), ex.GetType());
            StringAssert.Contains(ex.Message, "Assert.ThrowsException failed. No exception thrown. ArgumentException exception was expected.");
        }
コード例 #6
0
        public void IsValidDeploymentItemShouldReportWarningIfDeploymentOutputDirectoryIsNull()
        {
            string warning;

            Assert.IsFalse(this.deploymentItemUtility.IsValidDeploymentItem(this.defaultDeploymentItemPath, null, out warning));

            StringAssert.Contains(Resource.DeploymentItemOutputDirectoryCannotBeNull, warning);
        }
コード例 #7
0
        public void IsValidDeploymentItemShouldReportWarningIfSourcePathIsEmpty()
        {
            string warning;

            Assert.IsFalse(this.deploymentItemUtility.IsValidDeploymentItem(string.Empty, this.defaultDeploymentItemOutputDirectory, out warning));

            StringAssert.Contains(Resource.DeploymentItemPathCannotBeNullOrEmpty, warning);
        }
コード例 #8
0
        public void IsFalseNullableBooleansShouldFailWithNull()
        {
            bool?nullBool = null;
            var  ex       = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.IsFalse(nullBool));

            Assert.IsNotNull(ex);
            StringAssert.Contains(ex.Message, "Assert.IsFalse failed");
        }
コード例 #9
0
        public void WriteWithMessageShouldWriteToStringWriterForNullCharacters()
        {
            var stringWriter = new ThreadSafeStringWriter(null, "test");

            this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
            this.testContextImplementation.Write("1 Testing \0 write \0");
            StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0");
        }
コード例 #10
0
        public void WriteShouldWriteToStringWriter()
        {
            var stringWriter = new ThreadSafeStringWriter(null, "test");

            this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
            this.testContextImplementation.Write("{0} Testing write", 1);
            StringAssert.Contains(stringWriter.ToString(), "1 Testing write");
        }
コード例 #11
0
        public void WriteWithMessageShouldWriteToStringWriter()
        {
            var stringWriter = new StringWriter();

            this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
            this.testContextImplementation.Write("1 Testing write");
            StringAssert.Contains(stringWriter.ToString(), "1 Testing write");
        }
コード例 #12
0
        public void WriteShouldWriteToStringWriterForNullCharacters()
        {
            var stringWriter = new StringWriter();

            this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
            this.testContextImplementation.Write("{0} Testing \0 write \0", 1);
            StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0");
        }
コード例 #13
0
        public void GetDiagnosticMessagesShouldReturnMessagesFromWriteLine()
        {
            this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

            this.testContextImplementation.WriteLine("1 Testing write");
            this.testContextImplementation.WriteLine("2 Its a happy day");

            StringAssert.Contains(this.testContextImplementation.GetDiagnosticMessages(), "1 Testing write");
            StringAssert.Contains(this.testContextImplementation.GetDiagnosticMessages(), "2 Its a happy day");
        }
コード例 #14
0
        public void AddResultFileShouldThrowIfFileNameIsEmpty()
        {
            this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);

            var exception =
                ActionUtility.PerformActionAndReturnException(() => this.testContextImplementation.AddResultFile(string.Empty));

            Assert.AreEqual(typeof(ArgumentException), exception.GetType());
            StringAssert.Contains(exception.Message, Resource.Common_CannotBeNullOrEmpty);
        }
コード例 #15
0
        public void RunTestMethodForTestThrowingExceptionShouldReturnUnitTestResultWithFailedOutcome()
        {
            var testMethodInfo   = new TestableTestmethodInfo(this.methodInfo, this.testClassInfo, this.testMethodOptions, () => { throw new Exception("Dummy Exception"); });
            var testMethodRunner = new TestMethodRunner(testMethodInfo, this.testMethod, this.testContextImplementation, false);

            var results = testMethodRunner.RunTestMethod();

            Assert.AreEqual(AdapterTestOutcome.Failed, results[0].Outcome);
            StringAssert.Contains(results[0].ErrorMessage, "Exception thrown while executing test");
        }
コード例 #16
0
        public void IsValidDeploymentItemShouldReportWarningIfDeploymentOutputDirectoryIsRooted()
        {
            string warning;

            Assert.IsFalse(this.deploymentItemUtility.IsValidDeploymentItem(this.defaultDeploymentItemPath, "C:\\temp", out warning));

            StringAssert.Contains(
                string.Format(
                    Resource.DeploymentItemOutputDirectoryMustBeRelative,
                    "C:\\temp"),
                warning);
        }
コード例 #17
0
        public void IsValidDeploymentItemShouldReportWarningIfOutputDirectoryHasInvalidCharacters()
        {
            string warning;

            Assert.IsFalse(this.deploymentItemUtility.IsValidDeploymentItem(this.defaultDeploymentItemPath, "<>", out warning));

            StringAssert.Contains(
                string.Format(
                    Resource.DeploymentItemContainsInvalidCharacters,
                    this.defaultDeploymentItemPath,
                    "<>"),
                warning);
        }
コード例 #18
0
        public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAssertInconclusive()
        {
            DummyTestClass.ClassInitializeMethodBody = (tc) => UTF.Assert.Inconclusive("Test Inconclusive");
            this.testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");

            var exception = ActionUtility.PerformActionAndReturnException(() => this.testClassInfo.RunClassInitialize(this.testContext)) as TestFailedException;

            Assert.IsNotNull(exception);
            Assert.AreEqual(UnitTestOutcome.Inconclusive, exception.Outcome);
            StringAssert.Contains(exception.Message, "Test Inconclusive");
            StringAssert.StartsWith(
                exception.StackTraceInformation.ErrorStackTrace,
                "   at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.<RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAssertInconclusive>");
        }
コード例 #19
0
ファイル: TestClassInfoTests.cs プロジェクト: Haplois/testfx
        public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails()
        {
            // Arrange
            DummyTestClass.ClassCleanupMethodBody = () => UTF.Assert.Inconclusive("Test Inconclusive");
            this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.ClassCleanupMethod));

            // Act
            var classCleanup = this.testClassInfo.RunClassCleanup();

            // Assert
            StringAssert.StartsWith(classCleanup, "Class Cleanup method DummyTestClass.ClassCleanupMethod failed.");
            StringAssert.Contains(classCleanup, "Error Message: Assert.Inconclusive failed. Test Inconclusive.");
            StringAssert.Contains(classCleanup, $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>");
        }
コード例 #20
0
ファイル: TestClassInfoTests.cs プロジェクト: Haplois/testfx
        public void RunClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions()
        {
            // Arrange
            DummyTestClass.ClassCleanupMethodBody = () => { throw new ArgumentException("Argument Exception"); };
            this.testClassInfo.ClassCleanupMethod = typeof(DummyTestClass).GetMethod(nameof(DummyTestClass.ClassCleanupMethod));

            // Act
            var classCleanup = this.testClassInfo.RunClassCleanup();

            // Assert
            StringAssert.StartsWith(classCleanup, "Class Cleanup method DummyTestClass.ClassCleanupMethod failed.");
            StringAssert.Contains(classCleanup, "Error Message: System.ArgumentException: Argument Exception. Stack Trace:");
            StringAssert.Contains(classCleanup, $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions)}>");
        }
コード例 #21
0
        public void GetSettingsShouldThrowWhenParallelizeHasInvalidElements()
        {
            string runSettingxml =
                @"<RunSettings>
                    <MSTestV2>
                        <Parallelize>
                            <Hola>Hi</Hola>
                        </Parallelize>
                    </MSTestV2>
                  </RunSettings>";

            var exception = ActionUtility.PerformActionAndReturnException(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));

            Assert.IsNotNull(exception);
            Assert.AreEqual(typeof(AdapterSettingsException).FullName, exception.GetType().FullName);
            StringAssert.Contains(exception.Message, "Invalid settings 'Parallelize'. Unexpected XmlElement: 'Hola'.");
        }
コード例 #22
0
        public void GetSettingsShouldThrowIfParallelizationScopeIsNotValid()
        {
            string runSettingxml =
                @"<RunSettings>
                    <MSTestV2>
                        <Parallelize>
                            <Scope>JustParallelizeWillYou</Scope>
                        </Parallelize>
                    </MSTestV2>
                  </RunSettings>";

            var exception = ActionUtility.PerformActionAndReturnException(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));

            Assert.IsNotNull(exception);
            Assert.AreEqual(typeof(AdapterSettingsException).FullName, exception.GetType().FullName);
            StringAssert.Contains(exception.Message, "Invalid value 'JustParallelizeWillYou' specified for 'Scope'. Supported scopes are ClassLevel, MethodLevel.");
        }
コード例 #23
0
        public void GetSettingsShouldThrowIfParallelizationWorkersIsNegative()
        {
            string runSettingxml =
                @"<RunSettings>
                    <MSTestV2>
                        <Parallelize>
                            <Workers>-1</Workers>
                        </Parallelize>
                    </MSTestV2>
                  </RunSettings>";

            var exception = ActionUtility.PerformActionAndReturnException(() => MSTestSettings.GetSettings(runSettingxml, MSTestSettings.SettingsNameAlias));

            Assert.IsNotNull(exception);
            Assert.AreEqual(typeof(AdapterSettingsException).FullName, exception.GetType().FullName);
            StringAssert.Contains(exception.Message, "Invalid value '-1' specified for 'Workers'. The value should be a non-negative integer.");
        }
コード例 #24
0
        public void ThrowsExceptionAsyncShouldThrowAssertionOnNoException()
        {
            Task t = TestFrameworkV2.Assert.ThrowsExceptionAsync <ArgumentException>(
                async() =>
            {
                await Task.Delay(5);
            });
            var ex = ActionUtility.PerformActionAndReturnException(() => t.Wait());

            Assert.IsNotNull(ex);

            var innerException = ex.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(TestFrameworkV2.AssertFailedException), innerException.GetType());
            StringAssert.Contains(innerException.Message, "Assert.ThrowsException failed. No exception thrown. ArgumentException exception was expected.");
        }
コード例 #25
0
ファイル: TestClassInfoTests.cs プロジェクト: Haplois/testfx
        public void RunBaseClassCleanupWithNoDerivedClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions()
        {
            // Arrange
            DummyBaseTestClass.ClassCleanupMethodBody = () => { throw new ArgumentException("Argument Exception"); };
            var baseClassCleanupMethod = typeof(DummyBaseTestClass).GetMethod(nameof(DummyBaseTestClass.CleanupClassMethod));

            this.testClassInfo.ClassCleanupMethod = null;
            this.testClassInfo.BaseClassInitAndCleanupMethods.Enqueue(Tuple.Create((MethodInfo)null, baseClassCleanupMethod));
            this.testClassInfo.BaseClassCleanupMethodsStack.Push(baseClassCleanupMethod);

            // Act
            var classCleanup = this.testClassInfo.RunClassCleanup();

            // Assert
            StringAssert.StartsWith(classCleanup, "Class Cleanup method DummyBaseTestClass.CleanupClassMethod failed.");
            StringAssert.Contains(classCleanup, "Error Message: System.ArgumentException: Argument Exception. Stack Trace:");
            StringAssert.Contains(classCleanup, $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunBaseClassCleanupWithNoDerivedClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions)}>");
        }
コード例 #26
0
        public void ThrowsExceptionAsyncWithMessageShouldThrowAssertionOnWrongException()
        {
            Task t = TestFrameworkV2.Assert.ThrowsExceptionAsync <ArgumentException>(
                async() =>
            {
                await Task.Delay(5);
                throw new FormatException();
            },
                "Happily ever after.");
            var ex = ActionUtility.PerformActionAndReturnException(() => t.Wait());

            Assert.IsNotNull(ex);

            var innerException = ex.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(TestFrameworkV2.AssertFailedException), innerException.GetType());
            StringAssert.Contains(innerException.Message, "Assert.ThrowsException failed. Threw exception FormatException, but exception ArgumentException was expected. Happily ever after.");
        }
コード例 #27
0
        public void ThrowsExceptionAsyncWithMessageAndParamsShouldThrowAssertionOnNoException()
        {
            Task t = TestFrameworkV2.Assert.ThrowsExceptionAsync <ArgumentException>(
                async() =>
            {
                await Task.Delay(5);
            },
                "The world is not on fire {0}.{1}-{2}.",
                "ta",
                "da",
                123);
            var ex = ActionUtility.PerformActionAndReturnException(() => t.Wait());

            Assert.IsNotNull(ex);

            var innerException = ex.InnerException;

            Assert.IsNotNull(innerException);
            Assert.AreEqual(typeof(TestFrameworkV2.AssertFailedException), innerException.GetType());
            StringAssert.Contains(innerException.Message, "Assert.ThrowsException failed. No exception thrown. ArgumentException exception was expected. The world is not on fire ta.da-123.");
        }
コード例 #28
0
        public void ExecuteShouldFillInLogsIfAssemblyInitializeThrows()
        {
            StringWriter writer = new StringWriter(new StringBuilder());

            DummyTestClass.AssemblyInitializeMethodBody = (UTFExtension.TestContext tc) =>
            {
                writer.Write("Hills");
                tc.WriteLine("Valleys");
                throw new ArgumentException();
            };
            this.testClassInfo.Parent.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("DummyAssemblyInit");
            var testMethodInfo = new TestableTestmethodInfo(this.methodInfo, this.testClassInfo, this.testMethodOptions, () => new UTF.TestResult()
            {
                Outcome = UTF.UnitTestOutcome.Passed
            });
            var testMethodRunner = new TestMethodRunner(testMethodInfo, this.testMethod, this.testContextImplementation, true);

            this.testablePlatformServiceProvider.MockTraceListener.Setup(tl => tl.GetWriter()).Returns(writer);

            var results = testMethodRunner.Execute();

            Assert.AreEqual("Hills", results[0].DebugTrace);
            StringAssert.Contains(results[0].TestContextMessages, "Valleys");
        }