开发者

How do I pass parameters to a PSUnit test script?

开发者 https://www.devze.com 2023-03-23 12:55 出处:网络
I am using PSUnit for testing purposes in Powershell 2.0. Because my tests need to connect to a database server I would like to be able to pass the server and data开发者_运维知识库base name into the t

I am using PSUnit for testing purposes in Powershell 2.0. Because my tests need to connect to a database server I would like to be able to pass the server and data开发者_运维知识库base name into the test script. This would then allow developers to run the test scripts on their local machine with a local database while at the same time making it possible to run it on a server. The database may also change depending on the environment.

The PSUnit.Run script doesn't seem to allow you to include parameters with the test script name. Have I missed anything? Is there a workaround for this?

Thanks!


The only way that I was able to find to do this was to include tags at the start of my test script, then search and replace the correct values in place of those tags. For example, in my case my test script included this code at the top of the script:

[string]$ServerName=<ServerName>
[string]$TargetDatabaseName=<TargetDatabaseName>

That is the literal code. Then, in my script where I called the tests I included this code:

foreach ($testPSScript in Get-ChildItem "$testScriptDir\*.ps1") {
    (Get-Content $testPSScript.FullName) |
    ForEach-Object {$_ -replace "<ServerName>", "'$ServerName'"} |
    ForEach-Object {$_ -replace "<TargetDatabaseName>", "'$DatabaseName'"} |
    Set-Content $testPSScript.FullName -Force

    PSUnit.Run.ps1 -PSUnitTestFile "$testPSScript"
}

You have to remember to overwrite your test script(s) with the original version each time, otherwise it will include the values that you used in your last run instead of the tags and you won't be able to change the values.


Why don't you just include the 2 variables ([string]$ServerName & [string]$TargetDatabaseName) in 'profile.ps1' file & use these in your test cases. These would be available there and you can configure them anytime in the ps1 file.

This seems to be easier & more intuitive than writing a separate script for this task.

Just append any such info to PowerShell profile.

0

精彩评论

暂无评论...
验证码 换一张
取 消