-
-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow user to specify arguments to be passed to test files #271
Comments
This is a first crack at resolving issues pester#270 and pester#271 . I haven't written any tests around this new code yet, but it does successfully run all of Pester's existing tests (except for those looking specifically for the -Path parameter on New-PesterState; that's been removed since nothing but Invoke-Pester was actually looking at it.)
I have another usage proposal: Invoke-Pester -Path .\foo.Tests.ps1 -Parameters = @{ Parameter1 = 'Value1'; Parameter2 = 'Value2' } If somebody want to run more then 1 test file, he can call |
I tried to merge results, and I cannot find an API to do it. Does it exist? |
Nope, not at the moment. |
I don't think this is a great idea. I also think it's the author's responsibility to encourage proper testing patterns. http://googletesting.blogspot.com/2014/07/testing-on-toilet-dont-put-logic-in.html
My suggestion would be to setup Data-Driven/Parameterized tests like what is described in #179 |
The author knows jack squat about proper testing patterns. ;) I just make the thing work. This particular feature request came from VMware. |
Request also came from Microsoft, PowerShell team. We want run same test suite against different VMs in parallel (different VMs for different OS versions). Currently we workaround it with passing global variable, which is ugly. |
@vors - That's an interesting use case. Though I would argue that a test config file would be better, and there would be no need to try to support the originally requested feature. Regarding your comment on global variables being ugly: yep, they certainly are, if the global scope is cluttered with variables. That's generally bad practice. There's nothing truely wrong with a global variable, as long as they are used properly. A <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key ="foo" value ="Sample" />
</appSettings>
</configuration> var myTestConfigValue = ConfigurationManager.AppSettings[foo]; Consider the following: Run-Tests.ps1# Run-Tests.ps1
param (
[string]$TestName = "*",
[switch]$Debug,
[switch]$CurrentContext = $false
)
$ErrorActionPreference = "Stop"
if($Debug){
$DebugPreference = "Continue"
}
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path)
if($currentContext) {
Import-Module Pester
Invoke-Pester -TestName $TestName -Path $here
} else {
$cmd = 'Set-Location ''{0}''; Import-Module Pester; Invoke-Pester -TestName ''{1}'' -EnableExit;' -f $here, $TestName
powershell.exe -noprofile -command $cmd
}
Foo.Tests.ps1# Foo.Tests.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\TestCommon.ps1"
Describe ...
TestCommon.ps1# TestCommon.ps1
$private:here = Split-Path $MyInvocation.MyCommand.Path -Parent
# compute values for configuration here
. $here\test.config.ps1
# alternative get values from static configuration file (which maybe that is generated as a build action?):
$private:jsonPath = Join-Path $here 'JsonFile.json'
$global:TestConfiguration = (Get-Content $jsonPath) -join "`n" | ConvertFrom-Json
|
@ghostsquad Thank you for your thought. Let me provide a little bit more context. We want our test cases to be both developer friendly and automation friendly. It also should be easily explorable for new developers. Configuration file is not very explorable, because requiring reading the code and understand the whole test harness. Consider such code, if we allow pass parameters to tests: RunInParallel.Tests.ps1 #setup $VMs = @($vm1, $vm2, $vm3)
$pesterResults = $VMs | Invoke-Parallel {
Invoke-Pester -Path Basic.ps1 -Arguments $_ -PassThru
}
# Ideally merge results from all pester runs
# $pesterResults | merge
# for now just throw if some failed
$pesterResults | %{
if ($_.FailedCount -gt 0) { throw "$($_.FailedCount) tests failed."}
}
# cleanup $VMs Basic.ps1 <#
This suite runs basic tests agains specified VM.
It's the callers responsobility to setup VM, Import Azure module and clean it up after all.
#>
param(
[Parameter(Mandatory)]
[Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMRoleContext]$vm
)
Describe "behaviour" {
} Now it's both developer friendly: developer can pass parameter, without changing xml or global state. And automation friendly: simple call |
#270 is a great feature request. I think I changed my mind. Being able to pass arguments to 👍 |
Example usage:
The text was updated successfully, but these errors were encountered: