繁体   English   中英

在C#单元测试中执行Powershell脚本

[英]Execute Powershell script in C# Unit Test

我试图在C#中执行单元测试之前执行Powershell脚本来设置vmware ENV。

当我试图在Program.cs中执行powershell脚本时,它可以正常工作。

当我尝试从单元测试运行脚本脚本运行时,在测试开始后我可以看到异常。

System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. This can happen if the test(s) started a thread but did not stop it. Make sure that all the threads started by the test(s) are stopped before completion.

这是运行测试的示例代码:

using T;

namespace TTest
{
    [TestClass]
    public class Convert
    {
        [TestMethod]
        public void convert()
        {
            Utility.prepC();
            string actual = Program.httpPost("http://10.1.1.1:1/aaa/bbb/ccc", "{\"id\":\"111\"}");            
            string expected = "true";

            Assert.AreEqual(expected, actual,"true", "pass");          
        }

这是Utility类:

namespace T
{
    public class Utility
    { 
        static public void prepC()
        {
            PowerShell ps = PowerShell.Create();
            ps.AddScript("C:\\Users\\aaa\\Desktop\\C.ps1").Invoke();
        }
    }
}

找到了解决方案...问题是,C#代码没有等待Powershell脚本完成。

添加

Process.Start(“ powershell”,@“-noprofile -command”“&'C:\\ Users \\ aaa \\ Desktop \\ C.ps1'”“”)process.WaitForExit();

现在工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM