问题描述
我当前正在为项目中的每个程序集创建一个成对的单元测试程序集,它们都在同一文件夹中.
I'm currently creating a paired unit test assembly for every assembly in my project, both are in the same folder.
- MyProject/MyProject.csproj
- MyProject.Test/MyProject.Test.csproj
看开放源代码项目,我看到一些较小的项目将所有测试放在一个程序集中,而其他程序则像我一样拆分.我正在处理一个大型解决方案,因此将所有测试放在一个项目中会非常疯狂.
Looking at open source projects, I've seen some smaller project put all tests in one assembly, and other split it out like mine. I'm dealing with a large solution so it would be pretty crazy to put all tests in one project.
我目前有msbuild逻辑可以在所有* .Test.csproj文件上运行测试.如果我将所有测试都放在另一个文件夹中,则不需要这样做.
I currently have msbuild logic to run tests on all *.Test.csproj files. If I had all my tests in a different folder I wouldn't need to do this.
只是想知道是否有任何好的论点可以以某种方式做事.
Just wondering if there are any good arguments to do things a certain way.
谢谢
推荐答案
我以相同的方式进行操作,但 我更改了每个测试项目的默认名称空间以匹配生产项目的名称空间.因此,对类X.Y.Foo
的测试在X.Y.FooTest
中而不是在X.Y.Test.FooTest
中进行-这意味着您需要的using指令更少,并且通常使事情变得更简单.
I do it the same way but I change the default namespace for each test project to match the namespace of the production project. So the tests for class X.Y.Foo
are in X.Y.FooTest
rather than X.Y.Test.FooTest
- it means you need fewer using directives, and generally makes things simpler.
我想将两者保留在单独的项目中的主要原因是避免将测试包含在生产库中,或者避免交付未经测试的库.使用单独的项目结构,您可以针对所构建的任何内容运行单元测试.它还使浏览生产类变得更加容易,而无需查看两倍的文件(当获得库的感觉"时).
My main reason for wanting to keep the two in separate projects is to avoid either including the tests in the production library or having to ship an untested library. With the separate project structure, you can run unit tests against anything you build. It also makes it easier to look through just the production classes without having twice as many files to look at (when getting the "feel" of a library).
最后,不要忘记,如果您在测试时需要访问internal
成员,则总是有 [InternalsVisibleTo]
.
Finally, don't forget that if you need to access internal
members when testing, there's always [InternalsVisibleTo]
.
这篇关于在Visual Studio中组织单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!