add ContextResolver for MSTest V2
This commit is contained in:
Родитель
814900a42f
Коммит
64edf930c8
|
@ -743,6 +743,9 @@
|
|||
<Compile Include="..\Telerik.JustMock\Core\Context\MSTestMockingContextResolver.cs">
|
||||
<Link>Core\Context\MSTestMockingContextResolver.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Telerik.JustMock\Core\Context\MSTestV2MockingContextResolver.cs">
|
||||
<Link>Core\Context\MSTestV2MockingContextResolver.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Telerik.JustMock\Core\Context\NUnitMockingContextResolver.cs">
|
||||
<Link>Core\Context\NUnitMockingContextResolver.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
JustMock Lite
|
||||
Copyright © 2010-2015 Telerik AD
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Telerik.JustMock.Core.Context
|
||||
{
|
||||
internal class MSTestV2MockingContextResolver : HierarchicalTestFrameworkContextResolver
|
||||
{
|
||||
private const string Mstestv2AssemblyName = "Microsoft.VisualStudio.TestPlatform.TestFramework";
|
||||
private const string Mstestv2AssertionFailedName = "Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException, " + Mstestv2AssemblyName;
|
||||
|
||||
public MSTestV2MockingContextResolver()
|
||||
: base(Mstestv2AssertionFailedName)
|
||||
{
|
||||
this.SetupStandardHierarchicalTestStructure(
|
||||
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute, " + Mstestv2AssemblyName },
|
||||
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute, " + Mstestv2AssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute, " + Mstestv2AssemblyName },
|
||||
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute, " + Mstestv2AssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute, " + Mstestv2AssemblyName },
|
||||
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute, " + Mstestv2AssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute, " + Mstestv2AssemblyName },
|
||||
FixtureConstuctorSemantics.InstanceConstructorCalledOncePerFixture);
|
||||
}
|
||||
|
||||
public static bool IsAvailable
|
||||
{
|
||||
get { return FindType(Mstestv2AssertionFailedName, false) != null; }
|
||||
}
|
||||
|
||||
private const int DefaultGcFrequency = 50;
|
||||
private int createdRepoCount;
|
||||
private int lastGcCount;
|
||||
private int gcFrequency = GetGcFrequency();
|
||||
private bool synchronousGc = GetSynchronousGc();
|
||||
|
||||
private static int GetGcFrequency()
|
||||
{
|
||||
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "MSTestGcFrequency");
|
||||
int value;
|
||||
if (!String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value >= 1)
|
||||
return value;
|
||||
|
||||
return DefaultGcFrequency;
|
||||
}
|
||||
|
||||
private static bool GetSynchronousGc()
|
||||
{
|
||||
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "SynchronousGc");
|
||||
int value;
|
||||
return !String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value == 1;
|
||||
}
|
||||
|
||||
#if !SILVERLIGHT
|
||||
protected override void OnMocksRepositoryCreated(MocksRepository repo)
|
||||
{
|
||||
// MSTest runs every test in a different thread. We'd like to collect Thread objects often so that their handle is released.
|
||||
// At every N created repos (we assume that each test creates a single repo, so the number of repos created is close to
|
||||
// the number of threads created) do a garbage collection, but only if it hasn't been already done in this interval.
|
||||
|
||||
createdRepoCount++;
|
||||
|
||||
if (createdRepoCount % gcFrequency == 0)
|
||||
{
|
||||
var gen2Collections = GC.CollectionCount(GC.MaxGeneration);
|
||||
if (gen2Collections == lastGcCount)
|
||||
{
|
||||
if (synchronousGc)
|
||||
{
|
||||
GC.Collect();
|
||||
}
|
||||
else
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(_ => GC.Collect());
|
||||
}
|
||||
|
||||
gen2Collections++;
|
||||
}
|
||||
lastGcCount = gen2Collections;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -134,6 +134,8 @@ namespace Telerik.JustMock.Core.Context
|
|||
#else
|
||||
if (MSTestMockingContextResolver.IsAvailable)
|
||||
registeredContextResolvers.Add(new MSTestMockingContextResolver());
|
||||
if (MSTestV2MockingContextResolver.IsAvailable)
|
||||
registeredContextResolvers.Add(new MSTestV2MockingContextResolver());
|
||||
if (XUnit1xMockingContextResolver.IsAvailable)
|
||||
registeredContextResolvers.Add(new XUnit1xMockingContextResolver());
|
||||
if (XUnit2xMockingContextResolver.IsAvailable)
|
||||
|
|
|
@ -262,6 +262,7 @@
|
|||
<Compile Include="Core\Behaviors\PropertyStubsBehavior.cs" />
|
||||
<Compile Include="Core\Behaviors\RaiseEventBehavior.cs" />
|
||||
<Compile Include="Core\Behaviors\StaticConstructorMockBehavior.cs" />
|
||||
<Compile Include="Core\Context\MSTestV2MockingContextResolver.cs" />
|
||||
<Compile Include="Core\StrictMockException.cs" />
|
||||
<Compile Include="Core\ByRefComparer.cs" />
|
||||
<Compile Include="Core\ClrProfilerSetupHelper.cs" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче