【问题标题】:How can I check if project is a Test Project? (NUnit, MSTest, xUnit)如何检查项目是否为测试项目? (NUnit、MSTest、xUnit)
【发布时间】:2013-11-22 18:54:44
【问题描述】:

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

对于 MSTest,这很简单。我可以检查 .csproj 和标签。如果我有 {3AC096D0-A1C2-E12C-1390-A8335801FDAB} 则意味着它是测试项目。

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

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

【问题讨论】:

  • @Krzystof:您对更新的解决方案满意吗?
  • 是的,效果很好。谢谢帮助。

标签: visual-studio testing nunit mstest xunit.net


【解决方案1】:

其中一种方法是检查程序集是否包含测试方法。测试方法的属性如下:

  • NUnit:[Test]
  • MSTest:[TestMethod]
  • xUnit.net:[Fact]

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

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;
}

您还可以添加更多假设:

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

编辑:

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

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 文件解析示例:

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

【讨论】:

  • 它适用于已编译的项目,但我想避免编译。我可以编译,但它会使我的应用程序更耗时。我在我的应用程序 NRefactory 中使用它,也许它会添加你上面提到的功能。谢谢
  • @Krzysztof:添加了 NRefactory 示例。
【解决方案2】:

我会寻找代表每个框架的属性的用法,看看哪个是哪个。

使用反射查找具有适当属性类型的类/方法(例如Test/TestFixture

此答案有一个示例,您可以修改以满足您的需求:

get all types in assembly with custom attribute

【讨论】:

    【解决方案3】:

    检查 *.csproj 中的 NuGet 引用以获取用于测试的特定块。

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多