从带反射的代码中运行Xunit和UI Tests测试

时间:2015-01-07 22:36:52

标签: c# coded-ui-tests xunit

我正在尝试从MTM运行CodedUI测试以及在我们的代码库上运行Xunit测试。我应该能够根据我的要求从一个项目运行这些测试,以便以后能够收集测试数据。我已经能够使用反射在同一代码中的两个实例中运行测试,以根据各自的属性调用测试方法。

问题是,然后对于UI和Xunit测试,测试资源管理器都没有注册测试正在通过或失败。我假设测试环境必须进行一些初始化,但我找不到任何资源。

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodedUITestProject1;
using Microsoft.VisualStudio.TestTools.UITesting;
using CodedUITestProject2;
using System.Reflection;
using ConsoleApplication1;

namespace Run_UI_From_Outside
{
    class Program
    {

        static void Main(string[] args)
        {

// This section uses reflection to find Test Methods in the CodedUITest class bases on their attributes
            var assembly = typeof(CodedUITestProject2.CodedUITest1).Assembly;
            var attributes = assembly.GetCustomAttributes(typeof(CodedUITestProject2.CodedUITest1), true);
            var types = assembly.GetTypes();

            foreach (var t in types)
            {

                var ca = t.CustomAttributes;
                foreach (var item in ca)
                {

                    if (item.AttributeType == typeof(Microsoft.VisualStudio.TestTools.UITesting.CodedUITestAttribute))
                    {

                        object myInstance = Activator.CreateInstance(t);
                        MethodInfo myInstanceMethod = t.GetMethod("CodedUITestMethod1");
                        Playback.Initialize();
                        var test = myInstanceMethod.Invoke(myInstance, null);
                        Playback.Cleanup();
                    }
                }
            }


//  This section uses reflection to find Xunit Methods in the CodedUITest class bases on their attributes
            var assemblyXunit = typeof(ConsoleApplication1.Tests.SampleTest).Assembly;
            var xunitMethods = assemblyXunit.GetTypes().SelectMany(t => t.GetMethods())
                .Where(a => a.GetCustomAttributes(typeof(Xunit.FactAttribute), false).Length > 0)
                .ToArray();


            foreach (var t in xunitMethods)
            {
                    object myMethodInstance = Activator.CreateInstance(t.DeclaringType);
                    t.Invoke(myMethodInstance, null); 
            }



        }
    }
}

0 个答案:

没有答案
相关问题