Coded UI Tests: Start application under test only if it is not already running

automated-tests, coded-ui-tests, mstest, unit-testing, visual-studio-2010

Solution

It seems as if this can be done using this code:

[TestInitialize]
public void MyTestInitialize()
{
    if (_application != null)
    {
        return;
    }
    Process[] processes = Process.GetProcessesByName("ApplicationUnderTest");
    if (processes.Length > 0)
    {
        _application = ApplicationUnderTest.FromProcess(processes[0]);
    }
    else
    {
        _application = ApplicationUnderTest.Launch(@"C:\Path\To\ApplicationUnderTest.exe");
    }
}

The AUT is launched only if it is not already running.

Problem

When using "Coded UI tests" in Visual Studio 2010, is there an easy way to start the application under test (AUT) only if the AUT is not already running? I know that I can implement such a piece of startup code from scratch, but I wonder whether the Visual Studio test framework offers something out of the box.

Original source