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.
XTOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bissellGRCommented:
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

XTOAuthor Commented:
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?
bissellGRCommented:
That is correct. Depending on the logic that you have, you will have more than one test case. For the sample method that I provided in this post, I have about 6 different tests that I run. I am testing what I know should be true responses, but I also test out what I know should be false responses. In that manner, I am able to make sure that the code is working as expected -- succeeding when it is supposed to, and failing when it is supposed to. I have caught a number of logic bugs on the intentionally failed responses, because the code was not handling fail conditions properly.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Theory

From novice to tech pro — start learning today.