为nUnit测试设置Visual Studio项目的最佳实践

时间:2022-10-16 00:14:24

How do people set up their projects for Visual Studio and how do you reference the testable application ?

人们如何为Visual Studio设置项目,以及如何引用可测试的应用程序?

Right now I've added a separate project creating a .dll to my solution which contains all the test cases and references nunit.framework , and it also references the main .exe file right from the Debug/ folder where VS generates the output.

现在我添加了一个单独的项目,为我的解决方案创建一个.dll,其中包含所有测试用例和引用nunit.framework,它还引用了VS.生成输出的Debug /文件夹中的主.exe文件。

But I've no idea if that's a good idea - or what the best practices are, anyone want to share their practices ?

但我不知道这是不是一个好主意 - 或者最佳实践是什么,谁都想分享他们的做法?

5 个解决方案

#1


That sounds pretty good to me - you can distribute or deploy your application without the test assembly and NUnit, but still test everything. Pretty much standard practice.

这对我来说听起来不错 - 您可以在没有测试程序集和NUnit的情况下分发或部署应用程序,但仍然可以测试所有内容。几乎是标准练习。

#2


I have used a separate folder for all of my tests when using them inside a project. Then you can remove the folder easily when doing an actual build if you choose to.

在项目中使用它时,我为我的所有测试使用了一个单独的文件夹。然后,如果您选择执行实际构建,则可以轻松删除该文件夹。

I have also done what you are saying with a separate project. I think that is fine.

我也用一个单独的项目做了你说的话。我觉得这很好。

#3


Typically, I have a solution containing a number of projects without any unit tests in that main solution - this is the solution that gets built in Release mode for a true build.

通常,我有一个包含许多项目的解决方案,在该主要解决方案中没有任何单元测试 - 这是在发布模式下构建的真正构建的解决方案。

For a specific project, I have a separate solution which contains the project I want to unit test (and any dependent ones) and a MyProjectName.UnitTests project which house all the unit tests for that project. These unit test projects are then configured within a Continuous Integration machine to get build in debug mode and then the tests run.

对于特定项目,我有一个单独的解决方案,其中包含我想要进行单元测试的项目(以及任何从属项目)和一个MyProjectName.UnitTests项目,该项目包含该项目的所有单元测试。然后在Continuous Integration机器中配置这些单元测试项目,以便在调试模式下构建,然后运行测试。

Works well for me.

对我来说效果很好。

#4


Assuming that the test project is in the same solution as the project(s) under test, a small improvement could be to add a reference to the project(s) under test rather than to the binary in the Debug folder.

假设测试项目与正在测试的项目处于同一解决方案中,一个小的改进可能是添加对测试项目的引用,而不是Debug文件夹中的二进制文件。

In addition to what has been mentioned here, I often also use the InternalsVisibleTo assembly attribute to make the internal classes of the assembly I am testing visible to the test assembly i.e. so that they can also be tested directly.

除了这里提到的内容之外,我还经常使用InternalsVisibleTo程序集属性来使我正在测试的程序集的内部类对测试程序集可见,即使它们也可以直接测试。

Depending on the isolation framework of your choice, you might also need to make the internals of your assembly under test visible to isolation framework components to be able to mock/stub certain internal behaviours.

根据您选择的隔离框架,您可能还需要使受测试的程序集内部对隔离框架组件可见,以便能够模拟/存根某些内部行为。

#5


I don't think there's anything wrong delivering dlls containing code that proves that even the dll in a production envorinment is still working within established parameters :). In case of weird errors you can still run the unit tests with the release dll and see if something broke in a weird way. Because of this and because I don't like having twice the number of projects in a solution I prefer to add a Tests folder in each project where all the unit test code goes. Simple and effective.

我认为提供包含代码的dll没有任何错误,这证明即使生产环境中的dll仍然在已建立的参数内工作:)。如果出现奇怪的错误,您仍然可以使用release dll运行单元测试,看看是否有一些奇怪的方式。正因为如此,并且因为我不喜欢在解决方案中拥有两倍数量的项目,所以我更喜欢在每个项目中添加一个Tests文件夹,其中包含所有单元测试代码。简单有效。

Regards,

Sebastiaan

#1


That sounds pretty good to me - you can distribute or deploy your application without the test assembly and NUnit, but still test everything. Pretty much standard practice.

这对我来说听起来不错 - 您可以在没有测试程序集和NUnit的情况下分发或部署应用程序,但仍然可以测试所有内容。几乎是标准练习。

#2


I have used a separate folder for all of my tests when using them inside a project. Then you can remove the folder easily when doing an actual build if you choose to.

在项目中使用它时,我为我的所有测试使用了一个单独的文件夹。然后,如果您选择执行实际构建,则可以轻松删除该文件夹。

I have also done what you are saying with a separate project. I think that is fine.

我也用一个单独的项目做了你说的话。我觉得这很好。

#3


Typically, I have a solution containing a number of projects without any unit tests in that main solution - this is the solution that gets built in Release mode for a true build.

通常,我有一个包含许多项目的解决方案,在该主要解决方案中没有任何单元测试 - 这是在发布模式下构建的真正构建的解决方案。

For a specific project, I have a separate solution which contains the project I want to unit test (and any dependent ones) and a MyProjectName.UnitTests project which house all the unit tests for that project. These unit test projects are then configured within a Continuous Integration machine to get build in debug mode and then the tests run.

对于特定项目,我有一个单独的解决方案,其中包含我想要进行单元测试的项目(以及任何从属项目)和一个MyProjectName.UnitTests项目,该项目包含该项目的所有单元测试。然后在Continuous Integration机器中配置这些单元测试项目,以便在调试模式下构建,然后运行测试。

Works well for me.

对我来说效果很好。

#4


Assuming that the test project is in the same solution as the project(s) under test, a small improvement could be to add a reference to the project(s) under test rather than to the binary in the Debug folder.

假设测试项目与正在测试的项目处于同一解决方案中,一个小的改进可能是添加对测试项目的引用,而不是Debug文件夹中的二进制文件。

In addition to what has been mentioned here, I often also use the InternalsVisibleTo assembly attribute to make the internal classes of the assembly I am testing visible to the test assembly i.e. so that they can also be tested directly.

除了这里提到的内容之外,我还经常使用InternalsVisibleTo程序集属性来使我正在测试的程序集的内部类对测试程序集可见,即使它们也可以直接测试。

Depending on the isolation framework of your choice, you might also need to make the internals of your assembly under test visible to isolation framework components to be able to mock/stub certain internal behaviours.

根据您选择的隔离框架,您可能还需要使受测试的程序集内部对隔离框架组件可见,以便能够模拟/存根某些内部行为。

#5


I don't think there's anything wrong delivering dlls containing code that proves that even the dll in a production envorinment is still working within established parameters :). In case of weird errors you can still run the unit tests with the release dll and see if something broke in a weird way. Because of this and because I don't like having twice the number of projects in a solution I prefer to add a Tests folder in each project where all the unit test code goes. Simple and effective.

我认为提供包含代码的dll没有任何错误,这证明即使生产环境中的dll仍然在已建立的参数内工作:)。如果出现奇怪的错误,您仍然可以使用release dll运行单元测试,看看是否有一些奇怪的方式。正因为如此,并且因为我不喜欢在解决方案中拥有两倍数量的项目,所以我更喜欢在每个项目中添加一个Tests文件夹,其中包含所有单元测试代码。简单有效。

Regards,

Sebastiaan