在 Visual Studio 2010 中开发和运行 MSTest 单元测试,无需包含 .vsmdi 和 .testsettings
我知道这是以某种方式可能的,因为我们有一个项目包含可通过 VS2010 测试运行程序运行的 MSTest 单元测试。我们甚至可以向现有项目添加新的测试方法或类,运行程序将选择它们并将它们包含在测试运行中。
当我尝试向解决方案添加新的单元测试项目时,问题就出现了。如果我将“测试项目”类型的项目添加到解决方案中,VS2010 将生成运行其他项目中的任何其他测试不需要的测试元数据和设置文件。例如,对于 OSS 项目来说,这是不希望的。如果我只是添加一个普通的“类库”项目,并将单元测试放入其中,测试运行程序会忽略它们,并且我根本无法让它识别它们。
我疯了吗?这是侥幸吗? VS2010 是否可以在没有 .vsmdi 文件或 .testsettings 文件的情况下运行我们的测试?或者我是否缺少使此功能适用于新项目所需的设置或配置?
I know this is somehow possible, as we have a project that contains MSTest unit tests that are runnable via the VS2010 test runner. We can even add new test methods or classes to the existing projects, and the runner will pick them up and include them in the test run.
The problem comes when I try to add a new unit test project to the solution. If I add a project of the type "test project" to the solution, VS2010 will generate the test metadata and settings files that were not needed for running any of the other tests in the other projects. This is undesirable, for example, for an OSS project. If I simply add a normal "class library" project, and put unit tests in it, the test runner ignores them, and I cannot get it to recognize them at all.
Am I crazy? Is this a fluke? Should it even be possible for VS2010 to run the tests we have, without having a .vsmdi file or a .testsettings file? Or am I missing a setting or configuration that's required in order to make this work for new projects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确实可以在 VS 中运行测试,而无需 .vsmdi 和 .testsettings 文件(事实上,你可以在添加测试项目后删除它们)
那么为什么它不能与普通的类库一起使用呢? awnser 位于 .csproj 文件内。
这是一个常规类库:
这是一个测试项目:
最后一个元素
ProjectTypeGuids
告诉 VS 它是一个可以运行 MSTest 测试的项目。据我所知,这些 guid 总是相同的,[至少给定相同版本的 VS],因此您应该能够将该行粘贴到任何 .csproj 文件中,并让 VS 识别其中的测试。测试设置文件可用于指定部署选项(以及许多其他内容),但大多数选项也可以在 mstest.exe 的命令行中指定。
也可以通过向测试方法添加属性来替换 .vsmdi 。测试的
Properties
中可用的大多数(如果不是全部)选项都可以设置为属性以及 vsmdi 文件。我通常更喜欢这些属性,因为它们“更接近”代码。You can indeed run tests within VS without having the .vsmdi and .testsettings files (infact, you can just delete them after adding the test project)
So why doesnt it work with a normal class library? the awnser lies inside the .csproj file.
This is a regular class library:
And this is a test project:
That last element,
ProjectTypeGuids
is what tells VS that its a project that you can run MSTest tests on. these guids are always the same as far as i know, [at least given the same version of VS] so you should be able to paste that line into any .csproj file and have VS recognize the tests inside.The test settings file can be useful to specify options for deployment (and alot of other things) but most of the options can also be specified at the command line to mstest.exe
The .vsmdi can also be replaced by adding attributes to your test methods. most if not all options available in the
Properties
for a test can be set as attributes as well as in the vsmdi file. i generally prefer the attributes as they are 'closer' to the code.