Click here to Skip to main content
15,895,746 members
Articles / DevOps / TFS

TFS, Automated Testing and Continuous Integration

Rate me:
Please Sign up or sign in to vote.
4.95/5 (11 votes)
10 Feb 2013CPOL7 min read 121.5K   775   32  
Automated testing and continuous integration in TFS
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PostDeploymentTests
{
    [TestClass]
    public class WindowsServiceTest
    {
        [TestMethod]
        [TestCategory("PostDeployment")]
        public void TestSimpleWindowsService()
        {
            // This value is updated by the build workflow
            string hostName = ConfigurationManager.AppSettings["TestEnvironment"];

            TestWindowsService("SimpleWindowsService", hostName);
        }

        private void TestWindowsService(string serviceName, string hostName)
        {
            using (ServiceController sc = new ServiceController(serviceName, hostName))
            {
                // Ensure the service is running
                if (!(sc.Status == ServiceControllerStatus.Running))
                {
                    WindowsServiceHelper.StartService(serviceName, hostName);

                    // This will wait up to a minute for the service to be started
                    bool isRunning = WindowsServiceHelper.PollServiceStatus(serviceName, hostName, 60, 1000, ServiceControllerStatus.Running);

                    if(!isRunning)
                        throw new AssertFailedException(string.Format("Service {0} is not running and cannot be started", serviceName));
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
I like to code and work mainly in .NET although i'm moving into other areas nowadays.
When i'm not coding I like to ride my bike

TFS Productivity Pack

Blog

Comments and Discussions