简体   繁体   English

运行单元测试后避免完整的解决方案构建

[英]Avoid complete solution building after run Unit Tests

I'm working with Unit Test using VS2013 Professional. 我正在使用VS2013 Professional进行单元测试。 In particular I'm using the NUnit framework (NUnit TestAdapter for VS2013). 特别是我正在使用NUnit框架(NUnit TestAdapter for VS2013)。 My problem is that when I run my tests then VS starts building all the projects inside the solution. 我的问题是,当我运行测试时,VS开始构建解决方案中的所有项目。 Currently the Unit Test project does not reference any solution project. 目前,单元测试项目未引用任何解决方案项目。

If I simply code a single test method like: 如果我只是编写一个单一的测试方法,如:

[Test]
public void SimpleTestMethod(){
    Assert.That("a", Is.EqualTo("a"));
}

and the Unit Test project is in a Solution with N project, when I run my test then VS will build all N-1 project... In my case this behavior is boring because it takes too much time (the solution contains many projects) and some projects contain errors. 单元测试项目在N项目的解决方案中,当我运行我的测试时,VS将构建所有N-1项目......在我的情况下,这种行为很无聊,因为它需要太多时间(解决方案包含许多项目)有些项目包含错误。

Is there a way to run my SimpleTestMethod() without complete solution building? 有没有办法在没有完整的解决方案构建的情况下运行我的SimpleTestMethod()

Break your test project to multiple projects that only reference a subset of the solution's projects. 将测试项目分解为仅引用解决方案项目子集的多个项目。

This is also good test housekeeping - have a separate unit test project for each solution project instead of one huge project with dependencies to anything else. 这也是很好的测试管理 - 为每个解决方案项目都有一个单独的单元测试项目,而不是一个与其他任何东西有依赖关系的大型项目。 There are several advantages to this: 这有几个好处:

  • Tests run faster 测试运行得更快
  • It's a lot easier to isolate test cases, especially configuration settings 隔离测试用例,尤其是配置设置要容易得多
  • You can version projects and their test cases together 您可以将项目及其测试用例一起版本化

A good naming practice is to name your test projects the same as their target projects with a .Tests suffix. 一个好的命名实践是将测试项目命名为与目标项目相同的.Tests后缀。 You can also create a solution folder (not a real folder) called "Tests" and move the test projects in it. 您还可以创建名为“测试”的解决方案文件夹(不是真实文件夹),并在其中移动测试项目。

As for the why: Test runners use the Unit Test assembly and its dependencies to run their tests. 至于原因:测试运行器使用单元测试程序集及其依赖项来运行测试。 If any of the assembly's dependencies change, the assembly and the dependencies have to be rebuilt. 如果任何程序集的依赖项发生更改,则必须重建程序集和依赖项。 Visual Studio doesn't know what the external tool will call so it has to build all changed assemblies and their dependents. Visual Studio不知道外部工具将调用什么,因此它必须构建所有已更改的程序集及其依赖项。

If the build fails, there are no valid assemblies for the test runner to use so VS has to rebuild the entire solution before the runner can work. 如果构建失败,则没有有效的程序集供测试运行器使用,因此VS必须在运行器运行之前重建整个解决方案。 In this case, the obvious solution is to fix the error. 在这种情况下,显而易见的解决方案是修复错误。

There are some stopgap options you can use until you can fix the error: 您可以使用一些权宜之计选项,直到您可以修复错误:

  • Temporarily remove the broken project from the build configuration 暂时从构建配置中删除损坏的项目
  • Split the solution so that you have a solution that can be built and tested 拆分解决方案,以便您拥有可以构建和测试的解决方案

I struggled with this for a very long time as well. 我也在很长一段时间内努力解决这个问题。 I actually hated the automatic build process, even when everything was successful. 我实际上讨厌自动构建过程,即使一切都成功了。

I started to run tests through the command line. 我开始通过命令行运行测试。 No build process is necessary. 不需要构建过程。 You can write your own .bat files and keep logs of test results. 您可以编写自己的.bat文件并保留测试结果的日志。 There are plenty of command line parameters that can be added to customize for what you are looking for. 可以添加大量命令行参数来自定义您要查找的内容。

https://msdn.microsoft.com/en-us/library/jj155796.aspx https://msdn.microsoft.com/en-us/library/jj155796.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM