Getting Started with Unit Testing Part 1

Download this episode

Download Video

Download captions

Download Captions

Description

This is another in Robert's Getting Started series, where he provides an introduction to a topic that everyone knows about but not everyone is actually doing (or doing enough of). In this episode, part 1 of 3, Robert shows how to get started with unit testing in Visual Studio. The primary goal of unit testing is to take the smallest piece of testable code in an application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Each unit can be tested separately and can easily be tested over and over again.

You can find the sample code and demo scripts for the entire 3 part series here.

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      shaggygi

      Good job.  Do you plan to cover how to do data driven testing with MSTest?  It appeared to be a little limited in this area, so I went with NUnit.  Thx.

    • User profile image
      Sonal

      This is interesting. I always wanted to learn about testing. How to test JavaScript programs? I create windows 8 apps with JavaScript. In visual studio 2013, I find Test under C# language, but not under JavaScript. How do I get that?

    • User profile image
      rogreen

      @shaggygi: I cover a bit on data driven testing in VS in part 2, which will go live next week. After you see that, let's chat about what you see as the limitations.

      Robert

    • User profile image
      rogreen

      @Sonal: The Microsoft unit test framework works with managed code. For JavaScript unit testing, you should check out one of the available frameworks. There is a good list of them at http://stackoverflow.com/questions/300855/javascript-unit-test-tools-for-tdd.

       

    • User profile image
      SheldonS

      Personally when I'm trying to learn something new I prefer the smaller chunks at a time.  In fact it seems like PluralSight does that with their videos as well.

    • User profile image
      Proton2

      I don't know what you will be covering in future episodes, but here are some of my notes:

      ------

      If your doing Windows Store app development, your going to have to make some changes or additions, for example:

      #if NETFX_CORE
      using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
      #else
      using Microsoft.VisualStudio.TestTools.UnitTesting;
      #endif
      
      ---
              [TestMethod]
              [TestCategory( "Timer" ), TestCategory( "Exception expected" )]
      #if !NETFX_CORE
              [ExpectedException(typeof(NotSupportedException))]
      #endif
              public void SingleTimer_OnceRanAndStopped_ItShouldNotStartAgain()
              {
                  //ToDo: Change exception type to something more appropriate.
                  SingleTimer timer = new SingleTimer();
                  timer.Start();
                  timer.Stop();
      
      #if !NETFX_CORE
                  timer.Start();
      #endif
      
      #if NETFX_CORE
                  Assert.ThrowsException<NotSupportedException>(() => timer.Start());
      #endif
      
              }
      

       

      Use templated Asserts. Example:

        Assert.AreEqual<int>( 0, timers.Count );
      

      I have more, but that's enough for now.

       

    • User profile image
      Deactivated User

      The manuscript dead tree must be replaced by something geekier like Lumia OneNote RT.

    • User profile image
      rogreen

      @VSChawathe: I am addicted to paper. What can I say? :)

    • User profile image
      Luigi Zambetti

      In the top right side of your Visual Studio environment Robert, you have 2 MB values, maybe is the Ram consumption of Visual Studio itself.
      Is this an extension?

    • User profile image
      rogreen

      @Luigi Zambetti: It is an internal only extension. Sorry.

      Robert

    • User profile image
      rogreen

      @Luigi Zambetti: It is an internal only extension. Sorry.

      Robert

    • User profile image
      mihirchitto​ra

      Thanks Robert for this awesome video. Hope to see more videos in this series.

    • User profile image
      SonOfSam

      My humble opinion will be when going from red to green you don't act instead of doing an invalid assert, please let me know what you think.

       

    • User profile image
      JockStrap

      Love the show!

      I would be interested in others opinions on whether testing is only for purist and textbooks and is not practical in real world applications.

      While "unit testing" seems great in theory, I struggle to see that it adds much value to larger applications. Basically it is lots of work to try and get code coverage and building fakes for very little return. Maybe it is just because as an old developer in my early days, my testing was "did it compile?"

    • User profile image
      abahaggag

      Great show. I have some notes which aren't that clear to me:

      1. If I want to test a method that has more than one paths, what's better: writing single unit test method that has all testing scenarios or should I divide it into different test methods one for each test case?

      2. If I have a method that has many codes which can be divided into methods but they are used only in the main method. Now if I want to test this method should I first factor these sub-methods and then test them individually or just test it as one method?

      Thanks in advance :)

    • User profile image
      csrakowski

      Great video Robert, looking forward to part 2!

    • User profile image
      wlblakeshaw

      Great episode. I've been adopting unit testing in my latest projects. There were a few things in this video I wasn't aware of, like the ExpectedException attribute. It's also nice to know you can categorize your tests. Looking forward to the next part!

    • User profile image
      tgrt

      I'm well versed on unit testing, but I was curious. I thought you did a great job covering the basics.

    • User profile image
      Niner615224

      Nice episode. Please share episode 2 & 3 too.

    • User profile image
      rogreen

      @Niner615224: Episode 2 is now live. I am recording Episode 3 tomorrow and will post it next week.

      Robert

    • User profile image
      David Gallivan

      Excellent show.

      I like the shorter segments. It breaks it out like chapters.

    • User profile image
      MBE007

      Thanks for this video . I love it.

    • User profile image
      bcnzer

      I really enjoyed this video and the length was just right - I prefer it broken out into chapters. 

      I will be speaking to my team about unit tests today!

    • User profile image
      salemsalah

      great session 

    • User profile image
      prasant

      a must watch to start :)

      I was looking for such basic video. Thanks Green.

    • User profile image
      Daniel

      Hi,
      there's a serious problem with the subtitles of this video (part1), there's a delay several seconds and it is difficult to follow.
      It would be great if you could fix it, thank!

      Thanks for this video!

    • User profile image
      Doctor Who

      This is really excellent, Rod, thank you for doing this.

      I'm find with breaking up the videos into smaller chunks.

    • User profile image
      SocalSam_

      Good stuff.  Short is better.  Excellent! Thank you!

      :D

    • User profile image
      Deactivated User
    • User profile image
      Jesse

      Great video on Unit Testing. I'm doing an Independent Study at my University on different types of Automated testing and this is a perfect starting point. You had mentioned books on Test Driven Development. Do you have any in particular you suggest I could potentially look up for my project?

      Thanks!

    • User profile image
      zia

      sir please define how to apply unit test in web form asp.net

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.