VS2012 &TFS2012单元测试主要问题 [英] VS2012 & TFS2012 Unit Test major issues

查看:30
本文介绍了VS2012 &TFS2012单元测试主要问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 VS2012 和 TFS2012 并为我们的代码编写单元测试.我们想要报告代码覆盖率,并在我们的单元测试中使用 .config 文件进行测试应用设置,以及一些其他的日志设置、MS Enterprise 库设置等.

We are using VS2012 and TFS2012 and write unit tests for our code. We want to report code coverage, and also using .config files in our unit tests for test appsettings, and also some other settings for logging, MS Enterprise library settings etc. etc.

App.config 在新的测试框架中不起作用

MS 的新测试框架应该很棒,但对我来说根本没有那么好.当新框架不再使用配置文件时,我应该如何在配置文件中设置一些基本配置?

New test framework of MS should be great, but to me it is not so great at all. How I'm i suppose to set some basic configuration in config files, when the new framework does not use config files anymore?

我们遇到了混合模式 dll 的问题,并找到了解决方法:添加

We had a problem with mixed mode dlls, and found a fix: adding

<startup useLegacyV2RuntimeActivationPolicy="true"> 

到 app.config.但这对我们的单元测试项目不起作用.因为配置文件不再存在.网上搜了下解决办法

to the app.config. But this did not work for our unit test project. Becuase config files are not there anymore. Searching the internet came up with a solution

'Visual Studio .Net 4.5 测试项目中 .Net 2.0 混合模式程序集的问题'

这意味着在程序文件目录中编辑 Visual Studio 11 本身的文件,我认为这不是一个很好的解决方案......

This means editing a file of Visual Studio 11 itself in the program files directory, not a great solution i think....

那么一些基本的 appsetting 怎么样?我该如何设置?

And how about some basic appsetting? How am I supposed to set this?

不要使用 .testSettings 文件

MS 也不推荐使用旧的 .testsettings 文件,因为这样会使用旧的测试框架.如果我使用 .testsettings 文件,则无法在我的 tfs2012 构建服务上设置代码覆盖率.

Using the old .testsettings file is also not recommended by MS, becuase then the old test framework is used. And if I use the .testsettings file, i cannot setup Code Coverage on my tfs2012 build service.

另一个问题是我们的代码需要一个 dll (system.data.sqlite.dll),但只有在运行时单元测试代码才需要这个 dll.所以不需要参考.我们通过使用 testsettings 文件上的部署选项卡解决了这个问题.但是在新框架中,您不应该使用 testsettings 文件.如果需要文件,则具有 [deploymentitem] 属性.但是 deploymentitem 属性只能用于 [testmethod],不能用于 [testinitialize] 或 [assemblyinitialize] 方法.但是我们的代码需要 [testinitialize] 方法中的 dll.所以没有办法让dll到位.

Another issue is that we have code that need a dll (system.data.sqlite.dll), but only at runtime the unit test code needs this dll. So a reference is not needed. We fixed this by using the Deployment tab on the testsettings file. But in the new framework, you should not use the testsettings file. You have the [deploymentitem] attribute if you need files. But the deploymentitem attribute can only be used on a [testmethod] not on a [testinitialize] or [assemblyinitialize] method. But our code needs the dll in the [testinitialize] method. So there is no way to get the dll in place.

在 [assemblyinitialize](或 testinitialize)方法中用 File.Copy 复制它是行不通的.

Just copy it with File.Copy in the [assemblyinitialize] (or testinitialize) method does not work.

将 dll 作为文件添加到项目中,并将复制到输出目录"设置为始终复制",如使用 .runsettings 文件配置单元测试' 也根本不起作用.

Adding the dll as file to the project, and set the 'copy to output directory' to 'Copy Always' as mentioned in 'Configuring Unit Tests by using a .runsettings File' also does not work at all.

这个(真的不是很好)解决方案是添加 dll 作为引用,然后实例化一个类并且不使用它.这样就需要 dll 否则它不会构建,因此 dll 将自己部署到正确的目录中.

The (really not great) solution for this is to add the dll as a reference, then instantiate a class and do nothting with it. This way the dll is needed otherwise it is not building, and thus the dll will deploy itself to the right directories.

如何解决我的问题???- 我想在我的单元测试中使用配置文件.- 我想在assemblyinitialise"和/或classinitialize"方法中部署一些需要的文件.- 我希望在我的 TFS2012 夜间构建中启用代码覆盖率.

how to solve my problem(s)??? - I want to use config files in my unit test. - I want to deploy some files that are neede in the 'assemblyinitialise' and/or 'classinitialize' methods. - I want Code Coverage on my TFS2012 nightly build enabled.

推荐答案

a) App.config 在新的测试框架中不起作用

a) App.config not working in new test framework

这应该仍然有效.我认为在这种情况下缺少的是这个 .config 文件没有与您的测试 dll 一起复制.能否请您将此设置为部署项并重试?

This should still work. What I think is missing in this case is that this .config file is not being copied with your test dll. Could you please set this as a deployment item and try again?

b) 不要使用 .testSettings 文件

b) Do not use the .testSettings file

  • .testsettings 和代码覆盖率.VS 2012 版本仍然支持使用 .testsettings 文件设置代码覆盖率.您只需选择 mstest 2010 测试运行程序并在构建定义中指定您的 .testsettings 文件

如果您在 .testsettings 文件中除了代码覆盖率设置之外没有任何其他内容,那么您可以轻松迁移到 2012 测试运行器并在下拉项中选择启用代码覆盖率"

If you dont have anything except code coverage settings in the .testsettings file then you can easily migrate to the 2012 test runner and select "enable code coverage" in the drop down items

  • 复制测试初始化​​所需的文件您可以通过 .testsettings 文件执行此操作,也可以执行构建后文件复制任务.这样做非常简单,对其他任何事情都没有影响.使用复制到输出目录=始终复制"确实有效.请使用示例解决方案进行尝试,看看您是否可以缩小为什么这在您的设置中不起作用.

这篇关于VS2012 &amp;TFS2012单元测试主要问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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