Пример #1
0
        private bool ProcessDataSourceTests(UnitTestElement test, TestMethodInfo testMethodInfo, ITestContext testContext, List <UnitTestElement> tests)
        {
            var dataRows = PlatformServiceProvider.Instance.TestDataSource.GetData(testMethodInfo, testContext);

            if (dataRows == null || !dataRows.Any())
            {
                return(false);
            }

            try
            {
                int rowIndex = 0;

                foreach (var dataRow in dataRows)
                {
                    // TODO: Test serialization
                    rowIndex++;

                    var displayName    = string.Format(CultureInfo.CurrentCulture, Resource.DataDrivenResultDisplayName, test.DisplayName, rowIndex);
                    var discoveredTest = test.Clone();
                    discoveredTest.DisplayName               = displayName;
                    discoveredTest.TestMethod.DataType       = DynamicDataType.DataSourceAttribute;
                    discoveredTest.TestMethod.SerializedData = DataSerializationHelper.Serialize(new[] { (object)rowIndex });
                    tests.Add(discoveredTest);
                }

                return(true);
            }
            finally
            {
                testContext.SetDataConnection(null);
                testContext.SetDataRow(null);
            }
        }
Пример #2
0
        public async Task <TestResult> RunTest(TestMethodInfo testMethodInfo)
        {
            var result = new TestResult();

            try
            {
                var testClassType = Type.GetType(testMethodInfo.DeclaringTypeFullName);
                if (testClassType is null)
                {
                    throw new NotSupportedException("Test class type should not be null");
                }
                var testClassInstance = GetTestClassInstance(testClassType);
                var methodInfo        = testClassType.GetMethod(testMethodInfo.MethodName);
                if (methodInfo is null)
                {
                    throw new NotSupportedException("Test method info should not be null");
                }
                var response = methodInfo.Invoke(testClassInstance, Array.Empty <object>());
                if (response is Task task)
                {
                    await task;
                }
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.FailMessage = ex.Message;
                result.StackTrace  = GetStackTrace(ex);
                logger.LogError($"Test failed: {testMethodInfo.Description}", ex);
            }
            return(result);
        }
Пример #3
0
        private bool TryProcessDataSource(UnitTestElement test, TestMethodInfo testMethodInfo, ITestContext testContext, List <UnitTestElement> tests)
        {
            var dataSourceAttributes = ReflectHelper.GetAttributes <UTF.DataSourceAttribute>(testMethodInfo.MethodInfo, false);

            if (dataSourceAttributes == null)
            {
                return(false);
            }

            if (dataSourceAttributes.Length > 1)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Resource.CannotEnumerateDataSourceAttribute_MoreThenOneDefined, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, dataSourceAttributes.Length);
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo($"DynamicDataEnumarator: {message}");
                throw new InvalidOperationException(message);
            }

            // dataSourceAttributes.Length == 1
            try
            {
                return(this.ProcessDataSourceTests(test, testMethodInfo, testContext, tests));
            }
            catch (Exception ex)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Resource.CannotEnumerateDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, ex);
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo($"DynamicDataEnumarator: {message}");
                return(false);
            }
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestMethod"/> class from the given <see cref="TestMethod"/> instance (i.e. clone).
        /// </summary>
        /// <param name="testMethodInfo">The <see cref="TestMethod"/> instance to clone values from.</param>
        internal TestMethod(TestMethodInfo testMethodInfo)
        {
            Debug.Assert(testMethodInfo != null, "TestMethodInfo can't be null");

            this.Name          = testMethodInfo.TestMethodName;
            this.FullClassName = testMethodInfo.TestClassName;
            this.AssemblyName  = testMethodInfo.Parent.Parent.AssemblyName;
            this.IsAsync       = ReflectHelper.MatchReturnType(testMethodInfo.MethodInfo, typeof(Task));
        }
Пример #5
0
        private void CheckTestMethod(string name)
        {
            Configuration config = this.GetConfiguration();

            config.AssemblyToBeAnalyzed = Assembly.GetExecutingAssembly().Location;
            config.TestMethodName       = name;
            using var testMethodInfo    = TestMethodInfo.Create(config);
            Assert.Equal(Assembly.GetExecutingAssembly(), testMethodInfo.Assembly);
            Assert.Equal($"{typeof(EntryPointTests).FullName}.{name}", testMethodInfo.Name);
        }
Пример #6
0
        protected TestMethodInfo CreateMethodInfo(MethodDeclarationSyntax methodDeclaration)
        {
            TestMethodInfo result = new TestMethodInfo(methodDeclaration.Identifier.ValueText, new DataStructures.TypeInfo(methodDeclaration.ReturnType.ToString()));

            foreach (ParameterSyntax parameter in methodDeclaration.ParameterList.Parameters)
            {
                result.Parameters.Add(new ParameterInfo(parameter.Identifier.ValueText, new DataStructures.TypeInfo(parameter.Type.ToString())));
            }
            return(result);
        }
Пример #7
0
        private static void CheckTestMethod(string name)
        {
            Configuration config = Configuration.Create();

            config.AssemblyToBeAnalyzed = Assembly.GetExecutingAssembly().Location;
            config.TestMethodName       = name;
            var testMethodInfo = TestMethodInfo.Create(config);

            Assert.Equal(Assembly.GetExecutingAssembly(), testMethodInfo.Assembly);
            Assert.Equal(GetFullyQualifiedTestName(name), testMethodInfo.Name);
        }
Пример #8
0
        public TestMethodRunnerTests()
        {
            var constructorInfo = typeof(DummyTestClass).GetConstructors().Single();

            this.methodInfo = typeof(DummyTestClass).GetMethods().Single(m => m.Name.Equals("DummyTestMethod"));
            var classAttribute = new UTF.TestClassAttribute();

            this.testMethodAttribute = new UTF.TestMethodAttribute();
            var testContextProperty = typeof(DummyTestClass).GetProperty("TestContext");

            var testAssemblyInfo = new TestAssemblyInfo(typeof(DummyTestClass).Assembly);

            this.testMethod = new TestMethod("dummyTestName", "dummyClassName", "dummyAssemblyName", false);
            this.testContextImplementation = new TestContextImplementation(this.testMethod, new StringWriter(), new Dictionary <string, object>());
            this.testClassInfo             = new TestClassInfo(
                type: typeof(DummyTestClass),
                constructor: constructorInfo,
                testContextProperty: testContextProperty,
                classAttribute: classAttribute,
                parent: testAssemblyInfo);

            this.globaltestMethodOptions = new TestMethodOptions()
            {
                Timeout           = 3600 * 1000,
                Executor          = this.testMethodAttribute,
                TestContext       = this.testContextImplementation,
                ExpectedException = null
            };
            var globalTestMethodInfo = new TestMethodInfo(
                this.methodInfo,
                this.testClassInfo,
                this.globaltestMethodOptions);
            var testMethodInfo = new TestableTestmethodInfo(this.methodInfo, this.testClassInfo, this.testMethodOptions, null);

            this.globalTestMethodRunner = new TestMethodRunner(globalTestMethodInfo, this.testMethod, this.testContextImplementation, false);

            this.testMethodOptions = new TestMethodOptions()
            {
                Timeout           = 200,
                Executor          = this.testMethodAttribute,
                TestContext       = this.testContextImplementation,
                ExpectedException = null
            };

            this.mockReflectHelper = new Mock <ReflectHelper>();

            // Reset test hooks
            DummyTestClass.TestConstructorMethodBody = () => { };
            DummyTestClass.TestContextSetterBody     = value => { };
            DummyTestClass.TestInitializeMethodBody  = value => { };
            DummyTestClass.TestMethodBody            = instance => { };
            DummyTestClass.TestCleanupMethodBody     = value => { };
        }
Пример #9
0
        private bool TryProcessTestDataSourceTests(UnitTestElement test, TestMethodInfo testMethodInfo, List <UnitTestElement> tests)
        {
            var methodInfo      = testMethodInfo.MethodInfo;
            var testDataSources = ReflectHelper.GetAttributes <Attribute>(methodInfo, false)?.Where(a => a is UTF.ITestDataSource).OfType <UTF.ITestDataSource>().ToArray();

            if (testDataSources == null || testDataSources.Length == 0)
            {
                return(false);
            }

            try
            {
                return(this.ProcessTestDataSourceTests(test, (MethodInfo)methodInfo, testDataSources, tests));
            }
            catch (Exception ex)
            {
                var message = string.Format(CultureInfo.CurrentCulture, Resource.CannotEnumerateIDataSourceAttribute, test.TestMethod.ManagedTypeName, test.TestMethod.ManagedMethodName, ex);
                PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo($"DynamicDataEnumarator: {message}");
                return(false);
            }
        }
Пример #10
0
 public void TestMethod(TestMethodInfo minfo, object target)
 {
     try
     {
         minfo.MethodInfo.Invoke(target, null);
         _win.Dispatcher.Invoke(() =>
         {
             minfo.SetResult(true);
         });
     }
     catch (Exception ex)
     {
         Exception e   = getException(ex);
         string    msg = e.Message;
         _win.Dispatcher.Invoke(() =>
         {
             minfo.SetResult(false);
             minfo.Detail = msg + Environment.NewLine + e.StackTrace;
         });
     }
 }
 protected LocalDeclarationStatementSyntax CreateActualDeclaration(TestMethodInfo methodInfo, TestClassInfo classInfo)
 {
     return(LocalDeclarationStatement(
                VariableDeclaration(
                    IdentifierName(methodInfo.ReturnType.Typename))
                .WithVariables(
                    SingletonSeparatedList <VariableDeclaratorSyntax>(
                        VariableDeclarator(
                            Identifier(actualVariableName))
                        .WithInitializer(
                            EqualsValueClause(
                                InvocationExpression(
                                    MemberAccessExpression(
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        IdentifierName(CreateVariableName(classInfo.Name, true)),
                                        IdentifierName(methodInfo.Name)))
                                .WithArgumentList(
                                    ArgumentList(
                                        SeparatedList <ArgumentSyntax>(
                                            CreateArguments(methodInfo.Parameters))))))))));
 }
Пример #12
0
        public TestMethodRunnerTests()
        {
            var constructorInfo = typeof(DummyTestClass).GetConstructors().Single();

            this.methodInfo = typeof(DummyTestClass).GetMethods().Single(m => m.Name.Equals("DummyTestMethod"));
            var classAttribute = new UTF.TestClassAttribute();

            this.testMethodAttribute = new UTF.TestMethodAttribute();
            var testContextProperty = typeof(DummyTestClass).GetProperty("TestContext");

            var testAssemblyInfo = new TestAssemblyInfo();

            this.testMethod = new TestMethod("dummyTestName", "dummyClassName", "dummyAssemblyName", false);
            this.testContextImplementation = new TestContextImplementation(this.testMethod, null, new Dictionary <string, object>());
            this.testClassInfo             = new TestClassInfo(
                type: typeof(DummyTestClass),
                constructor: constructorInfo,
                testContextProperty: testContextProperty,
                classAttribute: classAttribute,
                parent: testAssemblyInfo);
            var globalTestMethodInfo = new TestMethodInfo(
                this.methodInfo,
                timeout: 3600 * 1000,
                executor: this.testMethodAttribute,
                expectedException: null,
                parent: this.testClassInfo,
                testContext: this.testContextImplementation);

            this.globalTestMethodRunner = new TestMethodRunner(globalTestMethodInfo, this.testMethod, this.testContextImplementation, false);

            // Reset test hooks
            DummyTestClass.TestConstructorMethodBody = () => { };
            DummyTestClass.TestContextSetterBody     = value => { };
            DummyTestClass.TestInitializeMethodBody  = value => { };
            DummyTestClass.TestMethodBody            = instance => { };
            DummyTestClass.TestCleanupMethodBody     = value => { };
        }
Пример #13
0
        private void MockHttpContextAndActionDescriptors(HateoasTestData mockData)
        {
            var config = new Mock <HttpConfiguration>().Object;
            var prefix = new RoutePrefixAttribute(mockData.Prefix);
            var controllerDescriptor =
                new TestControllerDescriptor(config, mockData.ControllerName, typeof(ApiController), prefix);
            var actionParameters =
                mockData.RouteValues.Keys.Aggregate(new Collection <HttpParameterDescriptor>(),
                                                    (collection, parameterName) =>
            {
                var mockParameterDescriptor = new Mock <HttpParameterDescriptor>();
                mockParameterDescriptor.Setup(x => x.ParameterName).Returns(parameterName);
                collection.Add(mockParameterDescriptor.Object);
                return(collection);
            });
            var methodInfo = new TestMethodInfo(new RouteAttribute(mockData.Template)
            {
                Name = mockData.RouteName
            });
            var actionDescriptor = new TestActionDescriptor(controllerDescriptor, methodInfo, actionParameters);

            actionDescriptor.SupportedHttpMethods.Add(new HttpMethod(mockData.Method));
            var dataTokens = new RouteValueDictionary
            {
                { "descriptors", new[] { actionDescriptor } }
            };

            RouteTable.Routes.Add(new Route(mockData.RoutePath, null)
            {
                DataTokens = dataTokens
            });
            var request  = new HttpRequest("", mockData.BaseUrl, "");
            var response = new HttpResponse(new StringWriter());

            HttpContext.Current = new HttpContext(request, response);
        }
        protected MethodDeclarationSyntax CreateTestMethodDeclaration(TestMethodInfo methodInfo, TestClassInfo classInfo)
        {
            List <LocalDeclarationStatementSyntax> arrangeBody = methodInfo.Parameters.Select((parameter) => CreateVariableInitializeExpression(parameter)).ToList();

            if (arrangeBody.Count != 0)
            {
                arrangeBody[arrangeBody.Count - 1] = arrangeBody[arrangeBody.Count - 1].WithSemicolonToken(emptyLineToken);
            }

            List <StatementSyntax> actAssertBody = new List <StatementSyntax>();

            if (methodInfo.ReturnType.Typename != "void")
            {
                actAssertBody.Add(CreateActualDeclaration(methodInfo, classInfo).WithSemicolonToken(emptyLineToken));
                actAssertBody.Add(CreateExpectedDeclaration(methodInfo.ReturnType));
                actAssertBody.Add(CreateAreEqualExpression(methodInfo.ReturnType));
            }
            actAssertBody.Add(failExpression);

            return(MethodDeclaration(
                       PredefinedType(
                           Token(SyntaxKind.VoidKeyword)),
                       Identifier(methodInfo.Name + "Test"))
                   .WithAttributeLists(
                       SingletonList <AttributeListSyntax>(
                           AttributeList(
                               SingletonSeparatedList <AttributeSyntax>(
                                   Attribute(
                                       IdentifierName("TestMethod"))))))
                   .WithModifiers(
                       TokenList(
                           Token(SyntaxKind.PublicKeyword)))
                   .WithBody(
                       Block(
                           arrangeBody.Concat(actAssertBody))));
        }