【发布时间】:2015-07-29 22:34:54
【问题描述】:
我在使用 VS2013 后迁移到 VS2015 后收到不一致的单元测试结果。在 VS2013 上,单元测试始终通过。在 VS2015 中,如果一次运行所有单元测试或者它是自动 Jenkins 构建,则单元测试会失败。如果单元测试是单独运行的,或者所有失败的测试都同时运行,那么它们就通过了。
这可能是 EF6 问题。运行所有测试时收到的错误消息是...
Test Name: GetQuoteService_NoExceptions
Test FullName: MEA.SmartApp.Business.Factories.APV.UnitTests.QuoteFactoryTest.GetQuoteService_NoExceptions
Test Source: C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\Unit Tests\MEA.SmartApp.Business.Factories.APV.UnitTests\QuoteFactoryTest.cs : line 15
Test Outcome: Failed
Test Duration: 0:00:00.00737
结果堆栈跟踪:
at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName)
at System.Data.Entity.Internal.AppConfig.<.ctor>b__2(ProviderElement e)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Internal.AppConfig.<.ctor>b__1()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Internal.AppConfig.get_DbProviderServices()
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.RegisterDbProviderServices()
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetServiceAsServices(IDbDependencyResolver resolver, Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServices(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass6.<GetServices>b__5(IDbDependencyResolver r)
at System.Linq.Enumerable.<SelectManyIterator>d__1`2.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__1`1.MoveNext()
at System.Linq.Enumerable.<OfTypeIterator>d__1`1.MoveNext()
at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
at System.Data.Entity.Infrastructure.DependencyResolution.InternalConfiguration.Lock()
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.<.ctor>b__1()
at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Lazy`1.get_Value()
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.GetConfiguration()
at System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
at MEA.SmartApp.Contexts.APV.AVMDB..ctor() in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\DataAccessLayer\MEA.SmartApp.Contexts.APV\AVMDB.Context.cs:line 19
at MEA.SmartApp.Business.Factories.APV.QuoteFactory.GetAvmBusinessService() in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\BusinessLogicLayer\MEA.SmartApp.Business.Factories.APV\QuoteFactory.cs:line 65
at MEA.SmartApp.Business.Factories.APV.QuoteFactory.GetQuoteService(APVTransactionCode transactionCode) in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\BusinessLogicLayer\MEA.SmartApp.Business.Factories.APV\QuoteFactory.cs:line 46
at MEA.SmartApp.Business.Factories.APV.UnitTests.QuoteFactoryTest.GetQuoteService_NoExceptions() in C:\MetLife_Sourcecode\Common\DEV2\MEA.Smartapp.Common\Unit Tests\MEA.SmartApp.Business.Factories.APV.UnitTests\QuoteFactoryTest.cs:line 18
Result Message:
Test method MEA.SmartApp.Business.Factories.APV.UnitTests.QuoteFactoryTest.GetQuoteService_NoExceptions threw exception:
**System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded**. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
*我意识到这不是一个真正的单元测试,因为它正在尝试连接到数据库,所以如果您对如何解决这个问题有任何建议,请告诉我。
【问题讨论】:
-
这可能是基于并行测试执行和共享状态的问题。我不知道如何配置 MSTest 或您使用的任何测试运行器以不允许并行测试执行。
-
您是否尝试过这里的任何解决方案:social.msdn.microsoft.com/Forums/en-US/…?
-
@KeithPayne 这是我的第一个想法,在共享状态下并行测试执行。我不知道每次测试执行的顺序是否相同。如果我运行所有单元测试,每次失败的测试都是相同的。如果我在第二次运行时运行所有失败的测试,所有失败的测试都会通过。我如何证明并行测试执行是罪魁祸首? VS2015 的执行方式是否有区别:运行所有单元测试 v. 除了运行所有单元测试之外的任何东西
-
@JohnKoerner 我确实尝试了该论坛的建议,但单元测试仍然失败。我应该注意到,当应用程序运行时,一切都按预期工作。
-
在 VS2015 Update 2 中,测试资源管理器上有一个按钮可以并行运行测试。
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\14.0_Config\FeatureFlags\TestingTools\UnitTesting\Pex处还有一个注册表项,您可以将其设置为 1 以默认开启并行执行。
标签: c# entity-framework unit-testing mstest visual-studio-2015