Beispiel #1
0
 public TestInfo AddTestNode(TestInfo test)
 {
     return(_reportManagerHandler.AddTestNode(test));
 }
Beispiel #2
0
        public virtual object ExecuteTest(string testId, string assemblyName = null, params object[] args)
        {
            Type     classType;
            Assembly assembly = null;
            //object instance;
            string        testName, classId;
            PrivateObject testObject = null;
            object        retVal     = null;
            Dictionary <Type, MethodInfo> methods = new Dictionary <Type, MethodInfo>();
            bool runAssemblyInitialize            = false;
            List <MethodInfo> existingMethods;
            MethodInfo        methodInfo;
            TestContextEx     testContext;

            if (String.IsNullOrEmpty(testId))
            {
                throw new ArgumentNullException("testId", "cannot be null or empty.");
            }

            List <string> sections = new List <string>(testId.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries));

            testName = sections[sections.Count - 1];
            sections.RemoveAt(sections.Count - 1);
            classId = String.Join(".", sections);

            if (!String.IsNullOrEmpty(assemblyName))
            {
                assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(i =>
                {
                    try { return(Path.GetFileName(i.Location).Equals(assemblyName)); }
                    catch { return(false); }
                });

                if (assembly == null)
                {
                    try
                    {
                        assembly = Assembly.LoadFile($"{this.TestContext.TestDeploymentDir}\\{assemblyName}");
                        runAssemblyInitialize = true;
                    }
                    catch (Exception e)
                    {
                        throw new InternalTestFailureException($"executing test: '{testId}' in Assembly: '{assemblyName}' failed. Loading assembly '{this.TestContext.TestDeploymentDir}\\{assemblyName}' failed.", e);
                    }
                }

                classType = assembly.GetTypes().FirstOrDefault(new Func <Type, bool>((x) =>
                {
                    // If the called test is in this assembly.
                    if (x.IsClass && x.FullName.Equals(classId))
                    {
                        Type interfaceType;

                        interfaceType = x.GetInterface(typeof(ITestClass).Name);
                        if (interfaceType != null)
                        {
                            return(true);
                        }
                    }

                    return(false);
                }));

                if (classType == null)
                {
                    throw new InternalTestFailureException($"executing test: '{testId}' in Assembly: '{assemblyName}' failed. test class '{classId}' not found in given assembly");
                }

                var instance = AppDomain.CurrentDomain.CreateInstance(assembly.FullName, classType.FullName).Unwrap();

                if (runAssemblyInitialize)
                {
                    testContext = new TestContextEx(testName, classId, new StringWriter(), this.TestContext.Properties);

                    testObject = new PrivateObject(classType);

                    methodInfo = classType.BaseType.GetMethod("AssemblyInitialize", BindingFlags.Static | BindingFlags.Public);
                    if (methodInfo != null)
                    {
                        methodInfo.Invoke(null, new object[] { testContext });
                    }

                    testObject.SetProperty("TestContext", testContext);
                }

                retVal = testObject.Invoke("ExecuteTest", testId, null, args);

                if (runAssemblyInitialize)
                {
                    methodInfo = classType.BaseType.GetMethod("AssemblyCleanup", BindingFlags.Static | BindingFlags.Public);
                    if (methodInfo != null)
                    {
                        methodInfo.Invoke(null, new object[] { });
                    }
                }

                return(retVal);
            }

            assembly = this.GetType().Assembly;

            classType = assembly.GetTypes().FirstOrDefault(new Func <Type, bool>((x) =>
            {
                // If the called test is in this assembly.
                if (x.IsClass && x.FullName.Equals(classId))
                {
                    Type interfaceType;

                    interfaceType = x.GetInterface(typeof(ITestClass).Name);
                    if (interfaceType != null)
                    {
                        return(true);
                    }
                }

                return(false);
            }));

            if (classType == null)
            {
                throw new InternalTestFailureException($"executing test: '{testId}' in Assembly: '{assemblyName}' failed. test class '{classId}' not found in given assembly");
            }

            existingMethods = classType.GetMethods().ToList();

            foreach (MethodInfo method in existingMethods)
            {
                Attribute attrib;

                if (!methods.ContainsKey(typeof(ClassInitializeAttribute)))
                {
                    attrib = method.GetCustomAttribute <ClassInitializeAttribute>();
                    if (attrib != null)
                    {
                        methods.Add(typeof(ClassInitializeAttribute), method);
                    }
                }

                if (!methods.ContainsKey(typeof(ClassCleanupAttribute)))
                {
                    attrib = method.GetCustomAttribute <ClassCleanupAttribute>();
                    if (attrib != null)
                    {
                        methods.Add(typeof(ClassCleanupAttribute), method);
                    }
                }

                if (!methods.ContainsKey(typeof(TestInitializeAttribute)))
                {
                    attrib = method.GetCustomAttribute <TestInitializeAttribute>();
                    if (attrib != null)
                    {
                        methods.Add(typeof(TestInitializeAttribute), method);
                    }
                }

                if (!methods.ContainsKey(typeof(TestCleanupAttribute)))
                {
                    attrib = method.GetCustomAttribute <TestCleanupAttribute>();
                    if (attrib != null)
                    {
                        methods.Add(typeof(TestCleanupAttribute), method);
                    }
                }

                if (methods.Count == 4)
                {
                    break;
                }
            }

            testContext = new TestContextEx(testName, classId, new StringWriter(), this.TestContext.Properties);
            methodInfo  = classType.GetMethod(testName);
            testObject  = new PrivateObject(classType);

            TestInfo test = new TestInfo()
            {
                IsChild = true,
                Class   = new ClassObject()
                {
                    Assembly    = GetAssemblyName(),
                    Description = GetClassDescription(),
                    FullName    = classType.FullName
                },
                Test = new TestObject()
                {
                    FullName    = testId,
                    Description = GetTestDescription(methodInfo),
                    Categories  = GetTestCategories(methodInfo)
                }
            };

            // Running the ClassInitialize method
            if (methods.ContainsKey(typeof(ClassInitializeAttribute)))
            {
                testObject.Invoke(methods[typeof(ClassInitializeAttribute)].Name, BindingFlags.Public | BindingFlags.Static, new object[] { testContext });

                testObject.SetProperty("TestContext", testContext);
            }

            Report.AddTestNode(test);

            // Running the TestInitialize method
            if (methods.ContainsKey(typeof(TestInitializeAttribute)))
            {
                testObject.Invoke(methods[typeof(TestInitializeAttribute)].Name);
            }

            // Running the test
            retVal = testObject.Invoke(testName, args);

            // Running the TestInitialize method
            if (methods.ContainsKey(typeof(TestInitializeAttribute)))
            {
                testObject.Invoke(methods[typeof(TestInitializeAttribute)].Name);
            }

            Report.TestEnd(test);

            // Running the ClassCleanup method
            if (methods.ContainsKey(typeof(ClassCleanupAttribute)))
            {
                testObject.Invoke(methods[typeof(ClassCleanupAttribute)].Name, BindingFlags.Public | BindingFlags.Static);
            }

            return(retVal);
        }
Beispiel #3
0
 public TestInfo TestEnd(TestInfo test)
 {
     return(_reportManagerHandler.TestEnd(test));
 }