locked
A simple unit test RRS feed

  • Question

  • Say I have a class in a library,

    namespace TestNlog
    {
        public class Class1
        {
            public void SomeMethod()
            {
                Console.Out.WriteLine("Greetings, some loggings is about to take place.");
                Console.Out.WriteLine("");
    
                /*
                 * This creates your logger. 
                 * Notice that in the config file we defined that everything should go into the file.
                 * (and if you set the console, it'll output to it as well).
                 */
                Logger logger = LogManager.GetCurrentClassLogger();
    
                //Console.Out.WriteLine("Let's assume you're going to work, and using the bus to get there:");
                //Console.Out.WriteLine("------------------------------------------------------------------");
                logger.Trace("Trace: The chatter of people on the street");
                logger.Debug("Debug: Where are you going and why?");
                logger.Info("Info: What bus station you're at.");
                logger.Warn("Warn: You're playing on the phone and not looking up for your bus");
                logger.Error("Error: You get on the wrong bus.");
                logger.Fatal("Fatal: You are run over by the bus.");
    
                /*
                 * Closing app
                 */
                Console.Out.WriteLine("");
                Console.Out.WriteLine("Done logging.");
                Console.Out.WriteLine("Hit any key to exit");
            }
    
        }

    Now I want unit test. What kind of the code is the best?

    I have the incomplete code here.

     [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                Assert.IsTrue(TestNlog.Class1);
            }
        }

    • Moved by Kristin Xie Thursday, May 14, 2015 8:44 AM move to the right forum
    Wednesday, May 13, 2015 6:21 PM

Answers

  • Hay, I just want the library can be use in other projects and reliable(say at least no exception when log). So in test driven environment, how to test it? By the way, it is a luxury to have Visual Studio Premium or Ultimate. I don't have it.

    Generally the libraries that you make use of written by 3rd parties have their own unit tests. For example, if you look up the source code for log4net then it has it's own set of unit tests to make sure it does not fail. There is no reason to retest log4net because that is not the point of unit testing. 

    Like JeffreyFerguson and I have both pointed out, your code does not do anything that warrants unit testing. Read my explanation of what a unit test should do again and compare it to something like this:

    // what to test
    // the method
    int AddNumbers(int number1, int number2);
    
    
    // my tests
    public void TestAddNumbers()
    {
       var result = AddNumbers(1,2);
       Assert.Equals(3,result, "Should be 3");
    }

    Here you have a method that takes parameters and returns a result. You can test different parameters and assert the expected result. Your method takes 0 parameters and does not return a result and does not alter anything so its not testable.

    Yes. Premium and Ultimate are paid versions of the product.



    Mark as answer or vote as helpful if you find it useful | Igor


    • Marked as answer by ardmore Wednesday, May 13, 2015 8:22 PM
    • Edited by IWolbers Wednesday, May 13, 2015 8:23 PM
    Wednesday, May 13, 2015 8:21 PM

All replies

  • A unit test should assert the output of a method call or other results of a method call. This includes

    • A return if there is a return type (ie. not void)
    • Any changes in state of the object that are expected
    • Any changes in state of parameters that are passed in

    A unit test should attempt to alter the method's behavior usually based on parameters that the method accepts or possible state of the method's containing object. Using various ranges in the variables to test and then assert the expected results.

    Your method above does not produce any change in state which makes in difficult to test. If you really wanted to test what was written to the logging output, which is not really recommended because it does not serve a strong purpose, you could use a Shim to shim the logger and then read the passed in strings. I saw not recommended because usually you do not test or assert the internal workings of a method, you assert the expected result as the caller and objects outside the method sees them.

    For information on shims see this page but you must be running Visual Studio Premium or Ultimate.
    https://msdn.microsoft.com/en-us/library/hh549176.aspx


    Mark as answer or vote as helpful if you find it useful | Igor

    Wednesday, May 13, 2015 6:39 PM
  • Start by asking yourself this question:

    "In my original code, what needs to be tested?"

    Since your original code doesn't seem like it does anything useful, it will be difficult to unit test. What were you hoping to have tested here?

    Wednesday, May 13, 2015 6:55 PM
  • Hay, I just want the library can be use in other projects and reliable(say at least no exception when log). So in test driven environment, how to test it? By the way, it is a luxury to have Visual Studio Premium or Ultimate. I don't have it.
    Wednesday, May 13, 2015 8:13 PM
  • But your code doesn't do anything but log messages. Can you tell us more about what you envision testing?
    Wednesday, May 13, 2015 8:16 PM
  • Hay, I just want the library can be use in other projects and reliable(say at least no exception when log). So in test driven environment, how to test it? By the way, it is a luxury to have Visual Studio Premium or Ultimate. I don't have it.

    Generally the libraries that you make use of written by 3rd parties have their own unit tests. For example, if you look up the source code for log4net then it has it's own set of unit tests to make sure it does not fail. There is no reason to retest log4net because that is not the point of unit testing. 

    Like JeffreyFerguson and I have both pointed out, your code does not do anything that warrants unit testing. Read my explanation of what a unit test should do again and compare it to something like this:

    // what to test
    // the method
    int AddNumbers(int number1, int number2);
    
    
    // my tests
    public void TestAddNumbers()
    {
       var result = AddNumbers(1,2);
       Assert.Equals(3,result, "Should be 3");
    }

    Here you have a method that takes parameters and returns a result. You can test different parameters and assert the expected result. Your method takes 0 parameters and does not return a result and does not alter anything so its not testable.

    Yes. Premium and Ultimate are paid versions of the product.



    Mark as answer or vote as helpful if you find it useful | Igor


    • Marked as answer by ardmore Wednesday, May 13, 2015 8:22 PM
    • Edited by IWolbers Wednesday, May 13, 2015 8:23 PM
    Wednesday, May 13, 2015 8:21 PM