开发者

C# : Unit testing without using 3rd party framework?

Should unit testing be run in debug or release mode?

I am using Visual Studio Standard Edition 2005 which does not come with any unit testing framework. Since I also do not want to make use of any other 3rd party unit testing framework, I used Debug.Assert to perform the actual test inside all unit test methods. However, De开发者_高级运维bug.Assert only works in debug mode.

Is there an equivalent in release mode or is there any other alternative (without using 3rd party tools)?


Do you know that you can define the DEBUG constant in any project configuration? (in Visual Studio, Project properties - Build - Define DEBUG symbol). Or you could define a custom TEST constant (Project properties - Build - Conditional compilation symbols), and create test methods that only run when this constant is defined, by using the Conditional attribute.

Anyway I would say that using a 3rd party testing framework such as NUnit is the most appropriate way for this task, but maybe you have solid reasons for not willing to use such tools.


Don't abuse or butcher Trace.Assert or Debug.Assert for unit testing purposes.

Use a third-party framework.


http://blogs.msdn.com/billbar/pages/features-and-behavior-of-load-tests-containing-unit-tests-in-vsts-2008.aspx

As for most of your question, it depends somewhat on what unit testing tool your using. However, in general what you want are preprocessor directives

//C#

ifndef DEBUG

//Unit test

end if

Perhaps for your situation

//C# - for NUnit

if !DEBUG

[Ignore("Only valid for release")] 

end if


You can actually use Debug.Assert (or Trace.Assert) in a Release config. Check out this article on MSDN for more information.

Personally I don't see a problem with running unit tests on a Debug config. Unit tests are usually aimed at things like logic, not items that are affected by release vs debug, like performance.


Using 3rd Party testing frameworks allow you to make sure that can get better code coverage of your code. By being able to write tests to test a method instead of just checking info along the way means that you can test edge cases before they make it out into production.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜