Application execution directory

Mar 28, 2010 at 11:22 AM

Hi, 

I'm trying using white with NUnit, to test a windows forms application.

I launch the application with Application.Launch("myapp"). My problem is that the execution directory is the directory of the NUnit project,

not the application. So when the application is reading files, loading assemblies dynamically etc. it does not find the files. 

Is there a way around this?

Regards

Mar 30, 2010 at 7:40 AM

// maybe the following will help

public class ApplicationUnderTest
{
    string _path = @"c:\program files\Foo\Bar.exe";
    public Application GetApplication()
    {
        Process[] processes = Process.GetProcessesByName("Bar");
        if (processes.Length == 0)
        {
            return Application.Launch(new ProcessStartInfo(_path){WorkingDirectory = Path.GetDirectoryName(_path)});
        }
        return Application.Attach(processes[0]);
    }
    
}

Mar 31, 2010 at 8:34 AM

Works like a charm. Here is my code, using NUnit.

 

 [TestFixture]
 public class TestClass
 {
        string _path = @"c:\tmp\MyApp.exe";
        Application m_application;

        [TestFixtureSetUp]
        public void fixture_setup()
        {
            Process[] processes   
                                =Process.GetProcessesByName("MyApp");
            if (processes.Length == 0)
            {
                m_application = Application.Launch(new  
                             ProcessStartInfo(_path) { WorkingDirectory = 
                             Path.GetDirectoryName(_path) });
		//wainting for startup
                Thread.Sleep(15000);
            }
            else
            {
                m_application = Application.Attach(processes[0]);
            }
        }

        [TestFixtureTearDown]
        public void fixture_teardown()
        {
            m_application.Kill();
        }

        [Test]
        public void TestStuff()
        {
            List<Window> windows = m_application.GetWindows();
            Window win = windows[0];
            Assert.AreNotEqual(null,win);
        }
 }