I'm very new to powershell unit testing. I wrote my "hello world" tests below:
[CmdletBinding()]
param()
Describe MyFailTest{
Assert-Throws {
throw "I do nothing interesting."
} -MessagePattern "Not that message because I want a failure"
}
Describe MyPassTest{
Assert-Throws {
throw "I will pass"
} -MessagePattern "I will pass"
}
And that looks like I would expect when run locally, the first one fails as the message pattern did not match, and the second test passes.
Now I'm trying to run the test as part of a CI/CD pipeline. I'm using "Pester Test Runner from Black Marble" task to kick off my .tests.ps1 files. In my NUnit output I can see it marked my tests as passing and failing as expected.
So now we get to the odd part. I used the publish results task in VSTS and I only ever see failed tests. Why am I not seeing passing tests as well? Seems odd it reports that 1/1 tests failed if I have a successful test run. I don't see any options to change that. I tried setting the Outcome filter to "All" in the tests tab. Any ideas?
Pertinent log sections:
Evaluating condition for step: 'Pester Test Runner'
Evaluating: succeeded()
Evaluating succeeded:
=> (Boolean) True
Expanded: True
Result: True
##[section]Starting: Pester Test Runner
==============================================================================
Task : Pester Test Runner from Black Marble
Description : Run Pester tests without the need to install Pester in with the PMModule folder or in the source repo (Using Pester 3.4.3 or 4.0.8)
Version : 6.0.9
Author : Richard Fennell
Help : Version: 6.0.9. [More Information](https://github.com/rfennell/vNextBuild/wiki/Pester-Task/)
==============================================================================
Preparing task execution handler.
Executing the powershell script: d:\a\_tasks\Pester_31ef0033-64e3-4c55-b888-f446541474a6\6.0.9\Pester.ps1
PowerShellHandler.Execute - AddCommand(d:\a\_tasks\Pester_31ef0033-64e3-4c55-b888-f446541474a6\6.0.9\Pester.ps1)
PowerShellHandler.Execute - Add inputParameters
PowerShellHandler.Execute - AddParameter(scriptFolder=d:\a\1\s\Tasks\XXXXTWStaging\Tests\)
PowerShellHandler.Execute - AddParameter(resultsFile=d:\a\1\s\Test-Pester.XML)
PowerShellHandler.Execute - AddParameter(CodeCoverageOutputFile=)
PowerShellHandler.Execute - AddParameter(tag=)
PowerShellHandler.Execute - AddParameter(excludeTag=)
PowerShellHandler.Execute - AddParameter(pesterVersion=4.0.8)
PowerShellHandler.Execute - AddParameter(run32Bit=False)
PowerShellHandler.Execute - AddParameter(moduleFolder=)
PowerShellHandler.Execute - Invoke
Running in AMD64 PowerShell
Loading Pester module from [D:\a\_tasks\Pester_31ef0033-64e3-4c55-b888-f446541474a6\6.0.9\4.0.8]
Loading module from path 'D:\a\_tasks\Pester_31ef0033-64e3-4c55-b888-f446541474a6\6.0.9\4.0.8\Pester.psd1'.
Loading module from path 'D:\a\_tasks\Pester_31ef0033-64e3-4c55-b888-f446541474a6\6.0.9\4.0.8\Pester.psm1'.
Processed: ##vso[task.setprogress value=-1;]
Loading module from path 'D:\a\_tasks\Pester_31ef0033-64e3-4c55-b888-f446541474a6\6.0.9\4.0.8\lib\Gherkin.dll'.
namespace Pester
{
using System;
using System.Management.Automation;
public static class ClosingBraceFinder
{
public static int GetClosingBraceIndex(PSToken[] tokens, int startIndex)
{
int groupLevel = 1;
int len = tokens.Length;
for (int i = startIndex + 1; i < len; i++)
{
PSTokenType type = tokens[i].Type;
if (type == PSTokenType.GroupStart)
{
groupLevel++;
}
else if (type == PSTokenType.GroupEnd)
{
groupLevel--;
if (groupLevel <= 0) { return i; }
}
}
return -1;
}
}
}
using System;
namespace Pester
{
[Flags]
public enum OutputTypes
{
None = 0,
Default = 1,
Passed = 2,
Failed = 4,
Pending = 8,
Skipped = 16,
Inconclusive = 32,
Describe = 64,
Context = 128,
Summary = 256,
Header = 512,
All = Default | Passed | Failed | Pending | Skipped | Inconclusive | Describe | Context | Summary | Header,
Fails = Default | Failed | Pending | Skipped | Inconclusive | Describe | Context | Summary | Header
}
}
Exporting function 'Context'.
Exporting function 'Describe'.
Exporting function 'In'.
Exporting function 'It'.
Exporting function 'Mock'.
Exporting function 'Assert-VerifiableMock'.
Exporting function 'Assert-MockCalled'.
Exporting function 'Set-TestInconclusive'.
Exporting function 'Assert-VerifiableMocks'.
Exporting function 'InModuleScope'.
Exporting function 'Invoke-Mock'.
Exporting function 'New-Fixture'.
Exporting function 'Get-TestDriveItem'.
Exporting function 'Setup'.
Exporting function 'Should'.
Exporting function 'Invoke-Pester'.
Exporting function 'BeforeEach'.
Exporting function 'AfterEach'.
Exporting function 'BeforeAll'.
Exporting function 'AfterAll'.
Exporting function 'Set-DynamicParameterVariable'.
Exporting function 'Get-MockDynamicParameter'.
Exporting function 'New-PesterOption'.
Exporting function 'SafeGetCommand'.
Exporting function 'Invoke-Gherkin'.
Exporting function 'Find-GherkinStep'.
Exporting function 'Invoke-GherkinStep'.
Exporting function 'BeforeEachFeature'.
Exporting function 'AfterEachFeature'.
Exporting function 'BeforeEachScenario'.
Exporting function 'AfterEachScenario'.
Exporting function 'GherkinStep'.
Exporting alias 'Given'.
Exporting alias 'When'.
Exporting alias 'Then'.
Exporting alias 'And'.
Exporting alias 'But'.
Exporting function 'Add-AssertionOperator'.
Exporting function 'New-MockObject'.
Importing function 'Add-AssertionOperator'.
Importing function 'AfterAll'.
Importing function 'AfterEach'.
Importing function 'AfterEachFeature'.
Importing function 'AfterEachScenario'.
Importing function 'Assert-MockCalled'.
Importing function 'Assert-VerifiableMock'.
Importing function 'Assert-VerifiableMocks'.
Importing function 'BeforeAll'.
Importing function 'BeforeEach'.
Importing function 'BeforeEachFeature'.
Importing function 'BeforeEachScenario'.
Importing function 'Context'.
Importing function 'Describe'.
Importing function 'Find-GherkinStep'.
Importing function 'Get-MockDynamicParameter'.
Importing function 'Get-TestDriveItem'.
Importing function 'GherkinStep'.
Importing function 'In'.
Importing function 'InModuleScope'.
Importing function 'Invoke-Gherkin'.
Importing function 'Invoke-GherkinStep'.
Importing function 'Invoke-Mock'.
Importing function 'Invoke-Pester'.
Importing function 'It'.
Importing function 'Mock'.
Importing function 'New-Fixture'.
Importing function 'New-MockObject'.
Importing function 'New-PesterOption'.
Importing function 'SafeGetCommand'.
Importing function 'Set-DynamicParameterVariable'.
Importing function 'Set-TestInconclusive'.
Importing function 'Setup'.
Importing function 'Should'.
Importing alias 'And'.
Importing alias 'But'.
Importing alias 'Given'.
Importing alias 'Then'.
Importing alias 'When'.
Running Pester from [d:\a\1\s\Tasks\XXXXTWStaging\Tests\] output sent to [d:\a\1\s\Test-Pester.XML]
Exporting function 'EnterTestGroup'.
Exporting function 'LeaveTestGroup'.
Exporting function 'AddTestResult'.
Exporting function 'AddSetupOrTeardownBlock'.
Exporting function 'GetTestCaseSetupBlocks'.
Exporting function 'GetTestCaseTeardownBlocks'.
Exporting function 'GetCurrentTestGroupSetupBlocks'.
Exporting function 'GetCurrentTestGroupTeardownBlocks'.
Exporting function 'EnterTest'.
Exporting function 'LeaveTest'.
Exporting variable 'Strict'.
Exporting variable 'Show'.
Exporting variable 'TagFilter'.
Exporting variable 'ExcludeTagFilter'.
Exporting variable 'TestNameFilter'.
Exporting variable 'SessionState'.
Exporting variable 'CommandCoverage'.
Exporting variable 'InTest'.
Exporting variable 'TestResult'.
Exporting variable 'TotalCount'.
Exporting variable 'Time'.
Exporting variable 'PassedCount'.
Exporting variable 'FailedCount'.
Exporting variable 'SkippedCount'.
Exporting variable 'PendingCount'.
Exporting variable 'InconclusiveCount'.
Exporting variable 'IncludeVSCodeMarker'.
Exporting variable 'TestSuiteName'.
Exporting variable 'TestActions'.
Exporting variable 'TestGroupStack'.
Executing all tests in 'd:\a\1\s\Tasks\XXXXTWStaging\Tests\'
Executing script D:\a\1\s\Tasks\XXXXTWStaging\Tests\L0ValidateDestinationPath.tests.ps1
Importing module: TestHelpersModule
Stubbing command: Import-Module
Setting copyFilesToMachinesPath
Asserting script block should throw: {
Validate-DestinationPath -value "" -environmentName $validEnvironmentName
}
Success. Matched exception message. Pattern: WFC_ParameterCannotBeNullorEmpty targetPath ; Message: WFC_ParameterCannotBeNullorEmpty targetPath
Asserting script block should throw: {
Validate-DestinationPath -value $invalidTargetPath -environmentName $validEnvironmentName
}
Success. Matched exception message. Pattern: WFC_RemoteDestinationPathCannotContainEnvironmentVariables $env:abc\123 ; Message: WFC_RemoteDestinationPathCannotContainEnvironmentVariables $env:abc\123
Executing script D:\a\1\s\Tasks\XXXXTWStaging\Tests\L0ValidateSourcePath.tests.ps1
Importing module: TestHelpersModule
Invoking mock command: Import-Module
Arguments: D:\a\1\s\Tasks\XXXXTWStaging\Tests\lib/TestHelpersModule -Verbose: False
Command is stubbed.
Stubbing command: Import-Module
Mocking command: Test-Path
ParametersEvaluator: { $LiteralPath -eq $invalidSourcePath }
Func: { return $false }
Asserting script block should throw: {
Validate-SourcePath -value ""
}
Success. Matched exception message. Pattern: WFC_ParameterCannotBeNullorEmpty sourcePath ; Message: WFC_ParameterCannotBeNullorEmpty sourcePath
Asserting script block should throw: {
Validate-SourcePath -value "$invalidSourcePath"
}
Invoking mock command: Test-Path
Arguments: -LiteralPath Invalid
Matching implementation found using parameters evaluator: { $LiteralPath -eq $invalidSourcePath }
Invoking Func: { return $false }
Success. Matched exception message. Pattern: WFC_SourcePathDoesNotExist Invalid ; Message: WFC_SourcePathDoesNotExist Invalid
Executing script D:\a\1\s\Tasks\XXXXTWStaging\Tests\L0ValidInputSequentialCopy.tests.ps1
Importing module: TestHelpersModule
Invoking mock command: Import-Module
Arguments: D:\a\1\s\Tasks\XXXXTWStaging\Tests\lib/TestHelpersModule -Verbose: False
Command is stubbed.
Stubbing command: Import-Module
Setting copyFilesToMachinesPath
Mocking command: Test-Path
ParametersEvaluator: { $LiteralPath -eq $validSourcePackage }
Func: { return $true }
Mocking command: Test-Path
ParametersEvaluator: { $LiteralPath -eq $invalidSourcePath }
Func: { return $false }
Mocking command: Invoke-Command
Func: { }
Mocking command: ConvertTo-SecureString
Func: { return $password }
Mocking command: Get-VstsInput
ParametersEvaluator: { $Name -eq "SourcePath" }
Func: { return $validSourcePackage }
Mocking command: Get-VstsInput
ParametersEvaluator: { $Name -eq "TargetPath" }
Func: { return $validApplicationPath }
Mocking command: Get-VstsInput
ParametersEvaluator: { $Name -eq "CleanTargetBeforeCopy" }
Func: { return $true }
Mocking command: Get-VstsInput
ParametersEvaluator: { $Name -eq "TechWintelDeployService" }
Func: { return "TechWintelDeployService" }
Mocking command: Get-VstsEndpoint
ParametersEvaluator: { $Name -eq "TechWintelDeployService" }
Func: { return $myVar }
Entering D:\a\1\s\Tasks\XXXXTWStaging\WindowsMachineFileCopy.ps1.
Invoking mock command: Get-VstsInput
Arguments: -Name TechWintelDeployService -Require
Matching implementation found using parameters evaluator: { $Name -eq "TechWintelDeployService" }
Invoking Func: { return "TechWintelDeployService" }
Invoking mock command: Get-VstsEndpoint
Arguments: -Name TechWintelDeployService -Require
Matching implementation found using parameters evaluator: { $Name -eq "TechWintelDeployService" }
Invoking Func: { return $myVar }
Invoking mock command: Get-VstsInput
Arguments: -Name SourcePath -Require
Matching implementation found using parameters evaluator: { $Name -eq "SourcePath" }
Invoking Func: { return $validSourcePackage }
Invoking mock command: Get-VstsInput
Arguments: -Name TargetPath -Require
Matching implementation found using parameters evaluator: { $Name -eq "TargetPath" }
Invoking Func: { return $validApplicationPath }
Invoking mock command: Get-VstsInput
Arguments: -Name AdditionalArguments
Command is stubbed.
Invoking mock command: Get-VstsInput
Arguments: -Name CleanTargetBeforeCopy
Matching implementation found using parameters evaluator: { $Name -eq "CleanTargetBeforeCopy" }
Invoking Func: { return $true }
Importing VSTSLocStrings
Invoking mock command: Test-Path
Arguments: -LiteralPath C:\Windows\Test
Matching implementation found using parameters evaluator: { $LiteralPath -eq $validSourcePackage }
Invoking Func: { return $true }
Invoking mock command: ConvertTo-SecureString
Arguments: Password -AsPlainText -Force
Invoking Func: { return $password }
Invoking mock command: Invoke-Command
Arguments: -ScriptBlock
param (
[string]$fqdn,
[string]$sourcePath,
[string]$targetPath,
[object]$credential,
[string]$cleanTargetBeforeCopy,
[string]$additionalArguments,
[string]$scriptRoot
)
Import-Module "$scriptRoot\..\ps_modules\VstsTaskSdk"
Import-VstsLocStrings -LiteralPath $scriptRoot/Task.json
Write-Verbose "Entering script RobocopyJob.ps1"
Write-Verbose "fqdn = $fqdn"
Write-Verbose "sourcePath = $sourcePath"
Write-Verbose "targetPath = $targetPath"
Write-Verbose "credential = $credential"
Write-Verbose "cleanTargetBeforeCopy = $cleanTargetBeforeCopy"
Write-Verbose "additionalArguments = $additionalArguments"
$sourcePath = $sourcePath.Trim().TrimEnd('\', '/')
$targetPath = $targetPath.Trim().TrimEnd('\', '/')
$isFileCopy = Test-Path -Path $sourcePath -PathType Leaf
$doCleanUp = $cleanTargetBeforeCopy -eq "true"
$sourceDirectory = $sourcePath
$filesToCopy = ""
if($isFileCopy)
{
$sourceDirectory = Split-Path $sourcePath
$filesToCopy = Split-Path $sourcePath -Leaf
}
function ThrowError
{
param(
[string]$errorMessage,
[string]$fqdn
)
$failMessage = (Get-VstsLocString -Key "WFC_CopyingFailedForResource" -ArgumentList $fqdn)
throw "$failMessage`n$errorMessage"
}
function Validate-Null(
[string]$value,
[string]$variableName
)
{
$value = $value.Trim()
if(-not $value)
{
ThrowError -errorMessage (Get-VstsLocString -Key "WFC_ParameterCannotBeNullorEmpty" -ArgumentList $variableName)
}
}
function Validate-Credential(
[object]$credential)
{
if($credential)
{
Validate-Null $credential.UserName "Username"
Validate-Null $credential.Password "Password"
}
else
{
ThrowError -errorMessage (Get-VstsLocString -Key "WFC_ParameterCannotBeNullorEmpty" -ArgumentList "credential")
}
}
function Get-DownLevelLogonName(
[string]$fqdn,
[string]$userName
)
{
if($userName -like '.\*') {
$userName = $userName.replace(".\","\")
$userName = $fqdn+$userName
}
return $userName
}
function Replace-First(
[string]$text,
[string]$search,
[string]$replace
)
{
$pos = $text.IndexOf($search);
if ($pos -le 0)
{
return $text;
}
return $text.Substring(0, $pos) + $replace + $text.Substring($pos + $search.Length);
}
function Get-DestinationNetworkPath(
[string]$targetPath,
[string]$machineShare
)
{
if(-not $machineShare)
{
return $targetPath
}
$targetSpecificPath = Replace-First $targetPath ":" '$'
return [io.path]::Combine($machineShare, $targetSpecificPath)
}
function Get-RoboCopyParameters(
[string]$additionalArguments,
[switch]$fileCopy,
[switch]$clean)
{
$robocopyParameters = "/COPY:DAT"
if(-not $fileCopy.IsPresent)
{
if($clean.IsPresent)
{
$robocopyParameters += " /MIR"
}
else
{
$robocopyParameters += " /E"
}
}
if (-not [string]::IsNullOrWhiteSpace($additionalArguments))
{
$robocopyParameters += " $additionalArguments"
}
return $robocopyParameters.Trim()
}
function Get-MachineShare(
[string]$fqdn,
[string]$targetPath
)
{
if([bool]([uri]$targetPath).IsUnc)
{
return $targetPath
}
if($fqdn)
{
return [IO.Path]::DirectorySeparatorChar + [IO.Path]::DirectorySeparatorChar + $fqdn
}
return ""
}
function Get-NetExeCommand
{
$netExePath = Join-Path -path (get-item env:\windir).value -ChildPath system32\net.exe
if(Test-Path $netExePath)
{
Write-Verbose "Found the net exe path $netExePath. Net command will be $netExePath"
return $netExePath
}
Write-Verbose "Unable to get the path for net.exe. Net command will be 'net'"
return 'net'
}
$machineShare = Get-MachineShare -fqdn $fqdn -targetPath $targetPath
$destinationNetworkPath = Get-DestinationNetworkPath -targetPath $targetPath -machineShare $machineShare
Validate-Credential $credential
$userName = Get-DownLevelLogonName -fqdn $fqdn -userName $($credential.UserName)
$password = $($credential.Password)
$netExeCommand = Get-NetExeCommand
if($machineShare)
{
$command = "$netExeCommand use `"$machineShare`""
if($userName)
{
$command += " /user:`'$userName`' `'$($password -replace "['`]", '$&$&')`'"
}
$command += " 2>&1"
$dtl_mapOut = iex $command
if ($LASTEXITCODE -ne 0)
{
$errorMessage = (Get-VstsLocString -Key "WFC_FailedToConnectToPathWithUser" -ArgumentList $machineShare, $($credential.UserName)) + $dtl_mapOut
ThrowError -errorMessage $errorMessage -fqdn $fqdn
}
}
try
{
if($isFileCopy -and $doCleanUp -and (Test-Path -path $destinationNetworkPath -pathtype container))
{
Get-ChildItem -Path $destinationNetworkPath -Recurse -force | Remove-Item -force -recurse;
$output = Remove-Item -path $destinationNetworkPath -force -recurse 2>&1
$err = $output | ?{$_.gettype().Name -eq "ErrorRecord"}
if($err)
{
Write-Verbose -Verbose "Error occurred while deleting the destination folder: $err"
}
}
$robocopyParameters = Get-RoboCopyParameters -additionalArguments $additionalArguments -fileCopy:$isFileCopy -clean:$doCleanUp
$command = "robocopy `"$sourceDirectory`" `"$destinationNetworkPath`" `"$filesToCopy`" $robocopyParameters"
Invoke-Expression $command
if ($LASTEXITCODE -ge 8)
{
$errorMessage = Get-VstsLocString -Key "WFC_CopyingFailedConsultRobocopyLogsForMoreDetails"
ThrowError -errorMessage $errorMessage -fqdn $fqdn
}
else
{
$message = (Get-VstsLocString -Key "WFC_CopyingRecurivelyFrom0to1MachineSucceed" -ArgumentList $sourcePath, $targetPath, $fqdn)
Write-Output $message
}
}
finally
{
if($machineShare)
{
$dtl_deleteMap = iex "$netExeCommand use `"$machineShare`" /D /Y";
}
}
-ArgumentList System.Object[]
Invoking Func: { }
Leaving D:\a\1\s\Tasks\XXXXTWStaging\WindowsMachineFileCopy.ps1.
Asserting was-called: Invoke-Command
Expected times: 1
Actual times: 1
Executing script D:\a\1\s\Tasks\XXXXTWStaging\Tests\MyFailTest.tests.ps1
Describing MyFailTest
Invoking mock command: Test-Path
Arguments: TestDrive:\
Command is stubbed.
Asserting script block should throw: {
throw "I do nothing interesting."
}
[-] Error occurred in Describe block
778ms
RuntimeException: Actual exception message does not match expected pattern. Expected: Not that message because I want a failure ; Actual: I do nothing interesting.
at Assert-Throws, D:\a\1\s\Tasks\XXXXTWStaging\Tests\lib\TestHelpersModule\PublicFunctions.ps1: line 67
at <ScriptBlock>, D:\a\1\s\Tasks\XXXXTWStaging\Tests\MyFailTest.tests.ps1: line 5
at DescribeImpl, D:\a\_tasks\Pester_31ef0033-64e3-4c55-b888-f446541474a6\6.0.9\4.0.8\Functions\Describe.ps1: line 161
Describing MyPassTest
Invoking mock command: Test-Path
Arguments: TestDrive:\
Command is stubbed.
Asserting script block should throw: {
throw "I will pass"
}
Success. Matched exception message. Pattern: I will pass ; Message: I will pass
Tests completed in 778ms
Tests Passed: 0,
Failed: 1,
Skipped: 0,
Pending: 0,
Inconclusive: 0
Perform operation 'Enumerate CimInstances' with following parameters, ''namespaceName' = root\cimv2,'className' = Win32_OperatingSystem'.
Operation 'Enumerate CimInstances' complete.
##[error]Microsoft.PowerShell.Commands.WriteErrorException: Pester returned errors
Processed: ##vso[task.logissue type=error;]Microsoft.PowerShell.Commands.WriteErrorException: Pester returned errors
##[error]PowerShell script completed with 1 errors.
##[section]Finishing: Pester Test Runner
Evaluating condition for step: 'Publish Test Results Test-Pester.XML'
Evaluating: succeededOrFailed()
Evaluating succeededOrFailed:
=> (Boolean) True
Expanded: True
Result: True
##[section]Starting: Publish Test Results Test-Pester.XML
==============================================================================
Task : Publish Test Results
Description : Publish Test Results to VSTS/TFS
Version : 2.0.1
Author : Microsoft Corporation
Help : [More Information](https://go.microsoft.com/fwlink/?LinkID=613742)
==============================================================================
agent.workFolder=d:\a
loading inputs and endpoints
loading ENDPOINT_AUTH_PARAMETER_SYSTEMVSSCONNECTION_ACCESSTOKEN
loading ENDPOINT_AUTH_SCHEME_SYSTEMVSSCONNECTION
loading ENDPOINT_AUTH_SYSTEMVSSCONNECTION
loading INPUT_MERGETESTRESULTS
loading INPUT_PUBLISHRUNATTACHMENTS
loading INPUT_SEARCHFOLDER
loading INPUT_TESTRESULTSFILES
loading INPUT_TESTRUNNER
loaded 8
testRunner=NUnit
testResultsFiles=Test-Pester.XML
mergeTestResults=true
platform=null
configuration=null
testRunTitle=null
publishRunAttachments=true
searchFolder=d:\a\1\s
testRunner: NUnit
testResultsFiles: Test-Pester.XML
mergeResults: true
platform: null
config: null
testRunTitle: null
publishRunAttachments: true
defaultRoot: 'd:\a\1\s'
findOptions.followSpecifiedSymbolicLink: 'true'
findOptions.followSymbolicLinks: 'true'
matchOptions.debug: 'false'
matchOptions.nobrace: 'true'
matchOptions.noglobstar: 'false'
matchOptions.dot: 'true'
matchOptions.noext: 'false'
matchOptions.nocase: 'true'
matchOptions.nonull: 'false'
matchOptions.matchBase: 'false'
matchOptions.nocomment: 'false'
matchOptions.nonegate: 'false'
matchOptions.flipNegate: 'false'
pattern: 'Test-Pester.XML'
findPath: 'd:\a\1\s\Test-Pester.XML'
statOnly: 'true'
found 1 paths
applying include pattern
adjustedPattern: 'd:\a\1\s\Test-Pester.XML'
1 matches
1 final results
Reading test results from file 'd:\a\1\s\Test-Pester.XML'
Processed: ##vso[results.publish type=NUnit;mergeResults=true;publishRunAttachments=true;resultFiles=d:\a\1\s\Test-Pester.XML;]
task result: Succeeded
Processed: ##vso[task.complete result=Succeeded;]
##[section]Async Command Start: Publish test results
Publishing test results to test run '17948'
Test results remaining: 1. Test run id: 17948
Published Test Run : https://XXXX.visualstudio.com/DevOps/_TestManagement/Runs#runId=17948&_a=runCharts
##[section]Async Command End: Publish test results
##[section]Finishing: Publish Test Results Test-Pester.XML
At the suggestion of a coworker I tried adding "It" blocks around my tests. That seemed to work. I won't mark this as the answer yet as I didn't see it blocks in the example I was working off of. My code now looks like:
[CmdletBinding()]
param()
Describe MyFailTest{
It "This will fail, the patterns don't match"{
Assert-Throws {
throw "I do nothing interesting."
} -MessagePattern "Not that message because I want a failure"
}
}
Describe MyPassTest{
It "This will pass, the patterns do match"{
Assert-Throws {
throw "I will pass"
} -MessagePattern "I will pass"
}
}
Related
I am trying to execute the if & else code but unable to pass through the condition
{
if(($ENV_TYPE = 'Dev') && ($REGION_CODE = 'eus2'))
{
}
else
{
}
}
Both the env_type and region_code values are getting from env variables
I get this error:
The token '&&' is not a valid statement separator in this version.
if ($ENV_TYPE -eq 'Dev' -And $REGION_CODE -eq 'eus2'){
}
I am unable to set Argument while creating Unit test case in Symfony 3.4 console command application
My Console command
php bin\console identification-requests:process input.csv
My Console code
protected function execute(InputInterface $input, OutputInterface $output)
{
// Retrieve the argument value using getArgument()
$csv_name = $input->getArgument('file');
// Check file name
if ($csv_name == 'input.csv') {
// Get input file from filesystem
$csvData = array_map('str_getcsv', file($this->base_path.$csv_name));
$formatData = Helpers::formatInputData($csvData);
// Start session
$session = new Session();
$session->start();
foreach ($csvData as $key => $data) {
if (!empty($data[0])) {
$validation = Validator::getInformationData($data, $formatData[$data[1]]);
if (!empty($validation)) {
$output->writeln($validation);
} else {
$output->writeln('valid');
}
}
}
} else {
$output->writeln('Invalid file!');
}
}
I tried the following test code
$kernel = static::createKernel();
$kernel->boot();
$application = new Application($kernel);
$application->add(new DocumentCommand());
$command = $application->find('identification-requests:process')
->addArgument('file', InputArgument::REQUIRED, "input.csv");
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName()
));
$output = $commandTester->getOutput();
$this->assertContains('valid',$output);
When I run unit test it showing the following error message
There was 1 error:
1) Tests\AppBundle\Command\DocumentCommandTest::testExecute
Symfony\Component\Console\Exception\LogicException: An argument with name "file" already exists.
I think you should put your input in the command tester and not in the command finder, in this case you are trying to create another parameter for that command, and that's why it's telling you that it is existing already.
try this
$command = $application->find('identification-requests:process');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'file' => 'input.csv'
));
I have a function which looks like the one written below. I need to write a unit test for the below function. I am not able to mock few values.
use File::Basename;
use File::Copy;
use File::Temp qw(tempdir);
our $CONFIG="/home/chetanv/svf.xml";
copyConfigFiles();
sub copyConfigFiles {
my $temp_dir = File::Temp->newdir();
my $targetpath = dirname("$CONFIG");
`sudo touch $tempdir/myfile`;
make_path($targetpath) if ( ! -d $targetpath );
File::Copy::copy("$temp_dir/myfile", $targetpath) or print "Problem copying file";
}
I have written the below unit test for the same below. I tried mocking "makepath" which does not seem to work.
subtest _setup(testname => "test copyConfigFiles") => sub {
my $CONFIG = "/my/dir/with/file.xml";
my $mockfileobj = Test::MockModule->new("File::Temp", no_auto => 1);
$mockfileobj->mock('newdir', sub { return "/tmp/delme"; } );
my $amockfileobj = Test::MockModule->new("File::Path", no_auto => 1);
$amockfileobj->mock('makepath', sub { return 0; } );
lives_ok { copyConfigFiles () } 'test copyConfigFiles OK';
done_testing();
};
The problem is i am not able to mock the below lines.
make_path($targetpath) if ( ! -d $targetpath );
File::Copy::copy("$temp_dir/myfile", $targetpath) or print "Problem copying file";
Any help on how i can mock the makepath function which is perl specific? I also tried to create a temporary directory and mock the global CONFIG file with the mocked file. Didn't seem to work.
If I ignore the code that can't be run because the context is missing and focus only on the two functions that you want mocked, one can do this by temporarily replacing the subs in the symbol table with local.
use warnings;
use strict;
use Data::Dump;
use File::Path qw/make_path/;
use File::Copy;
sub to_be_mocked {
my $targetpath = '/tmp/foo';
make_path($targetpath) if ! -d $targetpath;
File::Copy::copy("file.xml", $targetpath) or die;
}
sub run_with_mock {
no warnings 'redefine';
local *make_path = sub { dd 'make_path', #_; return 1 };
local *File::Copy::copy = sub { dd 'copy', #_; return 1 };
to_be_mocked();
}
run_with_mock();
__END__
# Output:
("make_path", "/tmp/foo")
("copy", "file.xml", "/tmp/foo")
Note that -d apparently can't be mocked, at least not directly.
So have a function that does something like this:
function mymod_init()
{
$ip = '123.123.123.123';
$newPath = mymod_redirect_calculate($ip);
if (!empty($newPath)) drupal_goto($newPath);
}
This completely breaks unit testing. I have tests for the "mymod_redirect_calculate", but if I add the above to my init function as shown, the tests can't run.
From what I have gathered "exit" and "drupal_goto" breaks unit testing.
How do I get around this?
Actually drupal_goto() ends up calling exit(). Using header() should work :
function mymod_init() {
$ip = '123.123.123.123';
$newPath = mymod_redirect_calculate($ip);
if (!empty($newPath)) {
$url = url($newPath, array('absolute' => TRUE));
header('Location: ' . $url, TRUE);
}
}
We're developing in Java for the most, but we want to integration test (using https://github.com/scottmuc/Pester) our web-services with ms as well. To do this I'm writing powershell scripts that connects to a web-service and compares the response to xml that I've loaded from a file.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$instance = New-WebServiceProxy -Uri "https://localhost:7002/service?WSDL" -Namespace "myspace"
$instance.Credentials = new-object System.Net.NetworkCredential("user", "pass")
...
$reply = $instance.fetchInformation($inputA, $inputB)
[xml]$expected = Get-Content ("expected.xml")
...
However, now I have a $reply that contains objects from the myspace namespace and an $expected that contains an XMLNode. I see two ways I can do this (there are probably many more):
Get the original XML response and compare that. However, I can't seem to find out how to get that.
Serialise the $expected XML into the myspace namespace objects. Is that possible?
You could serialize the response returned by the web service to XML and compare it with the contents of the expected.xml file as strings.
Here's an example:
$writer = New-Object System.IO.StringWriter
$serializer = New-Object System.Xml.Serialization.XmlSerializer($reply.GetType())
$serializer.Serialize($writer, $reply)
$replyAsXml = $writer.ToString()
$expectedReplyAsXml = Get-Content expected.xml
$replyAsXml -eq $expectedReplyAsXml
Note that in this example you need to make sure that XML contained in the expected.xml file matches the one returned by the XmlSerializer also in regard to spacing and indenting. In order to avoid that, you could strip all extra characters (such as spaces and newlines) from the two strings before comparing them.
I ended up with a completely different approach. The two XML's was quite different from each other so instead I created a custom comparator. This made it possible for me to simply write custom code to ignore uninteresting differences.
This lead to some pile of crude code that does the job:
# Assume two arrays of equal length
Function Zip {
Param($a1, $a2)
$sum = New-Object object[] $a1.Count
For ($i = 0; $i -lt $a1.Count; ++$i) {
$sum[$i] = New-Object object[] 2
$sum[$i][0] = $a1[$i]
$sum[$i][1] = $a2[$i]
}
Return ,$sum
}
Function XmlChildNodes2List{
param($nodes)
$myArray = New-Object object[] 0
For ($i = 0; $i -lt $nodes.Count; ++$i) {
$node = $nodes.Item($i)
If ($node -ne $null) {
$myArray += $node
}
}
Return ,$myArray
}
Function ShowContext{
Param($ctx)
" at " + $ctx
}
Function CompareNode{
Param($o1, $o2, $ctx)
Try {
Switch ($o1.GetType().Name) {
"XmlDocument" {
CompareXml $o1.ChildNodes $o2.ChildNodes
}
"XmlChildNodes" {
$olist1 = XmlChildNodes2List $o1 | Sort
$olist2 = XmlChildNodes2List $o2 | Sort
If ($olist1.Count -ne $olist2.Count) {
$msg = "Unequal child node count " + ($olist1 -join ",") + " and " + ($olist2 -join ",") + (ShowContext $ctx)
throw $msg
} Else {
$list = Zip $olist1 $olist2
$value = $true
foreach ($item in $list) {
if ($value -eq $true) {
$value = CompareXml $item[0] $item[1] $ctx
}
}
$value
}
}
"XmlElement" {
If ($o1.LocalName -eq $o2.LocalName) {
If ($o1.LocalName -eq "uninterestingElement" -or $o1.LocalName -eq "uninterestingElement2") {
$true
} Else {
CompareXML $o1.ChildNodes $o2.ChildNodes ($ctx + "/" + $o1.LocalName)
}
} Else {
throw ("Element " + $o1.LocalName + " != " + $o2.LocalName + (ShowContext $ctx))
}
}
"XmlDeclaration" {
$true
}
"XmlText" {
$result = $o1.InnerText.Replace("`r`n","`n")
$expect = $o2.InnerText.Replace("`r`n","`n")
# TODO: Hack to remove timezone from expected dates in format 2005-09-01+02:00, the webservice side of the
# reply to xml-conversion looses them
If ($expect -match "^(\d{4}-\d\d-\d\d)\+\d\d:\d\d$") {
$expect = $Matches[1]
}
If ($result -eq $expect) {
$true
} Else {
throw ($o1.InnerText + " is not equal to " + $o2.InnerText + (ShowContext $ctx))
}
}
Default {
throw ("What to do with node " + $o1.GetType().Name + (ShowContext $ctx))
}
}
} Catch [Exception] {
throw $_
}
}
Function CompareXML{
Param($o1, $o2, $ctx)
If ($o1 -eq $null -and $o2 -eq $null) {
$true
} ElseIf ($o1 -eq $null -or $o2 -eq $null) {
throw ("Response or expected is null")
} ElseIf ($o1.GetType() -eq $o2.GetType()) {
CompareNode $o1 $o2 $ctx
} Else {
throw ($o1.GetType().Name + " is not " + $o2.GetType().Name + (ShowContext $ctx))
}
}
This can then be run on two XML's like this:
CompareXML $result $expected ""