当从控制台应用程序调用时,如何让 MSTest 以管理员身份运行?

发布于 2024-09-19 06:45:41 字数 5016 浏览 6 评论 0原文

此控制台应用程序代码调用 MSTest 并开始运行单元测试,但因单元测试所需的某些程序集“访问被拒绝”而失败(请参阅代码下面的错误)。有什么方法可以让 MSTest 从这里顺利地以管理员身份运行吗?

我尝试了清单,没有更改任何内容,因为清单引用了调用应用程序,而不是 mstest,对吗?

还尝试指定 ProcessStartInfo.Username 和密码,但密码需要一些特殊的 SecureString,这不是交互式控制台应用程序,它由 SVN 在预提交挂钩上调用。

    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;
    }

错误:提交失败(详细信息如下):
错误:提交被预提交挂钩阻止(退出代码 1),输出为:
错误:正在加载 C:\Users\johndoe\Documents\SVN_TEST\Test\branches\johndoe\WebApplication1\TestProject1\bin\Debug\TestProject1.dll...
错误:开始执行...
错误:
错误:结果顶级测试
错误:------------------------
错误:TestProject1.BankTest.DebitTest 失败
错误:0/1 测试通过,1 失败
错误:
错误:摘要
错误:--------
错误:测试运行失败。
错误:失败 1
错误:---------
错误:总计 1
错误:结果文件:C:\temp\TestProject1.trx
错误:测试设置:默认测试设置
错误:
错误:运行存在以下问题:
错误:警告:测试运行部署问题:无法部署测试的依赖项
错误:存储
错误:'c:\users\johndoe\documents\svn_test\test\branches\johndoe\webapplication1\testproject1\bin\debug\testproject1.dll':
错误:System.IO.FileLoadException:无法加载文件或程序集
错误:“Microsoft.VisualStudio.QualityTools.Tips.UnitTest.AssemblyResolver,
错误:Version=10.0.0.0、Culture=neutral、PublicKeyToken=b03f5f7f11d50a3a' 或其中之一
错误:它的依赖关系。访问被拒绝。
错误:文件名:'Microsoft.VisualStudio.QualityTools.Tips.UnitTest.AssemblyResolver,
错误:版本=10.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'
错误:在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String
错误:codeBase、Evidence assemblySecurity、RuntimeAssembly locationHint、
错误:StackCrawlMark& stackMark,布尔值 throwOnFileNotFound,布尔值
错误:forIntrospection,布尔抑制SecurityChecks)
错误:在 System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String
错误:codeBase、Evidence assemblySecurity、RuntimeAssembly locationHint、
错误:StackCrawlMark& stackMark,布尔值 throwOnFileNotFound,布尔值
错误:forIntrospection,布尔抑制SecurityChecks)
错误:在 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName
错误: assemblyRef、Evidence assemblySecurity、StackCrawlMark& stackMark,布尔值
错误:forIntrospection,布尔抑制SecurityChecks)
错误:在 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString,
错误:证据组装安全、StackCrawlMark& stackMark,用于自省的布尔值)
错误:在 System.Activator.CreateInstance(字符串程序集名称,字符串类型名称,
错误:布尔值ignoreCase、BindingFlags bindingAttr、Binder 活页夹、Object[] args、
错误:CultureInfo 文化、Object[]activationAttributes、证据 securityInfo、
错误:StackCrawlMark&堆栈标记)
错误:在 System.Activator.CreateInstance(字符串程序集名称,字符串类型名称,
错误:布尔值ignoreCase、BindingFlags bindingAttr、Binder 绑定器、Object[] args、
错误:CultureInfo 文化、Object[] activateAttributes、证据 securityInfo)
错误:在 System.AppDomain.CreateInstance(字符串程序集名称,字符串类型名称,
错误:布尔值ignoreCase、BindingFlags bindingAttr、Binder 绑定器、Object[] args、
错误:CultureInfo 文化、对象 [] 激活属性、证据安全属性)
错误:在 System.AppDomain.CreateInstanceAndUnwrap(字符串程序集名称,字符串
错误:typeName、布尔值ignoreCase、BindingFlags bindingAttr、Binder 绑定器、Object[]
错误:args、CultureInfo 区域性、Object[]activationAttributes、证据 securityAttributes)
错误:在 System.AppDomain.CreateInstanceAndUnwrap(字符串程序集名称,字符串
错误:typeName、布尔值ignoreCase、BindingFlags bindingAttr、Binder 绑定器、Object[]
错误:args、CultureInfo 区域性、Object[]activationAttributes、证据 securityAttributes)
错误:位于
错误:Microsoft.VisualStudio.TestTools.Utility.AssemblyLoadStrategy.GetDependentAssemblies(字符串
错误:路径)
错误:位于
错误:Microsoft.VisualStudio.TestTools.Utility.AssemblyHelper.GetDependentAssemblies(字符串
错误:路径、DependentAssemblyOptions 选项、字符串配置文件)
错误:位于
错误:Microsoft.VisualStudio.TestTools.TestManagement.DeploymentManager.GetDependency(字符串
错误:主控、字符串配置文件、TestRunConfiguration runConfig、DeploymentItemOrigin
错误:dependencyOrigin、List1 dependencyDeploymentItems、Dictionary2 缺少DependentAssemblies)
错误:位于
错误:Microsoft.VisualStudio.TestTools.TestManagement.DeploymentManager.ProcessNewStorage(字符串
错误:testStorage、DeploymentHelper 帮助程序、DeploymentItemOriginType originType、
错误:列表1 部署CoverageItems、TestRunConfiguration runConfig、列表1
错误:dependencyDeploymentItems,Dictionary`2缺少DependentAssemblies)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-09-26 06:45:41

您可以通过执行以下操作来传递安全字符串密码

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

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;
}
百思不得你姐 2024-09-26 06:45:41

尝试添加这个

Process process = new Process();
process.StartInfo.Verb = "runas";

Try adding this

Process process = new Process();
process.StartInfo.Verb = "runas";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文