diff --git a/source/TestAdapter/Discover.cs b/source/TestAdapter/Discover.cs index 143648b..927ba2b 100644 --- a/source/TestAdapter/Discover.cs +++ b/source/TestAdapter/Discover.cs @@ -120,7 +120,7 @@ public static List ComposeTestCases(string sourceFile) AppDomain.CurrentDomain.Load(test.GetName()); var typeCandidatesForTests = test.GetTypes() - .Where(x => x.IsClass); + .Where(Helper.IsTestClassCandidate); foreach (var typeCandidate in typeCandidatesForTests) { diff --git a/source/TestFrameworkShared/Helper.cs b/source/TestFrameworkShared/Helper.cs index 5041f01..570b38c 100644 --- a/source/TestFrameworkShared/Helper.cs +++ b/source/TestFrameworkShared/Helper.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; + namespace nanoFramework.TestFramework { /// @@ -10,6 +12,43 @@ public static class Helper { private delegate bool AnyDelegateType(object source); + /// + /// Checks whether a type can be considered for test discovery and execution. + /// + /// Type to inspect. + /// when the type is a class and not an attribute type. + public static bool IsTestClassCandidate(Type type) + { + return type.IsClass && !IsAttributeType(type); + } + + private static bool IsAttributeType(Type type) + { + var attributeFullName = typeof(Attribute).FullName; + + Type current = type; + while (current != null) + { + if (current.FullName == attributeFullName) + { + return true; + } + try + { + current = current.BaseType; + } + catch + { + // Base type assembly not resolvable in this reflection context; + // conservatively treat as attribute to avoid calling GetCustomAttributes on it. + return true; + } + } + + return false; + + } + private static bool Any(this object[] array, AnyDelegateType predicate) { foreach (var item in array) diff --git a/source/UnitTestLauncher/Program.cs b/source/UnitTestLauncher/Program.cs index b88fc50..1e6bcb2 100644 --- a/source/UnitTestLauncher/Program.cs +++ b/source/UnitTestLauncher/Program.cs @@ -22,7 +22,7 @@ public static void Main() foreach (var type in allTypes) { - if (!type.IsClass) + if (!Helper.IsTestClassCandidate(type)) { continue; }