하나 방법은 어셈블리에 테스트 메소드가 있는지 확인하는 것입니다.
- 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>();
@Krzystof : 업데이트 된 솔루션에 만족하십니까? –
네, 잘 작동합니다. Thx 도움. – Krzysztof