views:

54

answers:

2

This console app code calls MSTest and starts to run the unit test but fails with "Access Denied" to some assemblies required for unit testing (see errors below code). Any way to smoothly make MSTest to run as Admin from here?

I tried a manifest, didn't change anything, since the manifest refers to the calling app, not to mstest, right?

Also tried specifying ProcessStartInfo.Username and Password, but Password wants some special SecureString, and this is NOT an interactive console app, it is called by SVN on a pre-commit hook.

    private static string MSTest()
    {
        if (File.Exists(@"C:\temp\TestProject1.trx"))
        {
            File.Delete(@"C:\temp\TestProject1.trx");
        }

        Process process = new Process();
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = @"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe";
        psi.Arguments = String.Format
            (
            "/testcontainer:{0} /resultsfile:{1} /nologo",
            @"C:\Users\johndoe\Documents\SVN_TEST\Test\branches\johndoe\WebApplication1\TestProject1\bin\Debug\TestProject1.dll",
            @"C:\temp\TestProject1.trx"
            );
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;
        process.StartInfo = psi;
        process.Start();
        string output = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        return output;
    }

Error: Commit failed (details follow):
Error: Commit blocked by pre-commit hook (exit code 1) with output:
Error: Loading C:\Users\johndoe\Documents\SVN_TEST\Test\branches\johndoe\WebApplication1\TestProject1\bin\Debug\TestProject1.dll...
Error: Starting execution...
Error:
Error: Results Top Level Tests
Error: ------- ---------------
Error: Failed TestProject1.BankTest.DebitTest
Error: 0/1 test(s) Passed, 1 Failed
Error:
Error: Summary
Error: -------
Error: Test Run Failed.
Error: Failed 1
Error: ---------
Error: Total 1
Error: Results file: C:\temp\TestProject1.trx
Error: Test Settings: Default Test Settings
Error:
Error: Run has the following issue(s):
Error: Warning: Test Run deployment issue: Failed to deploy dependencies for test
Error: storage
Error: 'c:\users\johndoe\documents\svn_test\test\branches\johndoe\webapplication1\testproject1\bin\debug\testproject1.dll':
Error: System.IO.FileLoadException: Could not load file or assembly
Error: 'Microsoft.VisualStudio.QualityTools.Tips.UnitTest.AssemblyResolver,
Error: Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of
Error: its dependencies. Access is denied.
Error: File name: 'Microsoft.VisualStudio.QualityTools.Tips.UnitTest.AssemblyResolver,
Error: Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Error: at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String
Error: codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint,
Error: StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean
Error: forIntrospection, Boolean suppressSecurityChecks)
Error: at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String
Error: codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint,
Error: StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean
Error: forIntrospection, Boolean suppressSecurityChecks)
Error: at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName
Error: assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean
Error: forIntrospection, Boolean suppressSecurityChecks)
Error: at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,
Error: Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
Error: at System.Activator.CreateInstance(String assemblyName, String typeName,
Error: Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args,
Error: CultureInfo culture, Object[] activationAttributes, Evidence securityInfo,
Error: StackCrawlMark& stackMark)
Error: at System.Activator.CreateInstance(String assemblyName, String typeName,
Error: Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args,
Error: CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
Error: at System.AppDomain.CreateInstance(String assemblyName, String typeName,
Error: Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args,
Error: CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
Error: at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String
Error: typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[]
Error: args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
Error: at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String
Error: typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[]
Error: args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
Error: at
Error: Microsoft.VisualStudio.TestTools.Utility.AssemblyLoadStrategy.GetDependentAssemblies(String
Error: path)
Error: at
Error: Microsoft.VisualStudio.TestTools.Utility.AssemblyHelper.GetDependentAssemblies(String
Error: path, DependentAssemblyOptions options, String configFile)
Error: at
Error: Microsoft.VisualStudio.TestTools.TestManagement.DeploymentManager.GetDependencies(String
Error: master, String configFile, TestRunConfiguration runConfig, DeploymentItemOrigin
Error: dependencyOrigin, List1 dependencyDeploymentItems, Dictionary2 missingDependentAssemblies)
Error: at
Error: Microsoft.VisualStudio.TestTools.TestManagement.DeploymentManager.ProcessNewStorage(String
Error: testStorage, DeploymentHelper helper, DeploymentItemOriginType originType,
Error: List1 deploymentCoverageItems, TestRunConfiguration runConfig, List1
Error: dependencyDeploymentItems, Dictionary`2 missingDependentAssemblies)

A: 

Try adding this

            Process process = new Process();
            process.StartInfo.Verb = "runas";
Pritam Karmakar
A: 

You can pass a secure string password by doing the following

if (!String.IsNullOrEmpty(Password))
{
    SecureString pwd = new SecureString();
    for (int i = 0; i < Password.Length; i++)
    {
        pwd.AppendChar(Password[i]);
    }
    process.StartInfo.Password = pwd;
}
Benjamin Baumann