2

I am implementing a collection of scripts to manage backup files and restores. The scripts are based on this article: http://www.solidq.com/sqj/Documents/2010%20September%20Issue/SQJ_003_pag._30-41.pdf

I want to develop Unit Tests around the scripts. I'm interested in using a framework such as Pester or PSUnit

My problem is that I don't understand what I should be looking for in the tests. I'm new to Unit testing, and most of what I have read relies on mocking up files, changing them with a function, and verifying that you can read the value you expect out of the file.

The functions I will build interact with a collection of backup files in some read-only fashion. My first inclination is to create a static directory with a couple of files and store it with my tests. Since the code only reads from the files, the test directory will always stay the same.

This would require storing these backup files in source control, which I don't love. I also don't think this follows in the true spirit of unit tests, but I'm not sure of a better way to do it. Any suggestions would be really appreciated.

4

1 回答 1

1

我在 CodePlex 项目SQL Server Powershell Extensions的一些工作中使用了 PSUnit

查看源区域中的 PSUnit 测试脚本:http: //sqlpsx.codeplex.com/SourceControl/changeset/view/ed45217f200e#Test%20Scripts%2fTest.SQLServer.ps1

Test.SQLServer.ps1 脚本中有一个测试函数,用于测试 invoke-sqlbackup 函数:

##function Test.Invoke-SqlBackup([switch] $Skip)
function Test.Invoke-SqlBackup([switch] $Category_InvokeSql)
{
    #Arrange
    $server = get-sqlserver "$env:computername\sql2k8"
    #Act
    Invoke-SqlBackup "$env:computername\sql2k8" "pubs"  "$($server.BackupDirectory)\pubs.bak" -force
    $Actual = $?
    Write-Debug $Actual
    #Assert
    Assert-That -ActualValue $Actual -Constraint {$ActualValue}
}

对于任何基于 xUnit 的框架,您都将遵循安排、行动、断言。还可能有一个拆卸操作来执行清理后的任务。至于一般的单元测试,而不是专注于 PSUnit 或 Pester。阅读 xUnit/nUnit 框架概念。

于 2012-11-22T02:12:04.750 回答