Microsoft Fakes & TFS 2012 Visual Studio Test RunneR语言 单元测试偶尔失败



环境

  • Visual Studio 2012高级更新3
  • Team Foundation Server 2012更新3
  • 编辑:.NET Framework 4
  • DefaultTemplate(DefaultTemplate.11.xaml)
    • Visual Studio测试运行程序
      • 目标平台:X86

问题

我有两个单元测试组件:

  • 扩展测试
  • 用户测试

两者都使用Microsoft Fakes Framework(使用StubShims)。

在本地运行单元测试工作良好(在4台不同的机器上测试,甚至在构建服务器上安装的Visual Studio上测试),但是,如果我们使用构建代理进行构建,则某些单元测试会失败,并出现异常,例如:

Unable to create instance of class UserTests.ClientUserTest. Error: System.TypeLoadException: Could not load type 'WorldDirect.CCM.Shared.Backend.SmartClassic.Fakes.StubClient' from assembly 'WorldDirect.Smart.Backend.Fakes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

或垫片相同:

Test method ExtensionTests.ExtensionTests.UpdateExtensionValidate_NoGrnp_ChecksIpPbxDependencies threw exception: System.TypeLoadExceptio: Could not load type                'WorldDirect.CCM.Shared.Backend.SmartClassic.Fakes.ShimIpPbxRemoteDestination' from assembly 'WorldDirect.Smart.Backend.Fakes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

有趣的是,它们偶尔会失败,我试着做了一些统计:

 #     | Outcome          | Comment  
 19    |  64/64 passed    |  only 'UserTests'-UnitTests run, 'ExtensionTests' inactive  
 20    |  37/64 passed    |  same source code as #19  
 21    |  64/64 passed    |  same source code as #19  
 22    |  64/66 passed    |  all 'UserTests' run as well as 2 'ExtensionTests', others still inactive; the 2 ExtensionTests failed with ShimIpPbxRemoteDestination-TypeLoadException  
 23    |  38/65 passed    |  same source code as #22, notice NOTHING has changed, however 1 unit test was not even run in 'ExtensionTests'; however the second succeeds; all unit tests in 'UserTests' using MS Fakes fail with a StubClient-TypeLoadException   
 24    |  38/65 passed    |  same source code as #22  
new day - no more luck :-(
  1     |  37/64 passed    | uncommented the 2 'ExtensionTests', so same code as #19
  2     |  37/64 passed    | so same code as #1
  3     |  64/64 passed    | so same code as #1; suddenly they all work again  

请注意,除了上述更改之外,没有人更改源代码
所有构建都是使用详细的日志级别进行的;垫片诊断="true"
.fakes文件中只包含明确需要的垫片/存根(其他情况下MSBUILD使用退出代码1失败)。

这一切对我来说似乎很神奇,可能有人已经经历过同样的问题,或者有人有暗示。

提前感谢

我想我知道问题出在哪里,TypeNotFoundException是正确的,至少没有生成相应的类型(如Reflector所示),这让我思考。更确切地说,在[buildName]\Binaries下,每个程序集只生成一个生成的伪文件,而且由于我的两个程序集都伪造了同一个程序集(backend.dll),这很可能导致了竞争条件,一个比另一个晚生成的程序集生成了最终的垫片/存根;不过,这并不能说明所有的问题。

无论如何,这不会出现在常规VS构建中的原因是,程序集是在各自的[assembly]\FakeAssemblies目录中生成的,不会影响任何其他单元测试(应该是这样)。

因此,我绕过这个问题的想法是将两个测试程序集使用的所有垫片/存根放入两个.fakes配置文件中;反射器显示,这次确实生成了所有需要的垫片/短截线,但的构建失败了

Exception Message: MSBuild error 1 has ended this build. 

任何(详细)日志中都没有更多信息。这个问题是由于没有使用在.fakes文件中指定的生成的shim/stub对象引起的(当然,这也是构建服务器刚刚显示的问题,否则会太无聊;-))。

因此,我提出的最终解决方案是将所有使用垫片/存根的代码放入同一个程序集中,其中只有1.fakes文件配置假程序集

现在它工作得很好:-),但我认为MS开发团队在这里还有一些事情要做,尽管还没有用TFS2013进行测试;无论如何都要提交一个错误(正如我已经发现的2个)

希望这能帮助到有同样问题的人。

最新更新