Wednesday, May 27, 2009

Using nUnit AND Mstest (Visual Studio Unit Testing) together in perfect harmony

I like mstest. I like the integration. I like right clicking on a method and adding a unit test SNAP done.
It works great for me. nUnit is 'ok' to me. Integration is poor without third party tools. You have to change things around to get it to work. Sometimes I get strange errors I just dont get using mstest.
You have to set config files up to be named the same as the project. Not intuitive at first since nothing actually tells you this - you expect to be able to tell it your assembly and voila. Now.. you can use a different sequence of events to prevent this, such as add assembly before creating a project in nUnit, but thats not obvious to anyone first using it.

however.. to use nUnit on a build server I dont need to install Visual Studio. In order to use Microsoft's unit testing, you need to either install Visual Studio on the build server or go through a hack to get the components on there. Its not pretty. So nUnit shines here. But.. why not use mstest in the IDE and nUnit on your build server? They have different namespaces - but you can change the aliases around to do this successfully.

At the top of your classes in your unit test projects define the following to default to microsoft testing unless a build constant is defined called NUNIT


#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Category = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
#else
using NUnit.Framework;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestContext = System.Object;
using TestProperty = NUnit.Framework.PropertyAttribute;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
#endif



Go into Visual Studio, choose the Build menu and select "Configuration Manager"
Select "new" and name this build Nunit (or mstest or whatever you want to NOT be your debug/release build). Personally.. I use a build name called MsTest since my debug builds are what get automatically tested every night with cruisecontrol.net/nant/nUnit so I want my default builds to compile with nUnit, but when I want to add/debug unit tests in the IDE I simply change my build to MsTest.

Once you create a new build name, go into each of your projects once you select the build, and go to prokect properties. In the Build tab under "Conditional Compilation Symbols" type in MSTEST or NUNIT.
Use NUNIT if you call your build nUnit and use the code above. This will assume you want your default debug/release build to default to Microsoft. Case DOES matter for the conditional compilation constants.. make sure you use all uppercase.. its easier.

If you want to create a separate build configuration for testing Microsoft unit tets, but want to keep the default debug/release for nUnit then your headers would look like this:


#if MSTEST
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Category = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
#else
using NUnit.Framework;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestContext = System.Object;
using TestProperty = NUnit.Framework.PropertyAttribute;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
#endif



hope this helps : )

34 comments:

  1. Hello,

    your blog post was mentioned here: http://stackoverflow.com/questions/5053557/using-both-mstest-and-nunit

    Thanks!

    ReplyDelete
    Replies

    1. I have read your blog its very attractive and impressive. I like it your blog.

      Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


      Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

      Delete
  2. Thanks for sharing use interview questions on .Net technology. While preparing for my job interview, your article helped me a lot to sharpen my skills and do well in my interview. One of trainer from leading dot net training institutes in Chennai suggests me about your site.

    ReplyDelete
  3. Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students.

    ReplyDelete
  4. TechnoSoftwar having several years experience working with global customers, connecting our professionals to their business needs, as their IT Development & Support Partner. TechnoSoftwar having a team of dedicated and experienced softwares developers that works for your all business needs. TechnoSoftwar deals in web design and development, Customized ERPs, CRMs, Web & Mobile Applications, eCommerce platforms etc

    ReplyDelete
  5. Hi Dude,
    Awesome Post!!! With unique content, I really get reading interest when I am following your article, I hope I ll help many of them who looking this pretty information.
    Regards,
    Angular training in chennai|Node JS training|Angularjs training in chennai

    ReplyDelete
  6. SAP having set of modules, Industry specific modules with a deep insight. So that scope in this technology is more in recent days, I really gathered some useful information through your post, keep sharing…
    SAS Training in Chennai

    ReplyDelete
  7. Thanks for sharing this informative blog. If you are interested in taking .net in professional carrier visit this website.Dot Net Training in Chennai

    ReplyDelete
  8. Thank you for your post. This was really an appreciating one. You done a good job. Keep on blogging like this unique information with us.

    SAS Training in Chennai

    ReplyDelete
  9. this website are very important site to your in this site WP IVR Plugin or even your home and Simple Autoinstall plugin for WordPress ,click this sit e

    ReplyDelete
  10. On my current project I found it useful to use both NUnit and MsTest for unit testing. R Programming Training | DataStage Training | SQL Training | SAS Training | Android Training | SharePoint Training

    ReplyDelete
  11. Selenium is an open source automation tool where anyone can learn to automate in faster way. We offer SELENIUM Training in practical way to ensure our participant gets most of this training.
    Node.js Training | WebSphere Training | LoadRunner Training | Pentaho Training | Python Training | Selenium Training

    ReplyDelete
  12. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    salesforce training in chennai | salesforce training institute in chennai

    ReplyDelete
  13. nice and really helpful article to everyone... thanks for sharing.
    mvc training in chennai

    ReplyDelete
  14. • Thanks for all your information, Website is very nice and informative content.
    sas training in chennai

    ReplyDelete
  15. • Very good effort in collecting information.........
    tib co training in chennai

    ReplyDelete
  16. Professional Expert level Android Training in chennai, Android App Development
    Android Training | Android App Development | Training in chennai

    ReplyDelete
  17. A debt of gratitude is in order for requiring some serious energy share this post. It is truly useful. I propose to visit this site to my group members. Please continue posting this way.
    Regards,
    SAS Training in Chennai | SAS Courses in Chennai | SAS Training center in Chennai

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

    ReplyDelete
  20. I have been reading out a lot of your articles and that i ought to say pretty nice stuff. Software Testing Training Institute in Chennai | Software Testing Training Institute in Velachery.

    ReplyDelete
  21. Thank you for this valuable information. I have got some important suggestions from it. Get your business to the next level in simple steps.
    erp software solutions in chennai.

    ReplyDelete
  22. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp providers in chennai.

    ReplyDelete
  23. Nice post. Very interesting to read. Thank you for Sharing.
    erp software in chennai

    ReplyDelete
  24. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Nunit, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training Nunit . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete