One of the way is to check if assembly contains test methods. Attributes for test methods are as following:
- NUnit:
[Test]
- MSTest:
[TestMethod]
- xUnit.net:
[Fact]
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:
- check if classes contains TestFixture method,
- check if classes / test methods are public.
EDIT:
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;
}
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>();