簡體   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