如何测试基于c#的cmdlet?

时间:2015-10-28 19:38:38

标签: c# unit-testing powershell testing

我一直在寻找一个体面的例子,大多数结果都是电源外壳的基础......

但无论如何,我确实找到了这个:http://blogs.msdn.com/b/heaths/archive/2008/04/06/functional-testing-of-cmdlets.aspx

这给了我一个开始,并允许我根据上面链接中的示例结束此测试:

using System;
using System.Collections.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using MyProject;

namespace Commands.Tests
{
    [TestClass]
    public class StartServiceTest
    {
        private RunspaceConfiguration config;

        [TestInitialize]
        public void Initialize()
        {
            config = RunspaceConfiguration.Create();
            config.Cmdlets.Append(new CmdletConfigurationEntry(
                "Start-UseService",
                typeof(StartUseServiceCommand),
                "Microsoft.Windows.Installer.PowerShell.dll-Help.xml"));
        }

        [TestMethod]
        [DeploymentItem(@"data\example.txt")]
        public void PathTest()
        {
            using (Runspace rs = RunspaceFactory.CreateRunspace(config))
            {
                rs.Open();
                using (Pipeline p = rs.CreatePipeline(@"start-useservice -service ACE"))
                {
                    Collection<PSObject> objs = p.Invoke();
                    Assert.AreEqual<int>(1, objs.Count);
                }
            }
        }
    }
}

文章没有解释任何内容,所以我确定&#34; data \ example.txt&#34;不适用于我,但example.txt用作示例测试中的参数。但是方法属性并没有被讨论过。

有没有人成功为他们的cmdlet编写过测试? 我也在寻找每件作品的一些解释。 (我对MSTest并不陌生,但我之前从未使用过System.Management.Automation / Runspaces命名空间)

我的cmdlet名称为Start-UseService

0 个答案:

没有答案
相关问题