PRoblem with System.Windows.Forms.Application.StartupPath when running test project

  • Thread starter Thread starter Patrik
  • Start date Start date
P

Patrik

Hi,

I am not sure this is the right forum but I will give it a try. I am using
the built-in test project template that exists in Visual Studio Team
Edition. This project runs unit test on code that is using the property
System.Windows.Forms.Application.StartupPath. Normaly this gives me the
start location of my exe. The problem is that when running the test this
return the location of C:\Program Files\Microsoft Visual Studio
8\Common7\IDE which probably is the location of some unit tester.

I do not know how to solve this. The code is looking for a folder called
StandardIcons which do not ofcourse exist in the IDE folder. When I then try
to get files from that location I will get an DirectoryNotFound exception.

Any suggestions on how to handle this?

/Patrik
 
Hi Patrik,

This is a quick note to let you know that I'm performing research on this
issue and will get the result back to you ASAP.

I appreciate your patience!

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Patrik,

I performed a test based on your description and did reproduce the problem.

If I use the Application.ExecutablePath property in the tested method, the
returned result is 'C:\Program Files\Microsoft Visual Studio
8\Common7\IDE\VSTestHost.exe'. It is obvious that it's the VSTestHost.exe
that runs and hosts the resulted assemblies of the test project and WinForm
project when the test is run.

To solve this problem, I suggest that you pass the location of the folder
'StandardIcons' explicitly to the unit test method to get file from that
location.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi,
thanks for the reply!

A solution would ofcourse be to copy the folder "Standard Icons" to the
directory where VSTestHost.exe is located. I though find it strange that
microsoft have not thought about this problem. It must be a common problem
for many developers out there. I had hoped that there would be a more
generic way of solving the problem then to copy files in an post build
script.

/Patrik
 
Hi Patrik,

Thank you for your feedback!

I understand your concern. I am performing more research on this issue and
will get the result abck to you ASAP.

I appreciate your patience!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi Patrik,

After doing some more research, I found the solution.

The test engine runs tests not in the folder in which you have created or
generated them, but in a separate deployment folder. The deployment folder
can be local or remote. Remote test deployment occurs when you work with
controllers and agents.

For local test deployment, test engine copies deployment items, both files
and folders, to a folder on your computer before it runs tests.

There're several ways to deploy files or folders to the deployment folder.
You may refer to the following MSDN document on how to configure Test
Deployment:

'How to: Configure Test Deployment'
http://msdn2.microsoft.com/en-us/library/ms182475(vs.80).aspx

In your scenario, you can deploy the folder 'StandardIcons' to the
subdirectory named 'StandardIcons' of the deployment root directory. To do
this, you have two options.

Option 1: through run configuration
1. Follow the steps in the section 'To select files or folders to deploy,
in run configuration' in the above MSDN document to deploy the folder
'StandardIcons' to the deployment root directory.

2. Right-click the <run config file name>.testrunconfig in the Solution
Explorer and choose Open With. In the Open With dialog, choose 'XML Editor'
and click OK button.

3. In the <run config file name>.testrunconfig file, navigate to the tag
'<deploymentItems>' and add the 'StandardIcons' in the tag
'<outputDirectory>'. For example:

<deploymentItems
type="Microsoft.VisualStudio.TestTools.Common.DeploymentItemCollection">
<m_container type="System.Collections.Hashtable">
<key type="Microsoft.VisualStudio.TestTools.Common.DeploymentItem">
<path type="System.String">TestProject1\StandardIcons\</path>
<outputDirectory
type="System.String">StandardIcons</outputDirectory>
</key>
<value />
</m_container>
</deploymentItems>

Option 2: through the DeploymentItemAttribute
1. Copy the folder 'StandardIcons' under the test project folder.

2. Follow the steps in section 'To deploy items for a single test using the
DeploymentItem attribute' in the above MSDN document.

3. You need specify the second parameter for the DeploymentItemAttribute
constructor, for example:

[TestMethod()]
[DeploymentItem("TestProject1\\StandardIcons","StandardIcons")]
public void MyTestMethod()
{....}

After you deploy the entire directory of 'StandardIcons' to the deployment
root folder, you you can access the directory 'StandardIcons' directly in
the test method. For example:
[TestMethod()]
[DeploymentItem("TestProject1\\StandardIcons","StandardIcons")]
public void MyTestMethod()
{
Form1 target = new Form1();
// pass the file path to the method to test
actual = target.ReadFile("StandardIcons\\MyText.txt");

}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi,

I have more or less the same problem. In my application I use a dll that creates a directory under Application.StartupPath. This works fine when I use it in My Documents folder ( where I have access ) but when I trying to pass a test I doesn't work because it's trying to make a folder under C:\Program Files\Microsoft Visual Studio 8\Common7\IDE.

How create from my application a directory in the right place?

Thank you,

Vicenç
 
The solution is to use this:


string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

this will always return where the program is run from.
 
Hi RobinS,

In my application I'm trying to create a folder under the
Application.StartupPath and it works fine. The problem is when I'm trying to
pass unit tests, because the Application.Startup path is the Visual Studio
Folder and I don't have access to create a folder under that folder.

Thank you very much,

Vicenç
 
Instead of creating the folder under Application.StartupPath, create it
under LocalApplicationData. This is the recommended folder for writing data,
especially in Windows Vista. You can create your folder there and put files
in it, with impunity.

string userPath =
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

LocalApplicationData is for data that is user-specific (and belongs to
the user currently running the application). For data shared between
all users of the application, the kind that was often put under
installation path on Win9x, there's
Environment.SpecialFolder.CommonApplicationData.
 
RobinS said:
Yes, it is true that my example is user-specific. Just for grins, can you
actually write to and/or update files under
Environment.SpecialFolder.CommonApplicationData when using Windows Vista?
I thought I had read in another forum that you can put files there at
installation time, and read them later, but can't write to them. Anyone?

You can't write it to them with default permissions, but your installer can
create a subfolder and set permissions for it the way you want (i.e., write
for all, etc) - which is what MS recommends to do in such cases.
 
RobinS said:
So if you're using ClickOnce deployment, which has no privileges, you're
out of luck, right? Or you have to push a prerequisite that does that for
you?

From what I know about ClickOnce, that is indeed the case. As I understand,
it's sort of by design - ClickOnce apps are supposed to run in a very
isolated environment...
 
Back
Top