简体   繁体   English

如何为具有多个小数的值范围编写Pester单元测试

[英]How to write Pester Unit Test for ranges of values with multiple decimals

I have a PowerShell function that returns value with multiple decimals from a range of values. 我有一个PowerShell函数,它返回一系列值中带有多个小数的值。 How do I write Pester unit test for this kind of function. 如何为这种功能编写Pester单元测试。 This kind of function is used to check version of some of applications like Outlook. 这种功能用于检查某些应用程序(如Outlook)的版本。

function randomVersion{
    $randomVersion= Get-Random -InputObject '14.1.3.5397', '14.2.3.5399', '15.4.2.1327', '15.5.2.1328', '16.3.7.9437', '16.7.7.9438'
    return $randomVersion
}

Any way you like, depending on what exactly you want to test - Pester is Powershell code, so you could do a regex match on the string pattern: 任何你喜欢的方式,取决于你想要测试的东西--Pester是Powershell代码,所以你可以在字符串模式上进行正则表达式匹配:

randomVersion | should -Match -RegularExpression '^(14|15|16)\.[0-9]+\.[0-9]+\.[0-9]+$'

Or you could write more code around it to test more things, eg 或者你可以在它周围编写更多代码来测试更多的东西,例如

function randomVersion
{
    Get-Random -InputObject '1', 'test', '14.1.3.5397', '14.2.3.5399', '15.4.2.1327', '15.5.2.1328', '16.3.7.9437', '16.7.7.9438'
}

Import-Module -Name Pester

Describe "Tests for randomVersion" {

    It "works" {

        $result = randomVersion 

        $version = $result -as [version]
        $version | Should -not -BeNullOrEmpty

        if ($version)
        {
            $version.Major | Should -BeIn 14, 15, 16
        }
    }
}

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

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