Skip to content

Perf improvements #1

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions xunit.runner.visualstudio.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Supported platforms:
<file src="xunit.runner.visualstudio.testadapter\bin\Release\xunit.runner.utility.desktop.dll" target="build\_common\" />
<file src="xunit.runner.visualstudio.testadapter\bin\Release\xunit.runner.visualstudio.testadapter.dll" target="build\_common\" />
<file src="xunit.runner.visualstudio.wpa81\bin\Release\xunit.runner.utility.dotnet.dll" target="build\_common\" />
<file src="xunit.runner.visualstudio.testadapter\bin\Release\Newtonsoft.Json.dll" target="build\_common\" />
<file src="xunit.runner.visualstudio.testadapter\bin\Release\xunit.core.dll" target="build\_common\" />
<file src="xunit.runner.visualstudio.testadapter\bin\Release\xunit.execution.desktop.dll" target="build\_common\" />

<file src="build\xunit.runner.visualstudio.v1.props" target="build\net20\xunit.runner.visualstudio.props" />

Expand Down
24 changes: 22 additions & 2 deletions xunit.runner.visualstudio.testadapter/Sinks/VsDiscoverySink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Xunit.Abstractions;
using Xunit.Sdk;
using Newtonsoft.Json;

#if PLATFORM_DOTNET
using System.Reflection;
Expand Down Expand Up @@ -65,14 +67,32 @@ public static TestCase CreateVsTestCase(string source, ITestFrameworkDiscoverer
{
try
{
var serializedTestCase = discoverer.Serialize(xunitTestCase);
var fqTestMethodName = $"{xunitTestCase.TestMethod.TestClass.Class.Name}.{xunitTestCase.TestMethod.Method.Name}";
var uniqueName = forceUniqueNames ? $"{fqTestMethodName} ({xunitTestCase.UniqueID})" : fqTestMethodName;

var result = new TestCase(uniqueName, uri, source) { DisplayName = Escape(xunitTestCase.DisplayName) };
result.SetPropertyValue(VsTestRunner.SerializedTestCaseProperty, serializedTestCase);

if (VsTestRunner.AppDomain != AppDomainSupport.Denied)
{
var serializedTestCase = discoverer.Serialize(xunitTestCase);
result.SetPropertyValue(VsTestRunner.SerializedTestCaseProperty, serializedTestCase);
}
result.Id = GuidFromString(uri + xunitTestCase.UniqueID);

if (xunitTestCase.TestMethodArguments != null)
{
var serializedTestArguments = JsonConvert.SerializeObject(xunitTestCase.TestMethodArguments, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
result.SetPropertyValue(VsTestRunner.SerializedTestCaseArgumentProperty, serializedTestArguments);
}

if (xunitTestCase is XunitTheoryTestCase)
{
result.SetPropertyValue(VsTestRunner.TheoryAgrumentProperty, "Theory");
}

if (addTraitThunk != null)
{
foreach (var key in xunitTestCase.Traits.Keys)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Xunit.Abstractions;

namespace Xunit.Runner.VisualStudio
{
Expand All @@ -8,5 +9,6 @@ public class AssemblyRunInfo
public string AssemblyFileName;
public TestAssemblyConfiguration Configuration;
public IList<TestCase> TestCases;
public Dictionary<ITestCase, TestCase> TestCaseMap;
}
}
182 changes: 171 additions & 11 deletions xunit.runner.visualstudio.testadapter/VsTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Xunit.Abstractions;
using System.Reflection;
using Xunit.Sdk;
using Newtonsoft.Json;

#if !PLATFORM_DOTNET
using System.Xml;
Expand All @@ -24,11 +27,13 @@ namespace Xunit.Runner.VisualStudio.TestAdapter
public class VsTestRunner : ITestDiscoverer, ITestExecutor
{
public static TestProperty SerializedTestCaseProperty = GetTestProperty();
public static TestProperty SerializedTestCaseArgumentProperty = GetTestArgumentProperty();
public static TestProperty TheoryAgrumentProperty = GetTheoryAgrumentProperty();

#if PLATFORM_DOTNET
static readonly AppDomainSupport AppDomainDefaultBehavior = AppDomainSupport.Denied;
internal static AppDomainSupport AppDomain = AppDomainSupport.Denied;
#else
static readonly AppDomainSupport AppDomainDefaultBehavior = AppDomainSupport.Required;
internal static AppDomainSupport AppDomain = AppDomainSupport.Required;
#endif

static readonly HashSet<string> platformAssemblies = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
Expand Down Expand Up @@ -111,10 +116,16 @@ void ITestExecutor.RunTests(IEnumerable<TestCase> tests, IRunContext runContext,
var stopwatch = Stopwatch.StartNew();
var logger = new LoggerHelper(frameworkHandle, stopwatch);

Dictionary<ITestCase, TestCase> testCaseMap = null;
if (AppDomain == AppDomainSupport.Denied)
{
testCaseMap = GetXunitTestCaseMap(tests);
}

RunTests(
runContext, frameworkHandle, logger,
() => tests.GroupBy(testCase => testCase.Source)
.Select(group => new AssemblyRunInfo { AssemblyFileName = group.Key, Configuration = LoadConfiguration(group.Key), TestCases = group.ToList() })
.Select(group => new AssemblyRunInfo { AssemblyFileName = group.Key, Configuration = LoadConfiguration(group.Key), TestCases = group.ToList(), TestCaseMap = testCaseMap })
.ToList()
);
}
Expand Down Expand Up @@ -257,6 +268,12 @@ static Stream GetConfigurationStreamForAssembly(string assemblyName)
static TestProperty GetTestProperty()
=> TestProperty.Register("XunitTestCase", "xUnit.net Test Case", typeof(string), typeof(VsTestRunner));

static TestProperty GetTestArgumentProperty()
=> TestProperty.Register("XunitTestCaseArgument", "xUnit.net Test Case Argument", typeof(string), typeof(object[]));

static TestProperty GetTheoryAgrumentProperty()
=> TestProperty.Register("TheoryAgrument", "xUnit.net Theory Argument", typeof(string), typeof(object[]));

List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger, IRunContext runContext)
{
// For store apps, the files are copied to the AppX dir, we need to load it from there
Expand All @@ -280,16 +297,45 @@ List<AssemblyRunInfo> GetTests(IEnumerable<string> sources, LoggerHelper logger,
vsFilteredTestCases = filterHelper.GetFilteredTestList(vsFilteredTestCases, runContext, logger, source).ToList();

// Re-create testcases with unique names if there is more than 1
var testCases = visitor.TestCases.Where(tc => vsFilteredTestCases.Any(vsTc => vsTc.DisplayName == tc.DisplayName))
.GroupBy(tc => $"{tc.TestMethod.TestClass.Class.Name}.{tc.TestMethod.Method.Name}")
.SelectMany(group => group.Select(testCase => VsDiscoverySink.CreateVsTestCase(source, discoverer, testCase, forceUniqueNames: group.Count() > 1, logger: logger, knownTraitNames: knownTraitNames))
.Where(vsTestCase => vsTestCase != null)).ToList(); // pre-enumerate these as it populates the known trait names collection
Dictionary<ITestCase, TestCase> testCaseMap = null;
List<TestCase> testCases = null;

if (AppDomain == AppDomainSupport.Denied)
{
testCaseMap = visitor.TestCases.Where(tc => vsFilteredTestCases.Any(vsTc => vsTc.DisplayName == tc.DisplayName)).GroupBy(tc => $"{tc.TestMethod.TestClass.Class.Name}.{tc.TestMethod.Method.Name}")
.SelectMany(group => group.Select((testCase, i) => new
{
testCase,
v = VsDiscoverySink.CreateVsTestCase(
source,
discoverer,
testCase,
forceUniqueNames: group.Count() > 1,
logger: logger,
knownTraitNames: knownTraitNames)
})).ToDictionary(x => x.testCase, x => x.v);

}
else
{
testCases = visitor.TestCases.Where(tc => vsFilteredTestCases.Any(vsTc => vsTc.DisplayName == tc.DisplayName)).GroupBy(tc => $"{tc.TestMethod.TestClass.Class.Name}.{tc.TestMethod.Method.Name}")
.SelectMany(group => group.Select(testCase =>
VsDiscoverySink.CreateVsTestCase(
source,
discoverer,
testCase,
forceUniqueNames: group.Count() > 1,
logger: logger,
knownTraitNames: knownTraitNames))
.Where(vsTestCase => vsTestCase != null)).ToList(); // pre-enumerate these as it populates the known trait names collection
}

var runInfo = new AssemblyRunInfo
{
AssemblyFileName = source,
Configuration = LoadConfiguration(source),
TestCases = testCases
TestCases = testCases,
TestCaseMap = testCaseMap
};
result.Add(runInfo);
}
Expand Down Expand Up @@ -377,9 +423,18 @@ void RunTestsInAssembly(IRunContext runContext,
var diagnosticMessageVisitor = new DiagnosticMessageSink(logger, assemblyDisplayName, runInfo.Configuration.DiagnosticMessagesOrDefault);
using (var controller = new XunitFrontController(appDomain, assemblyFileName: assemblyFileName, configFileName: null, shadowCopy: shadowCopy, diagnosticMessageSink: diagnosticMessageVisitor))
{
var xunitTestCases = runInfo.TestCases.Select(tc => new { vs = tc, xunit = Deserialize(logger, controller, tc) })
.Where(tc => tc.xunit != null)
.ToDictionary(tc => tc.xunit, tc => tc.vs);
Dictionary<ITestCase, TestCase> xunitTestCases;
if (AppDomain == AppDomainSupport.Denied)
{
xunitTestCases = runInfo.TestCaseMap;
}
else
{
xunitTestCases = runInfo.TestCases.Select(tc => new { vs = tc, xunit = Deserialize(logger, controller, tc) })
.Where(tc => tc.xunit != null)
.ToDictionary(tc => tc.xunit, tc => tc.vs);
}


var executionOptions = TestFrameworkOptions.ForExecution(runInfo.Configuration);
executionOptions.SetSynchronousMessageReporting(true);
Expand Down Expand Up @@ -452,6 +507,111 @@ IEnumerator IEnumerable.GetEnumerator()
}
}

Dictionary<ITestCase, TestCase> GetXunitTestCaseMap(IEnumerable<TestCase> tests)
{
// -- Get the xunit ITestCase from TestCase
Dictionary<ITestCase, TestCase> testcaselist = new Dictionary<ITestCase, TestCase>();

var dict = new Dictionary<string, Assembly>();
foreach (TestCase test in tests)
{
var asmName = test.Source.Substring(test.Source.LastIndexOf('\\') + 1, test.Source.LastIndexOf('.') - test.Source.LastIndexOf('\\') - 1);
Assembly asm = null;
if (!dict.ContainsKey(asmName))
{
#if PLATFORM_DOTNET
// todo : revisit this
asm = Assembly.Load(new AssemblyName(asmName));
#else
asm = Assembly.LoadFrom(test.Source);
#endif
dict.Add(asmName, asm);
}
else
{
asm = dict[asmName];
}

IAssemblyInfo assemblyInfo = new ReflectionAssemblyInfo(asm);
ITestAssembly testAssembly = new TestAssembly(assemblyInfo);

var lastIndexOfDot = test.FullyQualifiedName.LastIndexOf('.');
var testname = test.FullyQualifiedName.Split(' ')[0].Substring(lastIndexOfDot + 1);
var classname = test.FullyQualifiedName.Substring(0, lastIndexOfDot);

var methodClass = asm.GetType(classname);

ITypeInfo typeInfo = new ReflectionTypeInfo(methodClass);
TestCollection testCollection = new TestCollection(testAssembly, typeInfo, "Test collection for " + classname);
TestClass tc = new TestClass(testCollection, typeInfo);

//IMethodInfo
#if PLATFORM_DOTNET
var methodinfos = methodClass.GetRuntimeMethods().ToList();
#else
var methodinfos = methodClass.GetMethods().ToList();
#endif

XunitTestCase xunitTestCase = null;
var serializedTestArgs = test.GetPropertyValue<string>(SerializedTestCaseArgumentProperty, null);


Object[] testarguments = serializedTestArgs == null ? null : JsonConvert.DeserializeObject<Object[]>(serializedTestArgs, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});

if (testarguments != null)
{
testname = testname.Split('(')[0];
}


MethodInfo methodinfo = null;
for (int i = 0; i < methodinfos.Count(); i++)
{
if (methodinfos[i].Name.Equals(testname))
{
methodinfo = methodinfos[i];
break;
}
}

ReflectionMethodInfo refMethodInfo = new ReflectionMethodInfo(methodinfo);
ITestMethod method = new TestMethod(tc, refMethodInfo);
NullMessageSink sink = new NullMessageSink();

if (testarguments == null && !string.IsNullOrEmpty(test.GetPropertyValue<string>(TheoryAgrumentProperty, null)))
{
#if !PLATFORM_DOTNET
Type type = typeof(XunitTheoryTestCase);
ConstructorInfo ctor = (ConstructorInfo)type.GetConstructors().GetValue(1);
xunitTestCase = (XunitTheoryTestCase)ctor.Invoke(new object[] { sink, 1, method });
#else
xunitTestCase = new XunitTheoryTestCase(sink, Xunit.Sdk.TestMethodDisplay.ClassAndMethod, method);

#endif
}
else
{
#if !PLATFORM_DOTNET
Type type = typeof(XunitTestCase);
ConstructorInfo ctor = (ConstructorInfo)type.GetConstructors().GetValue(1);
xunitTestCase = (XunitTestCase)ctor.Invoke(new object[] { sink, 1, method, testarguments });
#else
xunitTestCase = new XunitTestCase(sink, Xunit.Sdk.TestMethodDisplay.ClassAndMethod, method, testarguments);
#endif
}

xunitTestCase.SourceInformation = new SourceInformation();
xunitTestCase.SourceInformation.FileName = test.CodeFilePath;
xunitTestCase.SourceInformation.LineNumber = test.LineNumber;

testcaselist.Add(xunitTestCase, test);
}
return testcaselist;
}

bool DisableAppDomainRequestedInRunContext(string settingsXml)
{
#if !PLATFORM_DOTNET
Expand Down
4 changes: 4 additions & 0 deletions xunit.runner.visualstudio.testadapter/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.VisualStudio.TestPlatform.ObjectModel" version="0.0.6" targetFramework="net35" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net35" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net35" />
<package id="xunit.core" version="2.2.0-beta3-build3335" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.2.0-beta3-build3335" targetFramework="net45" />
<package id="xunit.extensibility.execution" version="2.2.0-beta3-build3335" targetFramework="net45" />
<package id="xunit.runner.utility" version="2.2.0-beta3-build3335" targetFramework="net35" />
</packages>
Loading