C#查找解决方案中的所有测试 [英] C# Find all tests in a solution

查看:44
本文介绍了C#查找解决方案中的所有测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解决方案中是否有我可以用来标注为测试的所有技术( [TestMethod] [Fact] [理论] ...)?

Are there any technologies I can use to find all tests in a solution that are annotated as a test ([TestMethod], [Fact], [Theory]...)?

上下文:我对这一切还很陌生,但尝试制定一种持续的部署策略.我想在发布过程中/之后找到并运行解决方案中的所有测试.这些不是依赖于构建的单元测试,而是端对端解决方案测试的一部分.

Context: I am quite new to all this but attempting to iron out a continuous deployment strategy. I want to find and run all tests in a solution during/following a release process. These are not build dependent unit tests, these are part of end to end solution tests.

请让我知道我的想法是否偏离正确的方向.

Please let me know if my thinking is away from the right track.

推荐答案

因此,如果您想要一个仅使用TestAttribute收集方法的工具,Visual Studio就会内置该工具.它是Test Explorer,它检查解决方案中的程序集并根据属性挖掘出测试,然后让您运行它们.它在某些版本的VS中可用(我使用的是Professional).我不确定到底是哪个.

So if you want a tool that just gathers methods with the TestAttribute, Visual Studio has that built in. It's the Test Explorer, which examines the assemblies in your solution and digs out tests based on the attributes, then lets you run them. It's available in some versions of VS (I use Professional, which has it); I'm not sure exactly which ones do, though.

通常要为此目的编写应用程序.但是,您可以自己扫描部件以查看这些属性.这里并不是真的有必要-正如我说的那样,这里有一些测试工具可以做到这一点,而VSTS之类的东西将帮助您集成到部署过程中-但由于您提到了所有这些,因此我将将其保留为将来可能需要或可能不需要使用的东西.如果没有别的,您可以将其修改为其他内容.

Usually apps written for this purpose are the way to go. However, you could scan your assemblies yourself for these attributes. It's not really necessary here -- as I said there are testing tools out here that can do that, and things like VSTS will help you integrate into a deployment process -- but since you mentioned you're new to all of this I'll leave it here as something you may or may not need to use in the future. If nothing else, you can adapt it to something else.

基本上,您可以使用Reflection获取程序集,类,方法等,还可以获取装饰它们的属性.您可以这样:

Basically, you can use Reflection to get your assemblies, classes, methods, etc. and you can get the attributes that decorate each of them. You'd do it like this:

[TestClass]
public class AttributeFinder
{
    [TestMethod]
    public void MyTestMethod()
    {
        // Do something
    }

    public static void FindAttributes()
    {
        var assembly = Assembly.GetAssembly(typeof(AttributeFinder));
        var types = assembly.GetTypes();
        foreach (var t in types)
        {
            var typeAttr = t.GetCustomAttribute(typeof(TestClassAttribute));
            if (typeAttr != null)
            {
                // Your class has a TestClass attribute on it!
            }

            var methods = t.GetMethods();
            foreach (var m in methods)
            {
                var methodAttr = m.GetCustomAttribute(typeof(TestMethodAttribute));
                if (methodAttr != null) 
                {
                    // This method has the TestMethod attribute!
                }
            }
        }
    }
}

同样,有些工具会自动为您完成,并且QHero为您提供了一些链接.我在这里介绍的内容很长,需要您编写自己的工具,而那里的东西并不需要.但是,反射是一种很容易掌握的技术,例如,在生成列出测试方法/类或其他内容的测试工具之外的报告时,这可能很有用.

Again, there are tools that do this for you automatically, and QHero provided you some links. What I've presented here is the long way around and would require you writing your own tooling, which isn't necessary with the stuff out there. But Reflection is a handy technique to know, and this might be useful to, say, generate a report outside of testing tools that lists your test methods/classes or something.

这篇关于C#查找解决方案中的所有测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆