[英]Common approach to store unittests in .NET [duplicate]

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.


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.


3 个解决方案



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.


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


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




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. :)

你正在編寫測試的事實意味着你可能正走在正確的道路上,所以不要害怕嘗試一段時間,確定它是否適合你,如果沒有,請嘗試不同的方式。編寫代碼的美妙之處在於它可以隨着時間的推移進行重構,以更好地滿足您的需求。 :)



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.


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.


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.


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.


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



粤ICP备14056181号  © 2014-2021 ITdaan.com