Snapshooter ist ein Snapshot -Test -Tool für .NET Core und .NET Framework
Snapshooter ist ein flexibles Snapshot -Test -Tool, um die Ergebnisvalidierung in Ihren Unit -Tests in .NET zu vereinfachen. Es basiert auf der Idee des Scherz -Schnappschuss -Tests.
Um detailliertere Informationen zu Snapshooter zu erhalten, besuchen Sie die Snapshooter -Dokumente
Installieren Sie zu Beginn das Snapshooter Xunit oder Nunit Nuget -Paket:
dotnet add package Snapshooter.Xunitdotnet add package Snapshooter.NUnitdotnet add package Snapshooter.MSTestFangen an
Befolgen Sie die folgenden Schritte, um Ihre Testergebnisse mit Schnappschüssen in Ihren Einheitstests zu behaupten:
Fügen Sie eine Snapshot Assert -Anweisung Snapshot.Match(yourResultObject); in Ihren Einheitstest.
Beispiel:
/// <summary>
/// Tests if the new created person is valid.
/// </summary>
[ Fact ]
public void CreatePersonSnapshotTest ( )
{
// arrange
var serviceClient = new ServiceClient ( ) ;
// act
TestPerson person = serviceClient . CreatePerson (
Guid . Parse ( "2292F21C-8501-4771-A070-C79C7C7EF451" ) , "David" , "Mustermann" ) ;
// assert
Snapshot . Match ( person ) ;
} Die Anweisung Snapshot.Match(person) erstellt einen neuen Schnappschuss Ihres Ergebnisobjekts und speichert es im Ordner __snapshots__ . Der Ordner __snapshots__ befindet sich immer neben Ihrer ausgeführten Unit -Testdatei.
Snapshot -Name: <UnitTestClassName>.<TestMethodName>.snap
Überprüfen Sie Ihre neue Snapshot -Datei __snapshots__/<UnitTestClassName>.<TestMethodName>.snap .
Jetzt erstellt die Anweisung Snapshot.Match(person) erneut einen Snapshot Ihres Testergebnisses und vergleichen sie mit Ihrem überprüften Snapshot im Ordner __snapshots__ . Der Ordner __snapshots__ befindet sich immer neben Ihrer ausgeführten Unit -Testdatei.
Wenn sich Ihr Ergebnisobjekt geändert hat und der vorhandene Snapshot nicht mehr übereinstimmt, schlägt der Unit -Test fehl. Die Unit -Testfehlermeldung zeigt auf die genaue Nichtübereinstimmungsposition innerhalb des Snapshots.
Darüber hinaus wird im Snapshot -Ordner __snapshots__ ein Unterordner mit Namen __mismatch__ erstellt. In diesem Ordner finden Sie den tatsächlichen Schnappschuss, der mit dem vorhandenen Snapshot im Ordner __snapshots__ nicht übereinstimmt. Daher ist es möglich, die beiden Schnappschüsse mit einem Textvergleichswerkzeug zu vergleichen.
Wenn der Snapshot im nicht anpassenden Ordner __mismatch__ korrekt ist, bewegen Sie ihn einfach in den übergeordneten __snapshots__ -Ordner (überschreiben Sie den vorhandenen).
Mehr lesen
Die Standard -Übereinstimmungssyntax für Snapshots lautet:
Snapshot . Match ( person ) ;Wir könnten jedoch auch die fließende Syntax verwenden:
person . MatchSnapshot ( ) ;Oder wir können die Syntax von Fluentassertion verwenden:
person . Should ( ) . MatchSnapshot ( ) ;Für Nunit werden wir die assert.that syntax (in Kürze) unterstützen:
Assert . That ( person , Match . Snapshot ( ) ) ; Wenn einige Felder in Ihrem Snapshot während der Snapshot -Behauptung ignoriert werden müssen, können die folgenden Ignorierungsoptionen verwendet werden:
[ Fact ]
public void CreatePersonSnapshot_IgnoreId ( )
{
// arrange
var serviceClient = new ServiceClient ( ) ;
// act
TestPerson person = serviceClient . CreatePerson ( "Hans" , "Muster" ) ;
// assert
Snapshot . Match < Person > ( testPerson , matchOptions => matchOptions . IgnoreField ( "Size" ) ) ;
}Die zu ignorierenden Felder werden über JSONPADE gefunden, daher sind Sie sehr flexibel und Sie können auch Felder von untergeordneten Objekten oder Arrays ignorieren.
Beispiele ignorieren:
// Ignores the field 'StreetNumber' of the child node 'Address' of the person
Snapshot . Match < Person > ( person , matchOptions => matchOptions . IgnoreField ( "Address.StreetNumber" ) ) ;
// Ignores the field 'Name' of the child node 'Country' of the child node 'Address' of the person
Snapshot . Match < Person > ( person , matchOptions => matchOptions . IgnoreField ( "Address.Country.Name" ) ) ;
// Ignores the field 'Id' of the first person in the 'Relatives' array of the person
Snapshot . Match < Person > ( person , matchOptions => matchOptions . IgnoreField ( "Relatives[0].Id" ) ) ;
// Ignores the field 'Name' of all 'Children' nodes of the person
Snapshot . Match < Person > ( person , matchOptions => matchOptions . IgnoreField ( "Children[*].Name" ) ) ;
// Ignores all fields with name 'Id'
Snapshot . Match < Person > ( person , matchOptions => matchOptions . IgnoreField ( "**.Id" ) ) ; Wenn wir alle Felder mit einem bestimmten Namen ignorieren wollen, haben wir zwei Optionen:
Option 1: Verwenden Sie die Option "Match Ignore" Option 'ignoreAllfields ()' und fügen Sie den Namen hinzu.
// Ignores all fields with name 'Id'
Snapshot . Match < Person > ( person , matchOptions => matchOptions . IgnoreAllFields ( "Id" ) ) ;Option 2: Verwenden Sie die Standard -Übereinstimmung Option 'Ignorefields (**.)' Mit der folgenden JSONPAD -Syntax **.
// Ignores all fields with name 'Id'
Snapshot . Match < Person > ( person , matchOptions => matchOptions . IgnoreFields ( "**.Id" ) ) ;Wenn einige Felder unserer Snapshot zu groß sind, zum Beispiel ein binäres Feld mit vielen Daten, können wir die Hashfield -Option verwenden. Die Hashfield -Option erstellt einen Hash des Feldwerts und daher jedes Mal, wenn der Hash nur verglichen wird. Wenn sich der Feldwert ändert, schlägt das Snapshot -Match fehl.
[ Fact ]
public void ImageSnapshot_HashImageBinary ( )
{
// arrange
var serviceClient = new ServiceClient ( ) ;
// act
TestImage image = serviceClient . CreateMonaLisaImage ( ) ;
// assert
Snapshot . Match ( image , matchOptions => matchOptions . HashField ( "Data" ) ) ;
}Beispiel für Schnappschuss mit Hash
{
"Id" : 3450987 ,
"OwnerId" : " 0680faef-6e89-4d52-bad8-291053c66696 " ,
"Name" : " Mona Lisa " ,
"CreationDate" : " 2020-11-10T21:23:09.036+01:00 " ,
"Price" : 951868484.345 ,
"Data" : " m+sQR9KG9WpgYoQiRASPkt9FLJOLsjK86UuiXKVRzas= "
}Das Feld (en) zu Hash kann über JSONPADE oder über den Feldnamen gefunden werden.
Hash -Feldbeispiele:
// Hash the field 'Data' of the child node 'Thumbnail' of the person
Snapshot . Match < Person > ( person , matchOptions => matchOptions . HashField ( "Thumbnail.Data" ) ) ;
// Hash the field 'Data' of the first thumbnail in the 'Thumbnails' array of the image
Snapshot . Match < Person > ( person , matchOptions => matchOptions . HashField ( "Thumbnails[0].Data" ) ) ;
// Ignores the field 'Data' of all 'Thumbnails' nodes of the image
Snapshot . Match < Person > ( person , matchOptions => matchOptions . HashField ( "Thumbnails[*].Data" ) ) ;
// Ignores all fields with name 'Data'
Snapshot . Match < Person > ( person , matchOptions => matchOptions . HashField ( "**.Data" ) ) ;Manchmal gibt es Felder in einem Schnappschuss, die Sie separat gegen einen anderen Wert behaupten möchten.
Zum Beispiel ist das ID -Feld einer "Person" immer neu in einem Dienst generiert. Daher erhalten Sie im Test immer eine Person mit einer neuen ID (GUID). Wenn Sie nun überprüfen möchten, dass die ID keine leere Richtlinie ist, kann die Option Assert verwendet werden.
/// <summary>
/// Tests if the new created person is valid and the person id is not empty.
/// </summary>
[ Fact ]
public void CreatePersonSnapshot_AssertId ( )
{
// arrange
var serviceClient = new ServiceClient ( ) ;
// act
TestPerson person = serviceClient . CreatePerson ( "Hans" , "Muster" ) ; // --> id is created within the service
// assert
Snapshot . Match < Person > ( testPerson , matchOption => matchOption . Assert (
fieldOption => Assert . NotEqual ( Guid . Empty , fieldOption . Field < Guid > ( "Id" ) ) ) ) ;
}Die zu behaupten Felder werden über JSONPath gefunden, daher sind Sie sehr flexibel und Sie können auch Felder von untergeordneten Objekten oder Arrays ignorieren.
Beispiele durchsetzen:
// Assert the field 'Street' of the 'Address' of the person
Snapshot . Match < Person > ( person , matchOption => matchOption . Assert (
fieldOption => Assert . Equal ( 15 , fieldOption . Field < int > ( "Address.StreetNumber" ) ) ) ) ;
// Asserts the field 'Code' of the field 'Country' of the 'Address' of the person
Snapshot . Match < Person > ( person , matchOption => matchOption . Assert (
fieldOption => Assert . Equal ( "De" , fieldOption . Field < CountryCode > ( "Address.Country.Code" ) ) ) ) ;
// Asserts the fist 'Id' field of the 'Relatives' array of the person
Snapshot . Match < Person > ( person , > matchOption . Assert (
fieldOption => Assert . NotNull ( fieldOption . Field < string > ( "Relatives[0].Id" ) ) ) ) ;
// Asserts every 'Id' field of all the 'Relatives' of the person
Snapshot . Match < Person > ( person , > matchOption . Assert (
fieldOption => Assert . NotNull ( fieldOption . Fields < string > ( "Relatives[*].Id" ) ) ) ) ;
// Asserts 'Relatives' array is not empty
Snapshot . Match < Person > ( person , > matchOption . Assert (
fieldOption => Assert . NotNull ( fieldOption . Fields < TestPerson > ( "Relatives[*]" ) ) ) ) ;Der Snapshooter -Assert -Funktionalität ist nicht auf Xunit- oder Nunit -Behauptungen beschränkt, sondern kann auch fließende Behauptungen oder ein anderes Assert -Tool verwendet werden.
Alle Feldprüfungen für Ignorier-, ISType oder Assert können verkettet werden.
[ Fact ]
public void Match_ConcatenateFieldChecksTest_SuccessfulMatch ( )
{
// arrange
var serviceClient = new ServiceClient ( ) ;
// act
TestPerson person = serviceClient . CreatePerson ( "Hans" , "Muster" ) ;
// act & assert
Snapshot . Match < TestPerson > ( testPerson , matchOption => matchOption
. Assert ( option => Assert . NotEqual ( Guid . Empty , option . Field < Guid > ( "Id" ) ) )
. IgnoreField < DateTime > ( "CreationDate" )
. Assert ( option => Assert . Equal ( - 58 , option . Field < int > ( "Address.StreetNumber" ) ) )
. Assert ( option => testChild . Should ( ) . BeEquivalentTo ( option . Field < TestChild > ( "Children[3]" ) ) )
. IgnoreField < TestCountry > ( "Address.Country" )
. Assert ( option => Assert . Null ( option . Field < TestCountry > ( "Relatives[0].Address.Plz" ) ) ) ) ;
} Wenn Sie Snapshooter-Tests in einem CI-Gebäude ausführen, sollten Sie sicherstellen, dass ein Schnappschüsse korrekt überprüft wird, da sonst Tests ohne Schnappschuss nur den anfänglichen Schnappschuss erstellen und grün werden.
Um Tests zu fehlenden, die auf Ihrem CI-Build ohne Schnappschuss liegen, können Sie das Verhalten von Snapshooter in Strict-Modus festlegen, indem Sie die Umgebungsvariablen- SNAPSHOOTER_STRICT_MODE on oder true einstellen.
Dieses Projekt hat den vom Mitwirkenden Covenant definierten Verhaltenskodex übernommen, um das erwartete Verhalten in unserer Community zu klären. Weitere Informationen finden Sie im Verhaltenskodex des Schweizer Lebens.