2013-11-22 8 views
1

선택한 프로젝트 (소스 코드 있음)가 NUnit, MSTest, xUnit 중 하나의 프레임 워크에 대한 TestProject인지 확인하고 싶습니다.프로젝트가 테스트 프로젝트인지 어떻게 확인할 수 있습니까? (NUnit, MSTest, xUnit)

MSTest의 경우 간단합니다. .csproj와 태그를 확인할 수 있습니다. {3AC096D0-A1C2-E12C-1390-A8335801FDAB}이 있으면 테스트 프로젝트임을 의미합니다.

문제는 NUnit 및 xUnit입니다. .csproj에서이 사례 참조를 확인할 수 있습니다. 내가 nunit.framework 또는 xunit을 가지고 있다면 그것은 명백 할 것이다. 하지만이 방법을 다른 방식으로 확인할 수 있는지 궁금합니다.

테스트 프로젝트를 인식하는 다른 방법을 알고 계십니까?

+0

@Krzystof : 업데이트 된 솔루션에 만족하십니까? –

+0

네, 잘 작동합니다. Thx 도움. – Krzysztof

답변

2

하나 방법은 어셈블리에 테스트 메소드가 있는지 확인하는 것입니다.

  • 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 방법을 포함하는 경우,
  • 검사 클래스/테스트 방법을 공개하는 경우.

편집 :

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

당신이 C# 파서를 사용해야하는 경우, 여기에 .cs 파일이 테스트와 클래스가 포함되어있는 경우 확인하기위한 NRefactory 코드의 샘플입니다

NRefactory .cs 파일 파싱의 예 :

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

컴파일 된 프로젝트에서는 훌륭하게 작동하지만 컴파일을 피하고 싶습니다. 컴파일 할 수는 있지만 앱을 사용하면 시간이 오래 걸릴 것입니다. 내 애플 리케이션 NRefactory 어쩌면 당신이 위에서 언급 한 거기에 기능을 추가하는 좋은 방법을 사용하고 있습니다. Thx – Krzysztof

+0

@Krzysztof : NRefactory 샘플을 추가했습니다. –

1

각 프레임 워크를 나타내는 속성을 사용하여 어느 것이 어떤 것인지 확인합니다. 반사 해당 속성 유형 클래스/메소드를 찾을 수

사용 (예 : Test/TestFixture)이 대답은 당신이 당신의 요구에 맞게 수정할 수 있습니다 예를 들어이

:의

get all types in assembly with custom attribute