在.NET中存储单元测试的常用方法[重复]

时间:2023-01-17 11:38:08

Possible Duplicate:
Which is better? Unit-test project per solution or per project?

可能重复:哪个更好?每个解决方案或每个项目的单元测试项目?

From time to time i see different approaches:

我不时会看到不同的方法:

someone store unittest in the same project, which they currently testing, just creating test folder under the project root.

有人在他们目前正在测试的同一个项目中存储unittest,只是在项目根目录下创建测试文件夹。

Someone create additional test project for each project from solution they testing.

有人从他们测试的解决方案为每个项目创建额外的测试项目。

Someone have one big test project for all the tests from the whole solution.

对于整个解决方案中的所有测试,有人有一个大的测试项目。

So what is general and, lets say, "official" way to store tests in .NET world? Pretty sure that Microsoft have guidelines for that.

那么什么是一般的,并且,比方说,在.NET世界中存储测试的“官方”方式?很确定微软有这方面的指导方针。

3 个解决方案

#1


2  

First rule of test-code: Never ever put test code inside your production assemblies (the deliverables).

测试代码的第一条规则:永远不要将测试代码放入生产装配(可交付成果)中。

The reason for this rule is simple: Test code can potentially corrupt your on-site installation. Even with good coding practices. Even with good programmers.

此规则的原因很简单:测试代码可能会破坏您的现场安装。即使有良好的编码实践。即使有优秀的程序员。

Second rule of test-code: It must be maintained by the programmers themselves: When the developers refactor their code, the test-code must change with it. Therefore dont put it in a separate solution if you can in any way avoid it.

测试代码的第二条规则:必须由程序员自己维护:当开发人员重构代码时,测试代码必须随之改变。因此,如果您能以任何方式避免它,请不要将其放在单独的解决方案中。

From here on you are on your own. Personally I prefer to keep about a 1 to 1 correspondence between my production assemblies and my test assemblies, appending ".Test" to the name.

从这里开始,你就是靠自己。就个人而言,我更喜欢在我的生产装配和我的测试装配之间保持1对1的对应关系,并在名称后加上“.Test”。

I then use this naming to filter them out when I do unit testing on the csproj files for production assemblies.

然后,当我对生产程序集的csproj文件进行单元测试时,我使用这个命名来过滤掉它们。

Oh did I mention that you should never put test code in production assemblies?

哦,我提到你不应该把测试代码放在生产装配中吗?

#2


1  

This question seems a bit subjective, so you're likely to get a multitude of answers, and it will ultimately be up to you to decide what works best for you and your team.

这个问题似乎有点主观,因此您可能会获得大量答案,最终由您决定什么最适合您和您的团队。

My $.02 ...

我的$ .02 ......

I generally expect to see tests in a different project than the code being tested. This keeps the resulting assemblies from getting bloated, and avoids having to deploy test code into production (or distributed to clients, depending on its purpose).

我通常希望在与正在测试的代码不同的项目中看到测试。这可以防止生成的程序集变得臃肿,并避免必须将测试代码部署到生产环境中(或根据其目的分发给客户端)。

Regarding whether or not tests should be in the same project, or distributed across multiple projects depends on the tests that are being written. If the tests are all related, and it makes sense for them to be bundled together, then put them in one project, but if they're pretty unrelated, then I see no reason why they should be in the same project.

关于测试是否应该在同一个项目中,或者分布在多个项目中取决于正在编写的测试。如果测试都是相关的,并且将它们捆绑在一起是有意义的,那么将它们放在一个项目中,但如果它们非常不相关,那么我认为没有理由将它们放在同一个项目中。

I guess the judgment can be applied to tests the same as to any code. If it goes together, put it together, if not, then don't.

我猜这个判断可以应用于任何代码的测试。如果它在一起,把它放在一起,如果没有,那么不要。

The fact that you are writing tests at all means you're probably on the right path, so don't be afraid to try something for awhile, decide if it works for you, and if not, try it a different way. The beauty of writing code is that it can be refactored over time to better suit your needs. :)

你正在编写测试的事实意味着你可能正走在正确的道路上,所以不要害怕尝试一段时间,确定它是否适合你,如果没有,请尝试不同的方式。编写代码的美妙之处在于它可以随着时间的推移进行重构,以更好地满足您的需求。 :)

#3


0  

This is what I have seen

这就是我所看到的

a) With larger teams with dedicated QA teams managing test code independently it as easier to manage your test code with its own solution.

a)大型团队由专门的QA团队独立管理测试代码,因此使用自己的解决方案更容易管理测试代码。

b) With not so large teams where dev drives the unit tests and some functional tests it become easier to manager your tests as a part of the same solution.

b)由于不是那么大的团队开发单元测试和一些功能测试,因此作为同一解决方案的一部分管理测试变得更容易。

c) And today with TDD being adopted more aggressively it becomes easier to simply have your tests as a part of your product code projects themselves.

c)今天,TDD被更积极地采用,简单地将测试作为产品代码项目本身的一部分变得更容易。

The guideline is to do place your tests where you find it best to manage them and it usually turns out to be either a or b for most people today.

指南是将测试放在最适合管理它们的地方,对于今天的大多数人来说,它通常是a或b。

UT: http://msdn.microsoft.com/en-us/library/hh598957.aspx

UT:http://msdn.microsoft.com/en-us/library/hh598957.aspx

TDD: http://msdn.microsoft.com/en-us/library/aa730844(v=vs.80).aspx

TDD:http://msdn.microsoft.com/en-us/library/aa730844(v = vs。80).aspx

#1


2  

First rule of test-code: Never ever put test code inside your production assemblies (the deliverables).

测试代码的第一条规则:永远不要将测试代码放入生产装配(可交付成果)中。

The reason for this rule is simple: Test code can potentially corrupt your on-site installation. Even with good coding practices. Even with good programmers.

此规则的原因很简单:测试代码可能会破坏您的现场安装。即使有良好的编码实践。即使有优秀的程序员。

Second rule of test-code: It must be maintained by the programmers themselves: When the developers refactor their code, the test-code must change with it. Therefore dont put it in a separate solution if you can in any way avoid it.

测试代码的第二条规则:必须由程序员自己维护:当开发人员重构代码时,测试代码必须随之改变。因此,如果您能以任何方式避免它,请不要将其放在单独的解决方案中。

From here on you are on your own. Personally I prefer to keep about a 1 to 1 correspondence between my production assemblies and my test assemblies, appending ".Test" to the name.

从这里开始,你就是靠自己。就个人而言,我更喜欢在我的生产装配和我的测试装配之间保持1对1的对应关系,并在名称后加上“.Test”。

I then use this naming to filter them out when I do unit testing on the csproj files for production assemblies.

然后,当我对生产程序集的csproj文件进行单元测试时,我使用这个命名来过滤掉它们。

Oh did I mention that you should never put test code in production assemblies?

哦,我提到你不应该把测试代码放在生产装配中吗?

#2


1  

This question seems a bit subjective, so you're likely to get a multitude of answers, and it will ultimately be up to you to decide what works best for you and your team.

这个问题似乎有点主观,因此您可能会获得大量答案,最终由您决定什么最适合您和您的团队。

My $.02 ...

我的$ .02 ......

I generally expect to see tests in a different project than the code being tested. This keeps the resulting assemblies from getting bloated, and avoids having to deploy test code into production (or distributed to clients, depending on its purpose).

我通常希望在与正在测试的代码不同的项目中看到测试。这可以防止生成的程序集变得臃肿,并避免必须将测试代码部署到生产环境中(或根据其目的分发给客户端)。

Regarding whether or not tests should be in the same project, or distributed across multiple projects depends on the tests that are being written. If the tests are all related, and it makes sense for them to be bundled together, then put them in one project, but if they're pretty unrelated, then I see no reason why they should be in the same project.

关于测试是否应该在同一个项目中,或者分布在多个项目中取决于正在编写的测试。如果测试都是相关的,并且将它们捆绑在一起是有意义的,那么将它们放在一个项目中,但如果它们非常不相关,那么我认为没有理由将它们放在同一个项目中。

I guess the judgment can be applied to tests the same as to any code. If it goes together, put it together, if not, then don't.

我猜这个判断可以应用于任何代码的测试。如果它在一起,把它放在一起,如果没有,那么不要。

The fact that you are writing tests at all means you're probably on the right path, so don't be afraid to try something for awhile, decide if it works for you, and if not, try it a different way. The beauty of writing code is that it can be refactored over time to better suit your needs. :)

你正在编写测试的事实意味着你可能正走在正确的道路上,所以不要害怕尝试一段时间,确定它是否适合你,如果没有,请尝试不同的方式。编写代码的美妙之处在于它可以随着时间的推移进行重构,以更好地满足您的需求。 :)

#3


0  

This is what I have seen

这就是我所看到的

a) With larger teams with dedicated QA teams managing test code independently it as easier to manage your test code with its own solution.

a)大型团队由专门的QA团队独立管理测试代码,因此使用自己的解决方案更容易管理测试代码。

b) With not so large teams where dev drives the unit tests and some functional tests it become easier to manager your tests as a part of the same solution.

b)由于不是那么大的团队开发单元测试和一些功能测试,因此作为同一解决方案的一部分管理测试变得更容易。

c) And today with TDD being adopted more aggressively it becomes easier to simply have your tests as a part of your product code projects themselves.

c)今天,TDD被更积极地采用,简单地将测试作为产品代码项目本身的一部分变得更容易。

The guideline is to do place your tests where you find it best to manage them and it usually turns out to be either a or b for most people today.

指南是将测试放在最适合管理它们的地方,对于今天的大多数人来说,它通常是a或b。

UT: http://msdn.microsoft.com/en-us/library/hh598957.aspx

UT:http://msdn.microsoft.com/en-us/library/hh598957.aspx

TDD: http://msdn.microsoft.com/en-us/library/aa730844(v=vs.80).aspx

TDD:http://msdn.microsoft.com/en-us/library/aa730844(v = vs。80).aspx