pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.11k stars 473 forks source link

Feedback for datetimes unhelpful #631

Closed oylenshpeegul closed 6 years ago

oylenshpeegul commented 8 years ago

Datetimes can differ in milliseconds, but Pester doesn't reflect that. If we write

#!/usr/bin/powershell

Describe "Datetime tests" {
    Context "'2016-10-29 08:54:47' is '2016-10-29 08:54:47.001'" {
        [datetime]$dt1 = '2016-10-29 08:54:47'
        [datetime]$dt2 = '2016-10-29 08:54:47.001'
        It "Same?" {
            $dt1 | Should Be $dt2
        }
    }
}

then running it gives

$ ./Datetime.Tests.ps1 
Describing Datetime tests
   Context '2016-10-29 08:54:47' is '2016-10-29 08:54:47.001'
    [-] Same? 1.34s
      Expected: {10/29/2016 08:54:47}
      But was:  {10/29/2016 08:54:47}
      at line: 8 in /home/tim/posh/Datetime.Tests.ps1
      8:            $dt1 | Should Be $dt2

Can we make Pester give more information in the "Expected" and "But was" lines?

dlwyatt commented 8 years ago

That's awkward; Pester just casts the object to a string, and DateTime's string formatting doesn't show the milliseconds.

ArieHein commented 8 years ago

the test was done on a Linux, might the datetime type return differently from .net core ?

oylenshpeegul commented 8 years ago

I think I have .NET Core

PS /home/tim/posh> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.9
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

While this may well be different from "full" .NET or other platforms, I don't think that's the issue here. As @dlwyatt said, it's just a display issue. Powershell knows the two values and can tell us the difference

PS /home/tim/posh> $dt1.CompareTo($dt2)
-1

it's just not showing us the difference with the default output.

PS /home/tim/posh> $dt1.ToString()
10/29/16 8:54:47 AM
PS /home/tim/posh> $dt2.ToString()
10/29/16 8:54:47 AM

We can ask it to show us, though.

PS /home/tim/posh> $dt1.ToString("o")
2016-10-29T08:54:47.0000000
PS /home/tim/posh> $dt2.ToString("o")
2016-10-29T08:54:47.0010000

I guess the question is, how do we get Pester to do this?

iRon7 commented 6 years ago

I bounced into the same "display" issue. Pester revealed a bug in my (Windows) PowerShell cmdlet, but it took me quiet a while to figure out what was going wrong due the concerned "unhelpful" message:

PS C:\> $dt1 = Get-Date
PS C:\> $dt2 = Get-Date $dt1.ToString()
PS C:\> $dt1 | Should -Be $dt2
Expected: {01/07/2018 19:11:31}
But was:  {01/07/2018 19:11:31}
At C:\Program Files\WindowsPowerShell\Modules\Pester\4.1.1\Functions\Assertions\Should.ps1:209 char:9
+         throw ( New-ShouldErrorRecord -Message $testResult.FailureMes ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidResult: (System.Collections.Hashtable:Hashtable) [], Exception
    + FullyQualifiedErrorId : PesterAssertionFailed

Note that in the Get-Date example, .ToString('o') also shows the timezone:

PS C:\> $dt1.ToString('o')
2018-01-07T19:11:31.8413555+01:00
PS C:\> $dt2.ToString('o')
2018-01-07T19:11:31.0000000

Which I think is correct to display as well, as the whole time (on a tick level) could show an equal result but could still throw a pester error if two objects differ in timezone.

nohwnd commented 6 years ago

Good idea, there are few other types that could use a better output, but for date time the 'o' format looks great.

# outputs
0001-01-01T01:00:00.0000001 # date time
0001-01-01T00:00:00.0000001Z # datetime converted to utc

0001-01-01T10:00:00.0000001+01:00 # datetimeoffset
0001-01-01T00:00:00.0000001+00:00 #datetimteoffset converted to utc
nohwnd commented 6 years ago

I have this piece of code that formats different types for my assert module. Weirdly the date time is not one of them, but it's easy to add it :) So we might use that formatting for Pester.

https://github.com/nohwnd/Assert/blob/master/Format/tst/Format.Tests.ps1#L111

nohwnd commented 6 years ago

@oylenshpeegul @iRon7 will be fixed soon by #996 to print

 [-] dates are expanded to include ticks 200ms
    Expected 0001-01-01T00:00:00.0000000, but got 2018-02-20T18:28:02.6424963+01:00.
    24:         (Get-Date) | Should -Be ([datetime]::MinValue)