0

(Basic) Give me a nugget.. I mean.. a "NuGet", please?

Background

nuget-ikan-kakap-tokomesin.jpg

Wooow.. tasty... But, no, no, not that kind of nuggets!

Visual Studio (VS) has an open mechanism which allow us to use our favourite non-Microsoft libraries. And it has never been easier when Microsoft finally integrate VS2010 with its free and open repository platform "NuGet". In Microsoft, they call it simply a "package manager", while I prefer to stick with my own term, a "repository platform", because actually, you can even build your own NuGet service on premise, in your own server, as your own private repository.

Before you continue

I was using Visual Studio 2013 for all the samples and screenshots below, so your experience maybe differ based on the version you are using.

nuget.png

You can get new nuggets from NuGet (NOT)

Of course, you can't. Just go to the nearest supermarket to get some when you're hungry. But with THIS NuGet (the online version in particular) you can get any (almost) free libraries you will ever need to get the job done. I admit that this is one of the most significant feature that differentiate Visual Studio 2010 with all versions that released before it.

Let's have it a try then. For a start, how about using another unit test framework other than the one we've already tried before (in my previous article about unit testing). In Visual Studio the default unit test engine is "MsTest", but you can pick another one from NuGet that may serves its purpose better.

There are many unit test frameworks in Nuget with its own pros and cons, but for now, let's stick with one of the most popular unit test framework out there, "NUnit".

1001 ways to test your codes

Let's get back for awhile to revisit the test scripts we've ever made before :

    [TestClass]
    public class UnitTestForClass
    {
        [TestMethod]
        public void AreYouTonyStark()
        {
            var attribs = new CAttribDictionary();                          //create the initial dictionary first
            attribs.Add(Attrib.Intelligence, 170);                          //and add a new attribute 'intelligence' with its initial value 170
            attribs.Add(Attrib.Strength, 55);                               //add a new attribute 'strength' with its initial value 55
            attribs.Add(Attrib.Agility, 50);                                //add a new attribute 'agility' with its initial value 50
            attribs.Add(Attrib.Will, 500);                                  //finally, add a new attribute 'will' with its initial value 500

            Human tonyStark = new Human(1, "Tony Stark", 30, 50, attribs);  //now invoke this constructor this way

            Assert.AreEqual("Tony Stark", tonyStark.Name);                  //Are you Tony Stark?
            Assert.AreEqual(30, tonyStark.Age);                             //Are you 30 years old?
            Assert.AreEqual(50, tonyStark.Stamina);                         //Is your stamina equals 50 points?
            Assert.AreEqual(170, tonyStark.Attribs[Attrib.Intelligence]);   //Is your IQ as high as 170 points? //same question, different way
            Assert.AreEqual(55, tonyStark.Attribs[Attrib.Strength]);        //Is your strength as high as 55 points?
            Assert.AreEqual(50, tonyStark.Attribs[Attrib.Agility]);         //Is your agility as high as 50 points?
            Assert.AreEqual(500, tonyStark.Attribs[Attrib.Will]);           //Is your will as high as 500 points?
            Assert.AreEqual(100, tonyStark.HealthPoints);                   //Are you fully healthy?
        }
    }

If you feel intimidated by these long codes, you could read my previous article about unit testing first. But for short, these codes are only about giving some inputs to be consumed and then doing some assertions (or validations) to the expected output it was produced. Because that unit testing is all about, right?

Cool? Now is the time to get the NUnit library from NuGet using Visual Studio. Please right click on the reference node like this :

nunit01-hover_the_menu.jpg

And select "Manage NuGet Packages". When you click it rightly, a window will open in the middle of the screen. See the top right most textbox right there? Type "nunittestadapter" and the result will appear directly in the middle of the window like this :

nunit02-select_and_install.jpg

See the one with the Id "NUnitTestAdapter.WithFramework" right there? Install this one, and only this one! Why? Because it's the all in one version that integrates with Visual Studio seamlessly. No other library is required.

When it's done, you will notice that the references have been added with some libraries from NUnit like this :

nunit03-after_install.JPG

Now... The time is nigh, to change the way we build unit test scripts! Sorry, not yet. For now, just examine the differences when you use NUnit as the framework :

using NUnit.Framework; //Don't forget to import the NUnit library this way

    [TestFixture] //In NUnit you use this keyword, instead of "TestClass"
    public class UnitTestForClass
    {
        [TestCase] //And this keyword, instead of "TestMethod"
        public void AreYouTonyStark()
        {
            var attribs = new CAttribDictionary();                          //create the initial dictionary first
            attribs.Add(Attrib.Intelligence, 170);                          //and add a new attribute 'intelligence' with its initial value 170
            attribs.Add(Attrib.Strength, 55);                               //add a new attribute 'strength' with its initial value 55
            attribs.Add(Attrib.Agility, 50);                                //add a new attribute 'agility' with its initial value 50
            attribs.Add(Attrib.Will, 500);                                  //finally, add a new attribute 'will' with its initial value 500

            Human tonyStark = new Human(1, "Tony Stark", 30, 50, attribs);  //now invoke this constructor this way

            Assert.AreEqual("Tony Stark", tonyStark.Name);                  //Are you Tony Stark?
            Assert.AreEqual(30, tonyStark.Age);                             //Are you 30 years old?
            Assert.AreEqual(50, tonyStark.Stamina);                         //Is your stamina equals 50 points?
            Assert.AreEqual(170, tonyStark.Attribs[Attrib.Intelligence]);   //Is your IQ as high as 170 points? //same question, different way
            Assert.AreEqual(55, tonyStark.Attribs[Attrib.Strength]);        //Is your strength as high as 55 points?
            Assert.AreEqual(50, tonyStark.Attribs[Attrib.Agility]);         //Is your agility as high as 50 points?
            Assert.AreEqual(500, tonyStark.Attribs[Attrib.Will]);           //Is your will as high as 500 points?
            Assert.AreEqual(100, tonyStark.HealthPoints);                   //Are you fully healthy?
        }
    }

"That's it? That's all the difference?" Of course not, buddy. In this article I just want to emphasize on how we can utilize the online version of NuGet as a giant repository of free libraries made by visionary experts out there, to speed up our development process. So, hats off to you, gentlemen!

As I said, you can still have your own NuGet service to keep your codes in a more private environment. The choice is yours.

What we've learned so far?

###NuGet is actually the package manager for the Microsoft development platform. But for me, it's a very neat repository platform that might have been an integral part with Visual Studio now, so you .NET developers just can't live without it (pun intended).

Ok Next!

I really want to show you guys the comparison between popular unit testing frameworks out there, but somehow, to show you how far the Tony Stark project will go also one of the goal of this tutorial I set up from the beginning.

So, for now, let me think about it first..


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí