Serialization settings can be customized at three levels:
- Method: Will run the verification in the current test method.
- Class: Will run for all verifications in all test methods for a test class.
- Global: Will run for test methods on all tests.
toc
The default serialization settings are:
snippet: defaultSerialization
JsonTextWriter.QuoteChar is set to single quotes '
. The reason for this is that it makes approval files cleaner and easier to read and visualize/understand differences.
JsonTextWriter.QuoteName is set to false. The reason for this is that it makes approval files cleaner and easier to read and visualize/understand differences.
By default empty collections are ignored during verification.
To disable this behavior globally use:
snippet: DontIgnoreEmptyCollections
By default guids are sanitized during verification. This is done by finding each guid and taking a counter based that that specific guid. That counter is then used replace the guid values. This allows for repeatable tests when guid values are changing.
snippet: guid
Results in the following:
snippet: Tests.ShouldReUseGuid.verified.txt
To disable this behavior globally use:
snippet: DontScrubGuids
By default dates (DateTime
and DateTimeOffset
) are sanitized during verification. This is done by finding each date and taking a counter based that that specific date. That counter is then used replace the date values. This allows for repeatable tests when date values are changing.
snippet: Date
Results in the following:
snippet: Tests.ShouldReUseDatetime.verified.txt
To disable this behavior globally use:
snippet: DontScrubDateTimes
By default values of bool
and bool?
are ignored during verification. So properties that equate to 'false' will not be written,
To disable this behavior globally use:
snippet: DontIgnoreFalse
DateTime
, DateTimeOffset
, Guid
, bool
, and empty collection behavior can also be controlled at the verification level:
snippet: ChangeDefaultsPerVerification
To change the serialization settings for all verifications use Global.ApplyExtraSettings()
:
snippet: ExtraSettings
snippet: ScopedSerializer
Result:
snippet: Verify.Xunit.Tests/VerifyObjectSamples.ScopedSerializer.verified.txt
To ignore all members that match a certain type:
snippet: AddIgnoreType
Result:
snippet: Tests.IgnoreType.verified.txt
To ignore instances of a type based on delegate:
snippet: AddIgnoreInstance
Result:
snippet: Tests.AddIgnoreInstance.verified.txt
Members with an ObsoleteAttribute are ignored:
snippet: WithObsoleteProp
Result:
snippet: Tests.WithObsoleteProp.verified.txt
Obsolete members can be included using IncludeObsoletes
:
snippet: WithObsoletePropIncluded
Result:
snippet: Tests.WithObsoletePropIncluded.verified.txt
To ignore members of a certain type using an expression:
snippet: IgnoreMemberByExpression
Result:
snippet: Tests.IgnoreMemberByExpression.verified.txt
To ignore members of a certain type using type and name:
snippet: IgnoreMemberByName
Result:
snippet: Tests.IgnoreMemberByName.verified.txt
Members that throw exceptions can be excluded from serialization based on the exception type or properties.
By default members that throw NotImplementedException
or NotSupportedException
are ignored.
Note that this is global for all members on all types.
Ignore by exception type:
snippet: IgnoreMembersThatThrow
Result:
snippet: Tests.CustomExceptionProp.verified.txt
Ignore by exception type and expression:
snippet: IgnoreMembersThatThrowExpression
Result:
snippet: Tests.ExceptionMessageProp.verified.txt