Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable internal testclass discovery #944

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Enable internal testclass discovery (#937)
* Enable discovery of internal test classes

It's useful to extract sets of test methods which establish some common criteria into reusable, generic, abstract test classes from which concrete test classes for specific types can be derived.

However, this technique requires that the type under test be as accessible as the test class, itself. In particular, it's a compile-time error to have a public class derived from a generic class parameterized with an internal type. So, either the type must be made public or the test class must be made internal, in order for the compiler to be satisfied.

Before the change in this PR, the test framework would only discover tests which were declared in public classes. This meant that the option of declaring the test class internal wasn't really an option at all: doing so would prevent the tests from running. So, types which should have been internal were marked public, just so they could be tested.

The following code reiterates the dilemma. After this commit, the tests in the ```FancyStringsAreCaseInsensitive``` class will be discovered.

FancyStringTets.cs
```C#
using System;
using DevProject1;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    [TestClass]
    public class FancyStringTests
    {
        [TestClass]
        // This class will not be discovered by MSTest because it is internal rather than public.
        // But, since FancyString is internal, marking the test class public will produce a compilation error.
        // This forces us to either make FancyString public or abandon this technique for reusing test classes.
        internal class FancyStringsAreCaseInsensitive : CaseInsensitivityTests<FancyString>
        {
            protected override Tuple<FancyString, FancyString> EquivalentInstancesWithDistinctCasing { get; }
        }
    }
}
```

FancyString.cs
```C#
using System;

namespace DevProject1
{
    internal class FancyString
    {
        // TODO
    }
}
```

CaseInsensitivityTests.cs
```C#
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProject1
{
    internal abstract class CaseInsensitivityTests<T>
    {
        protected abstract Tuple<T, T> EquivalentInstancesWithDistinctCasing { get; }

        [TestMethod]
        public void EqualityIsCaseInsensitive()
        {
            var (left, right) = EquivalentInstancesWithDistinctCasing;
            Assert.AreEqual(left, right);
        }

        [TestMethod]
        public void HashCodeIsCaseInsensitive()
        {
            // TODO
        }
    }
}
```

* Add AnInternalTestClassDerivedFromAPublicAbstractGenericBaseClassForAnInternalTypeIsDiscovered

This test establishes support for the principal motivating scenario for the discovery of internal test classes.

* Add support for the discovery of internal test methods

This commit renames DiscoverInternalTestClassesAttribute to DiscoverInternalsAttribute, and causes the presence of that attribute in the assembly to additionally discover test methods which are declared internal, in addition to test methods which are declared public.

Co-authored-by: nohwnd <me@jakubjares.com>
Co-authored-by: Medeni Baykal <433724+Haplois@users.noreply.github.com>
  • Loading branch information
Haplois and nohwnd committed Aug 24, 2021
commit 7a2e3b7a255be8f59373da9ce5eb5d292e5be92e
83 changes: 55 additions & 28 deletions TestFx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WinUI", "WinUI", "{35D010CC
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReferencedProjectFromDataSourceTest", "test\E2ETests\TestAssets\ReferencedProjectFromDataSourceTest\ReferencedProjectFromDataSourceTest.csproj", "{6B4DE65C-4162-4C52-836A-8F9FA901814A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscoverInternalsProject", "test\E2ETests\TestAssets\DiscoverInternalsProject\DiscoverInternalsProject.csproj", "{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Adapter\PlatformServices.Shared\PlatformServices.Shared.projitems*{2177c273-ae07-43b3-b87a-443e47a23c5a}*SharedItemsImports = 13
Expand Down Expand Up @@ -464,14 +466,14 @@ Global
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Code Analysis Debug|x64.Build.0 = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Code Analysis Debug|x86.ActiveCfg = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Code Analysis Debug|x86.Build.0 = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|ARM.ActiveCfg = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|ARM.Build.0 = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|Any CPU.ActiveCfg = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|Any CPU.Build.0 = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|ARM.ActiveCfg = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|ARM.Build.0 = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|x64.ActiveCfg = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|x64.Build.0 = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|x86.ActiveCfg = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|x86.Build.0 = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|x86.ActiveCfg = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Debug|x86.Build.0 = Debug|x64
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Release|Any CPU.Build.0 = Debug|Any CPU
{0C139DA9-AA81-43A0-8625-7A873C0E2873}.Release|ARM.ActiveCfg = Release|Any CPU
Expand All @@ -488,14 +490,14 @@ Global
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Code Analysis Debug|x64.Build.0 = Debug|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Code Analysis Debug|x86.ActiveCfg = Debug|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Code Analysis Debug|x86.Build.0 = Debug|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|Any CPU.Build.0 = Release|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|ARM.ActiveCfg = Debug|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|ARM.Build.0 = Debug|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|Any CPU.ActiveCfg = Release|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|Any CPU.Build.0 = Release|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|ARM.ActiveCfg = Debug|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|ARM.Build.0 = Debug|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|x64.ActiveCfg = Debug|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|x64.Build.0 = Debug|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|x86.ActiveCfg = Debug|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|x86.Build.0 = Debug|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|x86.ActiveCfg = Debug|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Debug|x86.Build.0 = Debug|x64
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Release|Any CPU.Build.0 = Release|Any CPU
{C6AC27D2-975B-4CF3-B16B-9C17907B2268}.Release|ARM.ActiveCfg = Release|Any CPU
Expand All @@ -512,12 +514,12 @@ Global
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Code Analysis Debug|x64.Build.0 = Debug|Any CPU
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Code Analysis Debug|x86.ActiveCfg = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Code Analysis Debug|x86.Build.0 = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|ARM.ActiveCfg = Debug|Any CPU
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|ARM.Build.0 = Debug|Any CPU
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|x64.ActiveCfg = Debug|Any CPU
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|x64.Build.0 = Debug|Any CPU
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|Any CPU.ActiveCfg = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|Any CPU.Build.0 = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|ARM.ActiveCfg = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|ARM.Build.0 = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|x64.ActiveCfg = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|x64.Build.0 = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|x86.ActiveCfg = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Debug|x86.Build.0 = Debug|x86
{0C522C84-B5AF-4DB6-96A2-9EFC15C88589}.Release|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -536,12 +538,12 @@ Global
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Code Analysis Debug|x64.Build.0 = Debug|Any CPU
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Code Analysis Debug|x86.ActiveCfg = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Code Analysis Debug|x86.Build.0 = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|Any CPU.Build.0 = Release|Any CPU
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|ARM.ActiveCfg = Debug|Any CPU
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|ARM.Build.0 = Debug|Any CPU
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|x64.ActiveCfg = Debug|Any CPU
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|x64.Build.0 = Debug|Any CPU
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|Any CPU.ActiveCfg = Release|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|Any CPU.Build.0 = Release|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|ARM.ActiveCfg = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|ARM.Build.0 = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|x64.ActiveCfg = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|x64.Build.0 = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|x86.ActiveCfg = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Debug|x86.Build.0 = Debug|x86
{293597CE-CCF7-4C09-9ADD-17BF34C6E86F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -590,8 +592,8 @@ Global
{771B0933-B811-4002-B455-823C1CC96505}.Debug|ARM.Build.0 = Debug|Any CPU
{771B0933-B811-4002-B455-823C1CC96505}.Debug|x64.ActiveCfg = Debug|x64
{771B0933-B811-4002-B455-823C1CC96505}.Debug|x64.Build.0 = Debug|x64
{771B0933-B811-4002-B455-823C1CC96505}.Debug|x86.ActiveCfg = Debug|Any CPU
{771B0933-B811-4002-B455-823C1CC96505}.Debug|x86.Build.0 = Debug|Any CPU
{771B0933-B811-4002-B455-823C1CC96505}.Debug|x86.ActiveCfg = Debug|x64
{771B0933-B811-4002-B455-823C1CC96505}.Debug|x86.Build.0 = Debug|x64
{771B0933-B811-4002-B455-823C1CC96505}.Release|Any CPU.ActiveCfg = Release|Any CPU
{771B0933-B811-4002-B455-823C1CC96505}.Release|Any CPU.Build.0 = Release|Any CPU
{771B0933-B811-4002-B455-823C1CC96505}.Release|ARM.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -1262,8 +1264,8 @@ Global
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Debug|ARM.Build.0 = Debug|ARM
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Debug|x64.ActiveCfg = Debug|x64
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Debug|x64.Build.0 = Debug|x64
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Debug|x86.ActiveCfg = Debug|x86
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Debug|x86.Build.0 = Debug|x86
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Debug|x86.ActiveCfg = Debug|Any CPU
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Debug|x86.Build.0 = Debug|Any CPU
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Release|Any CPU.Build.0 = Release|Any CPU
{F4E2876F-6E42-4DCF-B629-041A9DF7C579}.Release|ARM.ActiveCfg = Release|ARM
Expand Down Expand Up @@ -1296,6 +1298,30 @@ Global
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|x64.Build.0 = Release|Any CPU
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|x86.ActiveCfg = Release|Any CPU
{6B4DE65C-4162-4C52-836A-8F9FA901814A}.Release|x86.Build.0 = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|Any CPU.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|ARM.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|ARM.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|x64.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|x64.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|x86.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Code Analysis Debug|x86.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|ARM.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|ARM.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|x64.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|x64.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|x86.ActiveCfg = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Debug|x86.Build.0 = Debug|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|Any CPU.Build.0 = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|ARM.ActiveCfg = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|ARM.Build.0 = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|x64.ActiveCfg = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|x64.Build.0 = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|x86.ActiveCfg = Release|Any CPU
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1366,6 +1392,7 @@ Global
{F4E2876F-6E42-4DCF-B629-041A9DF7C579} = {24088844-2107-4DB2-8F3F-CBCA94FC4B28}
{35D010CC-CDF2-4115-BCFB-E2E3D21C1055} = {CA01DAF5-8D9D-496E-9AD3-94BB7FBB2D34}
{6B4DE65C-4162-4C52-836A-8F9FA901814A} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
{44A504D9-A0D6-427D-BFB2-DB144A74F0D5} = {D53BD452-F69F-4FB3-8B98-386EDA28A4C8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {31E0F4D5-975A-41CC-933E-545B2201FAF9}
Expand Down
16 changes: 10 additions & 6 deletions src/Adapter/MSTest.CoreAdapter/Discovery/AssemblyEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ internal ICollection<UnitTestElement> EnumerateAssembly(string assemblyFileName,

var types = this.GetTypes(assembly, assemblyFileName, warningMessages);

var discoverInternals = assembly.GetCustomAttribute<UTF.DiscoverInternalsAttribute>() != null;

foreach (var type in types)
{
if (type == null)
{
continue;
}

var testsInType = this.DiscoverTestsInType(assemblyFileName, runSettingsXml, assembly, type, warningMessages);
var testsInType = this.DiscoverTestsInType(assemblyFileName, runSettingsXml, assembly, type, warningMessages, discoverInternals);
tests.AddRange(testsInType);
}

Expand Down Expand Up @@ -193,16 +195,18 @@ internal string GetLoadExceptionDetails(ReflectionTypeLoadException ex)
/// </summary>
/// <param name="type">The type to enumerate.</param>
/// <param name="assemblyFileName">The reflected assembly name.</param>
/// <param name="discoverInternals">True to discover test classes which are declared internal in
/// addition to test classes which are declared public.</param>
/// <returns>a TypeEnumerator instance.</returns>
internal virtual TypeEnumerator GetTypeEnumerator(Type type, string assemblyFileName)
internal virtual TypeEnumerator GetTypeEnumerator(Type type, string assemblyFileName, bool discoverInternals = false)
{
var typeValidator = new TypeValidator(ReflectHelper);
var testMethodValidator = new TestMethodValidator(ReflectHelper);
var typeValidator = new TypeValidator(ReflectHelper, discoverInternals);
var testMethodValidator = new TestMethodValidator(ReflectHelper, discoverInternals);

return new TypeEnumerator(type, assemblyFileName, ReflectHelper, typeValidator, testMethodValidator);
}

private IEnumerable<UnitTestElement> DiscoverTestsInType(string assemblyFileName, string runSettingsXml, Assembly assembly, Type type, List<string> warningMessages)
private IEnumerable<UnitTestElement> DiscoverTestsInType(string assemblyFileName, string runSettingsXml, Assembly assembly, Type type, List<string> warningMessages, bool discoverInternals = false)
{
var sourceLevelParameters = PlatformServiceProvider.Instance.SettingsProvider.GetProperties(assemblyFileName);
sourceLevelParameters = RunSettingsUtilities.GetTestRunParameters(runSettingsXml)?.ConcatWithOverwrites(sourceLevelParameters)
Expand All @@ -215,7 +219,7 @@ private IEnumerable<UnitTestElement> DiscoverTestsInType(string assemblyFileName
try
{
typeFullName = type.FullName;
var unitTestCases = this.GetTypeEnumerator(type, assemblyFileName).Enumerate(out var warningsFromTypeEnumerator);
var unitTestCases = this.GetTypeEnumerator(type, assemblyFileName, discoverInternals).Enumerate(out var warningsFromTypeEnumerator);
var typeIgnored = ReflectHelper.IsAttributeDefined(type, typeof(UTF.IgnoreAttribute), false);

if (warningsFromTypeEnumerator != null)
Expand Down
18 changes: 17 additions & 1 deletion src/Adapter/MSTest.CoreAdapter/Discovery/TestMethodValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,27 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery
internal class TestMethodValidator
{
private readonly ReflectHelper reflectHelper;
private readonly bool discoverInternals;

/// <summary>
/// Initializes a new instance of the <see cref="TestMethodValidator"/> class.
/// </summary>
/// <param name="reflectHelper">An instance to reflection helper for type information.</param>
internal TestMethodValidator(ReflectHelper reflectHelper)
: this(reflectHelper, false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TestMethodValidator"/> class.
/// </summary>
/// <param name="reflectHelper">An instance to reflection helper for type information.</param>
/// <param name="discoverInternals">True to discover methods which are declared internal in addition to methods
/// which are declared public.</param>
internal TestMethodValidator(ReflectHelper reflectHelper, bool discoverInternals)
{
this.reflectHelper = reflectHelper;
this.discoverInternals = discoverInternals;
}

/// <summary>
Expand All @@ -51,10 +64,13 @@ internal virtual bool IsValidTestMethod(MethodInfo testMethodInfo, Type type, IC
return false;
}

var isAccessible = testMethodInfo.IsPublic
|| (this.discoverInternals && testMethodInfo.IsAssembly);

// Todo: Decide whether parameter count matters.
// The isGenericMethod check below id to verify that there are no closed generic methods slipping through.
// Closed generic methods being GenericMethod<int> and open being GenericMethod<T>.
var isValidTestMethod = testMethodInfo.IsPublic && !testMethodInfo.IsAbstract && !testMethodInfo.IsStatic
var isValidTestMethod = isAccessible && !testMethodInfo.IsAbstract && !testMethodInfo.IsStatic
&& !testMethodInfo.IsGenericMethod
&& testMethodInfo.IsVoidOrTaskReturnType();

Expand Down
Loading