nunit怎么测试_使用NUnit测试PowerShell脚本

nunit怎么测试

nunit怎么测试

We've been doing lots of PowerShell at work, but we're also a continuous integration shop and we try to do TDD so testing, specifically NUnit, is very important to us.

我们一直在做很多PowerShell的工作,但是我们还是一家不断集成的公司,我们尝试进行TDD,因此测试(特别是NUnit)对我们非常重要。

Here's how Jason Scheuerman from my team tests PowerShell scripts with NUnit.

这就是我团队的Jason Scheuerman如何使用NUnit测试PowerShell脚本。

using System;using System.Collections;using System.Collections.ObjectModel;using System.Management.Automation.Runspaces;using System.Management.Automation;using NUnit.Framework;using System.Security;

使用系统; 使用System.Collections; 使用System.Collections.ObjectModel; 使用System.Management.Automation.Runspaces; 使用System.Management.Automation; 使用NUnit.Framework; 使用System.Security;

namespace PSUnitTestLibrary.Test{    [TestFixture]    public class Program    {        private Runspace myRunSpace;         [TestFixtureSetUp]        public void PSSetup()        {            myRunSpace = RunspaceFactory.CreateRunspace();            myRunSpace.Open();            Pipeline cmd = myRunSpace.CreatePipeline(@"set-Location 'C:\dev\someproject");            cmd.Invoke();         } 

命名空间PSUnitTestLibrary.Test { [ TestFixture ] 公共节目{ 私有运行空间myRunSpace; [ TestFixtureSetUp ] 公共无效PSSetup() { myRunSpace = RunspaceFactory .CreateRunspace(); myRunSpace.Open(); 管道cmd = myRunSpace.CreatePipeline( @“ set-Location'C:\ dev \ someproject” ); cmd.Invoke(); } 

        [Test]        public void PSTest()        {            Pipeline cmd = myRunSpace.CreatePipeline("get-location");            Collection<PSObject> resultObject = cmd.Invoke();            string currDir = resultObject[0].ToString();            Assert.IsTrue(currDir == @"'C:\dev\someproject");

[测试] 公共无效PSTest() { 管道cmd = myRunSpace.CreatePipeline( “ get-location” ); 集合< PSObject > resultObject = cmd.Invoke(); 字符串currDir = resultObject [0] .ToString(); 断言.IsTrue(currDir == @ “'C:\ dev的\ someproject”);

            cmd = myRunSpace.CreatePipeline(@".\new-securestring.ps1 password");            resultObject = cmd.Invoke();            SecureString ss = (SecureString)resultObject[0].ImmediateBaseObject;            Assert.IsTrue(ss.Length == 8);

cmd = myRunSpace.CreatePipeline( @“。\ new-securestring.ps1 password” ); resultObject = cmd.Invoke(); SecureString ss =( SecureString )resultObject [0] .ImmediateBaseObject; 断言.IsTrue(ss.Length == 8);

            myRunSpace.SessionStateProxy.SetVariable("ss", ss);            cmd = myRunSpace.CreatePipeline(@".\getfrom-securestring.ps1 $ss");            resultObject = cmd.Invoke();            string clearText = (string)resultObject[0].ImmediateBaseObject;            Assert.IsTrue(clearText == "password");        }    }} 

myRunSpace.SessionStateProxy.SetVariable( “ ss” ,ss); cmd = myRunSpace.CreatePipeline( @“。\ getfrom-securestring.ps1 $ ss” ); resultObject = cmd.Invoke(); 字符串clearText =(字符串)resultObject [0] .ImmediateBaseObject; 断言.IsTrue(clearText == “ password” ); } } } 

The "CreatePipeline" calls are us telling PowerShell "do that!" The Pipeline of commands is created, passed into cmd then Invoke'd.

“ CreatePipeline”调用是告诉PowerShell“这样做!” 创建命令管道,将其传递到cmd中,然后调用。

Also in this case we're assuming one PowerShell RunSpace per TestFixture and we're using the TestFixtureSetUp method to get that RunSpace going, but you could certainly move things around if you wanted different behavior or isolation.

同样在这种情况下,我们假设每个TestFixture使用一个PowerShell RunSpace,并且使用TestFixtureSetUp方法来使该RunSpace运行,但是如果您想要不同的行为或隔离,则可以随意移动。

翻译自: https://www.hanselman.com/blog/testing-powershell-scripts-with-nunit

nunit怎么测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值