单元测试应该在.Net解决方案中的自己的项目中

时间:2021-01-27 15:43:26

Should I start a new project for my unit tests? That would mean that I would get two executables correct? Then I am worried about the namespace organization. Would I be able to put the unit test in the same namespaces as the classes that they are testing although they are part of a different project?

我应该为单元测试启动一个新项目吗?这意味着我会得到两个可执行文件正确吗?然后我担心命名空间组织。我是否可以将单元测试放在与他们正在测试的类相同的名称空间中,尽管它们是不同项目的一部分?

This raises another question. I know that namespace naming convention is CompanyName.TechnologyName.Feautre.Design. How would I get this right in my solution/project layout? Does the solution name = companyname, project name = technology name?

这提出了另一个问题。我知道命名空间命名约定是CompanyName.TechnologyName.Feautre.Design。如何在我的解决方案/项目布局中实现这一目标?解决方案名称=公司名称,项目名称=技术名称?

If that is the case does that mean I can't separate my unit tests into a new project.

如果是这样的话,这意味着我无法将我的单元测试分成新项目。

5 个解决方案

#1


32  

The best approach is to have a separate unit test project per 'production project'. This ensures that you can vary and move them around in pairs.

最好的方法是为每个“生产项目”建立一个单独的单元测试项目。这可以确保您可以成对变化并移动它们。

If you have one unit test project covering more than one target project, this creates an artificial tight coupling between these two projects, because you will not be able to compile the unit test project without all of its target projects. This, again, makes it really hard to test a single project in isolation - which is what unit testing is all about.

如果您有一个单元测试项目涉及多个目标项目,则会在这两个项目之间创建一个人工紧密耦合,因为如果没有所有目标项目,您将无法编译单元测试项目。这再次使得单独测试单个项目变得非常困难 - 这就是单元测试的全部内容。

It's very important to keep unit tests in separate libraries, because this ensures that you test only the public API of your code (black box testing).

将单元测试保存在单独的库中非常重要,因为这可以确保您只测试代码的公共API(黑盒测试)。

I name my namespaces by appending "UnitTest" after the target namespace.

我通过在目标命名空间后附加“UnitTest”来命名我的命名空间。

#2


8  

I tend to have a specific test project/assembly per assembly. The test assembly will have the same name as the assembly it is supposed to test, with the addition of the .Test in the name and the namespace.

我倾向于每个组件都有一个特定的测试项目/组件。测试程序集将与它应该测试的程序集具有相同的名称,并在名称和名称空间中添加.Test。

That way, you keep your tests isolated and they do not get deployed with your production code.

这样,您就可以将测试隔离,并且不会使用生产代码进行部署。

#3


5  

1 test project per solution with folders => regression/integration/unit which has subfolders that 'mirrors' your solution project/folder architecture.

每个解决方案的1个测试项目,其中包含folders => regression / integration / unit,其中包含“镜像”解决方案项目/文件夹架构的子文件夹。

Remember - tests ain't your production code. They should be separated.

记住 - 测试不是您的生产代码。他们应该分开。

#4


2  

Yes. Your unit tests should be a separate project, which compiles to its own DLL.

是。您的单元测试应该是一个单独的项目,它编译为自己的DLL。

This means you're not deploying test code along with your project - and it encourages good design (since your tests can't see private/internal properties, you'll naturally tend towards testing those bits of your project which interact with other systems, rather than getting fixated on testing every detail of their internal implementation)

这意味着您不会将测试代码与项目一起部署 - 并且它鼓励良好的设计(因为您的测试无法看到私有/内部属性,您自然会倾向于测试项目中与其他系统交互的那些部分,而不是专注于测试其内部实现的每个细节)

In terms of naming, we normally pick a "codename" for each project - current one's called Zanzibar - and then end up with projects like:

在命名方面,我们通常为每个项目选择一个“代号” - 当前的一个名为Zanzibar - 然后最终得到如下项目:

MyCompany.Zanzibar.Website (ASP.NET MVC web application)

MyCompany.Zanzibar.Website(ASP.NET MVC Web应用程序)

MyCompany.Zanzibar.Website.Testing (contains unit tests for MVC web app)

MyCompany.Zanzibar.Website.Testing(包含MVC Web应用程序的单元测试)

#5


1  

Yes. Create a unit test project for each solution project.

是。为每个解决方案项目创建单元测试项目。

There will only be a single executable. The units tests are held in a DLL.

只有一个可执行文件。单元测试保存在DLL中。

Why not append 'UnitTests' to the relevant namespace.

为什么不将'UnitTests'附加到相关的命名空间。

#1


32  

The best approach is to have a separate unit test project per 'production project'. This ensures that you can vary and move them around in pairs.

最好的方法是为每个“生产项目”建立一个单独的单元测试项目。这可以确保您可以成对变化并移动它们。

If you have one unit test project covering more than one target project, this creates an artificial tight coupling between these two projects, because you will not be able to compile the unit test project without all of its target projects. This, again, makes it really hard to test a single project in isolation - which is what unit testing is all about.

如果您有一个单元测试项目涉及多个目标项目,则会在这两个项目之间创建一个人工紧密耦合,因为如果没有所有目标项目,您将无法编译单元测试项目。这再次使得单独测试单个项目变得非常困难 - 这就是单元测试的全部内容。

It's very important to keep unit tests in separate libraries, because this ensures that you test only the public API of your code (black box testing).

将单元测试保存在单独的库中非常重要,因为这可以确保您只测试代码的公共API(黑盒测试)。

I name my namespaces by appending "UnitTest" after the target namespace.

我通过在目标命名空间后附加“UnitTest”来命名我的命名空间。

#2


8  

I tend to have a specific test project/assembly per assembly. The test assembly will have the same name as the assembly it is supposed to test, with the addition of the .Test in the name and the namespace.

我倾向于每个组件都有一个特定的测试项目/组件。测试程序集将与它应该测试的程序集具有相同的名称,并在名称和名称空间中添加.Test。

That way, you keep your tests isolated and they do not get deployed with your production code.

这样,您就可以将测试隔离,并且不会使用生产代码进行部署。

#3


5  

1 test project per solution with folders => regression/integration/unit which has subfolders that 'mirrors' your solution project/folder architecture.

每个解决方案的1个测试项目,其中包含folders => regression / integration / unit,其中包含“镜像”解决方案项目/文件夹架构的子文件夹。

Remember - tests ain't your production code. They should be separated.

记住 - 测试不是您的生产代码。他们应该分开。

#4


2  

Yes. Your unit tests should be a separate project, which compiles to its own DLL.

是。您的单元测试应该是一个单独的项目,它编译为自己的DLL。

This means you're not deploying test code along with your project - and it encourages good design (since your tests can't see private/internal properties, you'll naturally tend towards testing those bits of your project which interact with other systems, rather than getting fixated on testing every detail of their internal implementation)

这意味着您不会将测试代码与项目一起部署 - 并且它鼓励良好的设计(因为您的测试无法看到私有/内部属性,您自然会倾向于测试项目中与其他系统交互的那些部分,而不是专注于测试其内部实现的每个细节)

In terms of naming, we normally pick a "codename" for each project - current one's called Zanzibar - and then end up with projects like:

在命名方面,我们通常为每个项目选择一个“代号” - 当前的一个名为Zanzibar - 然后最终得到如下项目:

MyCompany.Zanzibar.Website (ASP.NET MVC web application)

MyCompany.Zanzibar.Website(ASP.NET MVC Web应用程序)

MyCompany.Zanzibar.Website.Testing (contains unit tests for MVC web app)

MyCompany.Zanzibar.Website.Testing(包含MVC Web应用程序的单元测试)

#5


1  

Yes. Create a unit test project for each solution project.

是。为每个解决方案项目创建单元测试项目。

There will only be a single executable. The units tests are held in a DLL.

只有一个可执行文件。单元测试保存在DLL中。

Why not append 'UnitTests' to the relevant namespace.

为什么不将'UnitTests'附加到相关的命名空间。