如何检查项目是否为测试项目?(NUnit、MSTest、xUnit) [英] How can I check if project is a Test Project? (NUnit, MSTest, xUnit)

查看:43
本文介绍了如何检查项目是否为测试项目?(NUnit、MSTest、xUnit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查所选项目(我有源代码)是否是以下框架之一的 TestProject:NUnit、MSTest、xUnit.

I want to check if selected project (I have source code) is a TestProject for one of the following framework: NUnit, MSTest, xUnit.

对于 MSTest 来说很简单.我可以检查 .csproj 并标记.如果我有 {3AC096D0-A1C2-E12C-1390-A8335801FDAB},则表示它是测试项目.

For MSTest it is simple. I can check .csproj and tag. If I have there {3AC096D0-A1C2-E12C-1390-A8335801FDAB} than it means it is Test project.

问题是 NUnit 和 xUnit.我可以在 .csproj 中检查此案例参考.如果我有 nunit.framework 或 xunit 这将是显而易见的.但我想知道是否有可能以不同的方式检查这一点.

The problem are NUnit and xUnit. I can check for this cases references in .csproj. If I have nunit.framework or xunit it will be obvious. But I wondering if is possible to check this in diffrent way.

您知道识别测试项目的不同方法吗?

Do you know different way to recognize test projects?

推荐答案

方法之一是检查程序集是否包含测试方法.测试方法的属性如下:

One of the way is to check if assembly contains test methods. Attributes for test methods are as following:

  • NUnit:[测试]
  • MSTest:[TestMethod]
  • xUnit.net:[事实]

迭代程序集并检查程序集是否包含带有测试方法的类.示例代码:

Iterate over assemblies and check if assembly contains class with test methods. Example code:

bool IsAssemblyWithTests(Assembly assembly)
{
    var testMethodTypes = new[]
    {
        typeof(Xunit.FactAttribute),
        typeof(NUnit.Framework.TestAttribute),
        typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute)
    };

    foreach (var type in assembly.GetTypes())
    {
        if (HasAttribute(type, testMethodTypes)) return true;
    }
    return false;
}

bool HasAttribute(Type type, IEnumerable<Type> testMethodTypes)
{
    foreach (Type testMethodType in testMethodTypes)
    {
        if (type.GetMethods().Any(x => x.GetCustomAttributes(testMethodType, true).Any())) return true;
    }

    return false;
}

您还可以添加更多假设:

You can also add more assumptions:

  • 检查类是否包含 TestFixture 方法,
  • 检查类/测试方法是否公开.

如果您需要使用 C# Parser,这里有一个 NRefactory 代码示例,用于检查 .cs 文件是否包含带有测试的类:

If you need to use C# Parser, here is a sample of NRefactory code for checking if a .cs file contains classes with tests:

string[] testAttributes = new[]
    {
        "TestMethod", "TestMethodAttribute", // MSTest
        "Fact", "FactAttribute", // Xunit
        "Test", "TestAttribute", // NUnit
    };

bool ContainsTests(IEnumerable<TypeDeclaration> typeDeclarations)
{
    foreach (TypeDeclaration typeDeclaration in typeDeclarations)
    {
        foreach (EntityDeclaration method in typeDeclaration.Members.Where(x => x.EntityType == EntityType.Method))
        {
            foreach (AttributeSection attributeSection in method.Attributes)
            {
                foreach (Attribute atrribute in attributeSection.Attributes)
                {
                    var typeStr = atrribute.Type.ToString();
                    if (testAttributes.Contains(typeStr)) return true;
                }
            }
        }
    }

    return false;
}

NRefactory .cs 文件解析示例:

Example of NRefactory .cs file parsing:

var stream = new StreamReader("Class1.cs").ReadToEnd();
var syntaxTree = new CSharpParser().Parse(stream);
IEnumerable<TypeDeclaration> classes = syntaxTree.DescendantsAndSelf.OfType<TypeDeclaration>();

这篇关于如何检查项目是否为测试项目?(NUnit、MSTest、xUnit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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