Skip to content
This repository has been archived by the owner on Nov 9, 2018. It is now read-only.

Commit

Permalink
Fix SkipReasonTestCase to allow serialization and deserialization.
Browse files Browse the repository at this point in the history
- With CSproj based test runners test cases are passed across application boundaries which require test cases to be serializable.

microsoft/vstest#291
  • Loading branch information
NTaylorMullen committed Dec 22, 2016
1 parent a457043 commit e179dc9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -39,7 +40,15 @@ public IEnumerable<IXunitTestCase> Discover(

var testCases = innerDiscoverer
.Discover(discoveryOptions, testMethod, factAttribute)
.Select(testCase => new SkipReasonTestCase(isTheory, skipReason, testCase));
.Select(testCase => new SkipReasonTestCase
{
IsTheory = isTheory,
SkipReason = skipReason,
SourceInformation = testCase.SourceInformation,
TestMethod = testCase.TestMethod,
TestMethodArguments = testCase.TestMethodArguments,
UniqueID = testCase.UniqueID,
});

return testCases;
}
Expand Down
131 changes: 82 additions & 49 deletions src/Microsoft.AspNetCore.Testing/xunit/SkipReasonTestCase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,88 +13,93 @@ namespace Microsoft.AspNetCore.Testing.xunit
{
internal class SkipReasonTestCase : LongLivedMarshalByRefObject, IXunitTestCase
{
private readonly bool _isTheory;
private readonly string _skipReason;
private readonly IXunitTestCase _wrappedTestCase;
private string _displayName;
private bool _initialized;
private IMethodInfo _methodInfo;
private ITypeInfo[] _methodGenericTypes;
private Dictionary<string, List<string>> _traits;

public SkipReasonTestCase(bool isTheory, string skipReason, IXunitTestCase wrappedTestCase)
{
_isTheory = isTheory;
_skipReason = wrappedTestCase.SkipReason ?? skipReason;
_wrappedTestCase = wrappedTestCase;
}
public bool IsTheory { get; set; }

public string DisplayName
{
get
{
return _wrappedTestCase.DisplayName;
}
}
public string SkipReason { get; set; }

public IMethodInfo Method
public string UniqueID { get; set; }

public ISourceInformation SourceInformation { get; set; }

public ITestMethod TestMethod { get; set; }

public object[] TestMethodArguments { get; set; }

public string DisplayName
{
get
{
return _wrappedTestCase.Method;
EnsureInitialized();
return _displayName;
}
}

public string SkipReason
{
get
set
{
return _skipReason;
EnsureInitialized();
_displayName = value;
}
}

public ISourceInformation SourceInformation
public IMethodInfo Method
{
get
{
return _wrappedTestCase.SourceInformation;
EnsureInitialized();
return _methodInfo;
}
set
{
_wrappedTestCase.SourceInformation = value;
EnsureInitialized();
_methodInfo = value;
}
}

public ITestMethod TestMethod
protected ITypeInfo[] MethodGenericTypes
{
get
{
return _wrappedTestCase.TestMethod;
EnsureInitialized();
return _methodGenericTypes;
}
}

public object[] TestMethodArguments
public Dictionary<string, List<string>> Traits
{
get
{
return _wrappedTestCase.TestMethodArguments;
}
}
EnsureInitialized();

public Dictionary<string, List<string>> Traits
{
get
return _traits;
}
set
{
return _wrappedTestCase.Traits;
EnsureInitialized();

_traits = value;
}
}

public string UniqueID
public void Deserialize(IXunitSerializationInfo info)
{
get
{
return _wrappedTestCase.UniqueID;
}
UniqueID = info.GetValue<string>(nameof(UniqueID));
SkipReason = info.GetValue<string>(nameof(SkipReason));
IsTheory = info.GetValue<bool>(nameof(IsTheory));
TestMethod = info.GetValue<ITestMethod>(nameof(TestMethod));
TestMethodArguments = info.GetValue<object[]>(nameof(TestMethodArguments));
}

public void Deserialize(IXunitSerializationInfo info)
public void Serialize(IXunitSerializationInfo info)
{
_wrappedTestCase.Deserialize(info);
info.AddValue(nameof(UniqueID), UniqueID);
info.AddValue(nameof(SkipReason), SkipReason);
info.AddValue(nameof(IsTheory), IsTheory);
info.AddValue(nameof(TestMethod), TestMethod);
info.AddValue(nameof(TestMethodArguments), TestMethodArguments);
}

public Task<RunSummary> RunAsync(
Expand All @@ -104,12 +110,12 @@ public Task<RunSummary> RunAsync(
CancellationTokenSource cancellationTokenSource)
{
TestCaseRunner<IXunitTestCase> runner;
if (_isTheory)
if (IsTheory)
{
runner = new XunitTheoryTestCaseRunner(
this,
DisplayName,
_skipReason,
SkipReason,
constructorArguments,
diagnosticMessageSink,
messageBus,
Expand All @@ -121,7 +127,7 @@ public Task<RunSummary> RunAsync(
runner = new XunitTestCaseRunner(
this,
DisplayName,
_skipReason,
SkipReason,
constructorArguments,
TestMethodArguments,
messageBus,
Expand All @@ -132,9 +138,36 @@ public Task<RunSummary> RunAsync(
return runner.RunAsync();
}

public void Serialize(IXunitSerializationInfo info)
private void EnsureInitialized()
{
if (!_initialized)
{
_initialized = true;
Initialize();
}
}

private void Initialize()
{
_wrappedTestCase.Serialize(info);
Traits = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
Method = TestMethod.Method;

if (TestMethodArguments != null)
{
IReflectionMethodInfo reflectionMethod = Method as IReflectionMethodInfo;
if (reflectionMethod != null)
{
TestMethodArguments = reflectionMethod.MethodInfo.ResolveMethodArguments(TestMethodArguments);
}

if (Method.IsGenericMethodDefinition)
{
_methodGenericTypes = Method.ResolveGenericTypes(TestMethodArguments);
Method = Method.MakeGenericMethod(MethodGenericTypes);
}
}

DisplayName = Method.GetDisplayNameWithArguments(TestMethod.Method.Name, TestMethodArguments, MethodGenericTypes);
}
}
}

0 comments on commit e179dc9

Please sign in to comment.