【问题标题】:Test non-idempotent web methods with Pester使用 Pester 测试非幂等 Web 方法
【发布时间】:2015-09-12 15:49:54
【问题描述】:

我正在尝试测试一个设置 SharePoint 列表项的属性值的 PowerShell 命令 (Set-PropertyValue)。该函数使用Invoke-WebMethod函数做实际工作。

我当前的Set-PropertyValue.Test.ps1 文件:

# include stuff omitted

Describe 'Set-PropertyValue' {

  #
  # arrange
  #

  # set $url, $list, $id, $property variables
  ...

  # the value that it should be 
  $expected=10.5

  BeforeEach {
    # get the property's current value
    $original = Get-PropertyValue $url $list $id $property
  }

  AfterEach {
    # restore original value
    Set-PropertyValue $url $list $id $property $original
  }

  It "Should set a property's value" {

    #
    # act
    #

    # update property's value
    $response = Set-PropertyValue $url $list $id $property $expected

    # get the new value
    $actual = Get-PropertyValue $url $list $id $property

    #
    # assert
    #

    $response.statuscode | should be 204 # no content
    $actual | Should Be $expected

  } # It

} # Describe

我不喜欢这个有很多原因:

  • Get-PropertyValue的外部依赖
  • 没有测试隔离;对 SharePoint 列表进行了更改
  • 可能会在不良状态下对列表项进行分级
  • 测试结构不适合轻松测试多个属性,可能是在一个循环中

有没有更好的测试方法?

【问题讨论】:

  • 我没有适合你的解决方案,但我一直在为同样的事情苦苦挣扎。 I found this article to be a good read.
  • 我越想这种情况,就越觉得这些真的是集成测试。
  • 没错。就个人而言,我在纠结 Pester 是否仍然是运行集成测试的合适工具,以及如何有效地实现它们。如果你发现你已经解决了这个问题,我很想听听你的想法。

标签: powershell sharepoint powershell-3.0 pester


【解决方案1】:

一种方法:

  • 将所有命令脚本放在一个子目录中(例如Functions
  • 创建一个模块文件(例如PsFoo.psm1); 'dot-source' 每个命令的脚本文件(例如Functions\Invoke-Foo.ps1),但不包括单元测试文件(例如Functions\Invoke-Foo.Tests.ps1)。
  • 创建一个“点源”模块文件并包含所有集成测试的模块测试文件(例如PsFoo.Tests.ps1)。
  • 将所有执行多个命令的单元测试(如我的问题示例)重构为集成测试
  • PS> Invoke-Pester 将运行集成测试(PsFoo.Tests.ps1)和单元测试(Functions\*.Tests.ps1)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多