代码合同-Visual Studio Team Service脚本化生成服务器单元测试失败 [英] Code Contracts - Visual Studio Team Service Scripted Build Server Unit Tests Fail

查看:100
本文介绍了代码合同-Visual Studio Team Service脚本化生成服务器单元测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我的解决方案中添加了代码合同.进行一些修改后,我们的构建没有任何问题,但是由于代码合同,我们的单元测试失败了.

I recently added Code Contracts to my solution. After some modifications our build runs without any problems but our unit tests fail because of Code Contracts.

环境:

  • 源代码管理和构建服务器托管在 Visual Studio Team Service (VSTS)脚本生成(以前是VSO )
  • 在配置DebugRelease 上启用了
  • VS 2013高级代码(现在为VS 2015 Enterprise )合同
  • 关闭代码协定,并为自定义配置BuildServer设置DoNotBuild标志
  • Visual Studio团队服务构建定义:
  • Source control and build server are hosted on Visual Studio Team Service (VSTS) Scripted Build (formerly VSO)
  • VS 2013 Premium Code (now VS 2015 Enterprise) contracts enabled on configurations Debug and Release
  • Code contract turned off with DoNotBuild flag set for custom configurations BuildServer
  • Visual Studio Team Services build definition:
  1. 已安装Visual Studio
  2. 已将标志设置为/p:CodeContractsEnableRuntimeChecking=false;CodeContractsReferenceAssembly=false

来自构建服务器的错误文本示例:

测试方法 Web.Tests.AccountControllerTests.CreateAccount_Pass_NoPasswordSend_Test 引发异常:System.Diagnostics.Contracts.ContractException:一个 程序集(可能是"Cms.Web")必须使用以下代码重写 合同二进制重写器(CCRewrite),因为它正在调用 Contract.Requires,且CONTRACTS_FULL符号为 定义.删除任何CONTRACTS_FULL符号的明确定义 从您的项目中进行重建.

Test method Web.Tests.AccountControllerTests.CreateAccount_Pass_NoPasswordSend_Test threw exception: System.Diagnostics.Contracts.ContractException: An assembly (probably "Cms.Web") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires and the CONTRACTS_FULL symbol is defined. Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild.

我已经检查了构建服务器的诊断输出,并且在任何地方都找不到符号CONTRACTS_FULL.

I have examined the diagnostic output from the build server and there is no symbol CONTRACTS_FULL that can be found anywhere.

我不想在构建服务器上启用代码协定,主要是因为我曾尝试过,但没有成功,并且由于时间限制,我放弃了(在进行了更多搜索之后,我得出结论认为这是不可能的)因为Microsoft不允许在构建服务器上安装自定义扩展).我只是想忽略构建服务器上的代码协定,但是执行单元测试似乎没有做到这一点.

I do not want to enable code contracts on the build server mostly because I had tried that before with no success and due to time constraints I gave up (after more searching I came to the conclusion that this is not possible as Microsoft does not allow for custom extensions to be installed on the build server). I simply want to ignore code contracts on the build server but the Unit Test execution does not seem to do this.

有人对我需要在哪里确保单元测试忽略代码约定有任何想法吗?似乎应该这样吧?也许我可以在构建定义中设置另一个标志,以供单元测试使用?

Any one have any ideas on where I need to look to ensure code contracts are ignored by the unit tests? Seems like this should be possible right? Maybe there is another flag I could set in the build definition that will be picked up by the unit tests?

修改

请注意,作为Microsoft服务的一部分,构建服务器在云中已 HOSTED 在云中,作为Visual Studio Team Services(以前称为Visual Studio Online)的一部分.这意味着它不是我们的构建服务器,我们无法控制构建服务器上安装的内容.即使如此,我们仍要使用代码合同.下面接受的答案是允许这种情况的一种方式.

Please note the build server is HOSTED in the cloud as a Microsoft service as a part of Visual Studio Team Services (formerly Visual Studio Online). This means it is not our build server, we have no control over what is installed on the build server. Even so we want to use Code Contracts. The accepted answer below is a way to allow for this.

推荐答案

请,请阅读《代码合同》手册.它告诉您所有您需要知道的内容.您不需要在方法上定义任何ConditionalAttribute.如果您知道这样做的要求,则可以使用Contract.Requires<TException>(bool condition).

Please, please read the Code Contracts manual. It tells you everything you need to know. You don't need any ConditionalAttribute defined on your methods. You can use Contract.Requires<TException>(bool condition) if you know what the requirements for doing so are.

@Mixxiphoid在回答中说,由于他们使用的是Requires的通用形式,因此必须将代码合同安装在构建服务器上 Peform Runtime Contract Checking 除了调试配置之外,还需要为发布配置启用.

As @Mixxiphoid said in their answer, because they were using the generic form of Requires, Code Contracts is required to be installed on the build server and Peform Runtime Contract Checking needs to be enabled for the Release configuration in addition to the Debug configuration.

如果您使用的是Code Contracts,那么您将始终 需要在构建Debug版本的构建服务器上安装Code Contracts.您的团队也是如此:所有需要编译代码的开发人员都必须安装用于调试版本的代码合同.

If you are using Code Contracts, then you will always need Code Contracts installed on the build servers that build Debug releases. The same goes for your team: all developers who need to compile the code will be required to have Code Contracts installed for Debug builds.

全部内容均在《代码合同》手册的第5节:使用指南中. 第20页上有一个有用的图表,其中对各种使用场景及其要求进行了很好的总结.

It's all in Section 5: Usage Guidelines in the Code Contracts manual. There's a helpful diagram on page 20 that gives a great summary of the various usage scenarios and their requirements.

首先,为代码合同编写此方法的开发人员 没有 忘记将ConditionalAttribute应用于该方法,这是公认的答案.离得很远. 根据设计 .该方法没有用该​​属性修饰的事实.

First off, the developer that wrote this method for Code Contracts did not forget to apply the ConditionalAttribute to the method, as the accepted answer states. Far from it. The very fact that this method is not decorated with that attribute is by design.

如果您阅读第5部分:使用指南,则会看到,如果您使用这种形式的Requires方法,则在使用二进制重写器时需要使用ccrewrite建立您的程序集.这意味着将要构建装配的任何计算机都必须具有ccrewrite可用.

If you read Section 5: Usage Guidelines, you see that if you use this form of the Requires method, then you are required to use the binary rewriter—ccrewrite—when building your assembly. This implies that any machine which will build your assembly must have ccrewrite available.

那么,您可能无法控制的构建服务器呢?我很高兴你问.

So, what about build servers that you may not control? I'm glad you asked.

关于TFS托管的构建服务器:的确,您不能简单地在构建服务器上安装代码协定.那你有什么选择呢?

With respect to TFS hosted build servers: It's true that you cannot simply install Code Contracts on the build servers. So what are your options?

  1. 您可以将自己限制为仅使用旧式"形式的代码合同, if-then-throw 块后跟Contract.EndContractBlock()或将自己限制为仅使用Contract.Requires(bool condition)(非通用形式),或两者的混合形式.
    • 无论哪种情况,您都可能希望为TFS托管的构建服务器使用单独的Debug构建配置.此构建配置将为此构建配置禁用执行运行时合同检查.
  1. You could limit yourself to using only the "legacy" form of code contracts, if-then-throw blocks followed by Contract.EndContractBlock() or limit yourself to only using Contract.Requires(bool condition) (the non-generic form), or a mixture of the two.
    • In either case, you'll probably want to have a separate Debug build configuration for the TFS-hosted build server. This build configuration would have Perform Runtime Contract Checking disabled for that build configuration.

上面链接到的博客文章包含有关修改TFS托管构建控制器托管构建的说明.

The blog post linked to above has instructions for modifying a TFS Hosted Build Controller hosted build.

更新:将很快成为代码合同的一小部分 对于那些使用托管构建服务(例如Visual Studio Online,AppVeyor等)的用户,新的社区驱动的Code Contracts GitHub存储库最近宣布了 v1.10.xxxxx.RC1 .在发行说明中,他们提到了 NuGet版本的代码合同.享受.

UPDATE: THERE WILL SOON BE A NUGET VERSION OF CODE CONTRACTS For those of you using hosted build services, such as Visual Studio Online, AppVeyor, etc., the new community-driven Code Contracts GitHub repo recently announced v1.10.xxxxx.RC1. In the release notes, they mention a NuGet version of code contracts. Enjoy.

这篇关于代码合同-Visual Studio Team Service脚本化生成服务器单元测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆