Link to home
Start Free TrialLog in
Avatar of XTO
XTO

asked on

My first ever unit test

I've tried unit tests in the past, but I'd give up out of frustration. For example, I had a Silverlight project and trying to get unit tests to work there was a headache. This time, I'm more determined than ever.
   We have a few restrictions here: For example, I can only use the tools that come with Visual Studio 2015 Enterprise edition. Using apps that are available through Nuget are outside the scope of this specific question. My company has some strict policies on that. We can come back to those tools in future questions that I will surely be asking.
   Another restriction is that I'm not testing private or internal accessible methods. I understand that there is a big debate among developers as to whether or not private methods should be tested. For now, I'm keeping it simple. I'm only testing public methods.
   Another restriction is that I'm not doing Test Driven Development yet; the key word being "yet." I'm building up to that. Also, I'm not doing "test first, code later" yet either. I'm building up to that also. For now, I'm just creating tests for the legacy code that I've started on already.
   My self-education plan is to first learn to test my legacy code, then understand the "test first" method, then grasp Test Driven Development, and finally graduate to full test driven development with design patterns and all that. (baby tortoise steps).

   Here is what I've done so far. I found a simple method and I changed it from internal to public. (I know it is not really a factory design yet even though that's what I optimistically named it.) Here it is:
public class RestServiceInfoFactory
{
    public static IRestServiceInfo CreateServiceInfo(string path)
    {
        return new ServiceInfo()
        {
            ServicePath = path
        };
    }
}

Open in new window

I right-clicked on the name of the method and I chose "Create Unit Tests."
In the dialogue box, I chose all the defaults such as MSTest. Here is the code that Visual Studio provided for me:
[TestClass()]
public class RestServiceInfoFactoryTests
{
    [TestMethod()]
    public void CreateServiceInfoTest()
    { 
        Assert.Fail();        
    }
}

Open in new window

I then changed the test method to this:
[TestMethod()]
public void CreateServiceInfoTest()
{            
    ServiceInfo si = RestServiceInfoFactory.CreateServiceInfo("http://testPath") as ServiceInfo;
    Assert.IsInstanceOfType(si, typeof(IRestServiceInfo));                  
}

Open in new window


My questions are very basic, general, broad, and simple:
Did I do this correctly? Is this acceptable? Am I on the right track with the test method code? If so, could I have made it better? How? What benefit should I hope to gain from what I've done? What have I achieved? What have I not achieved? What questions should I be asking which I've neglected? What do I learn next? What do I do next?

Thanks in advance.
Avatar of bissellGR
bissellGR

Below I have a sample Unit Test.

1. You have set up a correct test. You have a test element, and an expected result.
2. This is acceptable for this particular type of test.
3. You are on the correct path for starting simple tests (making sure something is created as an object)
4. What have you gained from this? Very little. This only tests the creation of an object. You will want to have your tests actually test the logic that is within the method you are creating (see below).
5. Very little has been accomplished other than validating that an object was made.

You will want to test out multiple test scenarios for your unit tests to validate positive and negative results to assure that your code is behaving properly, but also failing appropriately as well.

Sample Method
       public bool isValidIP4Address(string ip)
        {
            bool isValid = false;
            Regex ip4Pattern = new Regex(
                                        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                                        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                                        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                                        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");

            if (ip4Pattern.Match(ip).Success)
            {
                isValid = true;
            }
            else
            {
                logger.Info("public bool isValidIP4Address(string ip) function. ip = " + ip + ". IP Address is invalid.");
            }

            return isValid;
        }      // end of public bool isValidIP4Address(string ip)

Open in new window


Sample Unit Test
        [TestMethod]
        public void isValidIP4AddressFalse1()
        {
            // Arrange
            const bool expected = false;
            Random rnd = new Random();
            string ipAddress = rnd.Next(0, 256).ToString();
            var sd = new SiteRedirection();

            // Act
            bool actual = sd.isValidIP4Address(ipAddress);

            // Assert
            Assert.AreEqual(expected, actual);
        }

Open in new window

Avatar of XTO

ASKER

Thank you bissellGR,
   All of your comments and examples were very helpful.

   You wrote, "You will want to test out multiple test scenarios for your unit tests to validate positive and negative results to assure that . . . ."

   By saying, "multiple test scenarios" do you mean that in some cases, a single method might have multiple unit tests?
ASKER CERTIFIED SOLUTION
Avatar of bissellGR
bissellGR

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial