![nunit怎么测试](https://i-blog.csdnimg.cn/blog_migrate/03e12f6fd7eab512912f20b959beffb8.png)
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怎么测试