diff --git a/src/Adapter/MSTest.Engine/Assertions/AssertFailedException.cs b/src/Adapter/MSTest.Engine/Assertions/AssertFailedException.cs index 13efec5f35..cad313aaad 100644 --- a/src/Adapter/MSTest.Engine/Assertions/AssertFailedException.cs +++ b/src/Adapter/MSTest.Engine/Assertions/AssertFailedException.cs @@ -82,6 +82,9 @@ public AssertFailedException(string message, Exception ex) { } +#if NET8_0_OR_GREATER + [Obsolete(DiagnosticId = "SYSLIB0051")] +#endif private AssertFailedException(SerializationInfo serializationInfo, StreamingContext streamingContext) { } diff --git a/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs b/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs index 5c75c15abe..b2c5e7cdd6 100644 --- a/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs +++ b/src/TestFramework/TestFramework/Exceptions/AssertFailedException.cs @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.ComponentModel; +using System.Runtime.Serialization; + namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// @@ -35,4 +38,18 @@ public AssertFailedException() : base() { } + + /// + /// Initializes a new instance of the class. + /// + /// Serialization info. + /// Streaming context. +#if NET8_0_OR_GREATER + [Obsolete(DiagnosticId = "SYSLIB0051")] +#endif + [EditorBrowsable(EditorBrowsableState.Never)] + protected AssertFailedException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } diff --git a/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs b/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs index 638eb2658b..a0ab122863 100644 --- a/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs +++ b/src/TestFramework/TestFramework/Exceptions/AssertInconclusiveException.cs @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.ComponentModel; +using System.Runtime.Serialization; + namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// @@ -35,4 +38,18 @@ public AssertInconclusiveException() : base() { } + + /// + /// Initializes a new instance of the class. + /// + /// Serialization info. + /// Streaming context. +#if NET8_0_OR_GREATER + [Obsolete(DiagnosticId = "SYSLIB0051")] +#endif + [EditorBrowsable(EditorBrowsableState.Never)] + protected AssertInconclusiveException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } diff --git a/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs b/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs index 4a668b3a09..41e1d43342 100644 --- a/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs +++ b/src/TestFramework/TestFramework/Exceptions/InternalTestFailureException.cs @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.ComponentModel; +using System.Runtime.Serialization; + namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// @@ -46,4 +49,18 @@ public InternalTestFailureException() : base() { } + + /// + /// Initializes a new instance of the class. + /// + /// Serialization info. + /// Streaming context. +#if NET8_0_OR_GREATER + [Obsolete(DiagnosticId = "SYSLIB0051")] +#endif + [EditorBrowsable(EditorBrowsableState.Never)] + protected InternalTestFailureException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } diff --git a/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs b/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs index e5a23d79b4..d76cea2d92 100644 --- a/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs +++ b/src/TestFramework/TestFramework/Exceptions/UnitTestAssertException.cs @@ -1,6 +1,9 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.ComponentModel; +using System.Runtime.Serialization; + namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// @@ -34,4 +37,18 @@ protected UnitTestAssertException(string msg) : base(msg) { } + + /// + /// Initializes a new instance of the class. + /// + /// Serialization info. + /// Streaming context. +#if NET8_0_OR_GREATER + [Obsolete(DiagnosticId = "SYSLIB0051")] +#endif + [EditorBrowsable(EditorBrowsableState.Never)] + protected UnitTestAssertException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } } diff --git a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt index 7dc5c58110..c7380b5e95 100644 --- a/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt @@ -1 +1,5 @@ #nullable enable +Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException.AssertFailedException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void +Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException.AssertInconclusiveException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void +Microsoft.VisualStudio.TestTools.UnitTesting.InternalTestFailureException.InternalTestFailureException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void +Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestAssertException.UnitTestAssertException(System.Runtime.Serialization.SerializationInfo! info, System.Runtime.Serialization.StreamingContext context) -> void diff --git a/test/UnitTests/TestFramework.UnitTests/Exceptions/BinaryFormatterExceptionSerializationTests.cs b/test/UnitTests/TestFramework.UnitTests/Exceptions/BinaryFormatterExceptionSerializationTests.cs new file mode 100644 index 0000000000..7c3a083458 --- /dev/null +++ b/test/UnitTests/TestFramework.UnitTests/Exceptions/BinaryFormatterExceptionSerializationTests.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Runtime.Serialization.Formatters.Binary; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using TestFramework.ForTestingMSTest; + +namespace Microsoft.VisualStudio.TestPlatform.TestFramework.UnitTests.Attributes; + +[Obsolete] +public sealed class BinaryFormatterExceptionSerializationTests : TestContainer +{ + public void AssertFailedExceptionCanBeSerializedAndDeserialized() + => VerifySerialization(Assert.Fail); + + public void AssertInconclusiveExceptionCanBeSerializedAndDeserialized() + => VerifySerialization(Assert.Inconclusive); + + public void InternalTestFailureExceptionCanBeSerializedAndDeserialized() + => VerifySerialization(() => throw new InternalTestFailureException("Some internal error.")); + + private void VerifySerialization(Action actionThatThrows) + { + try + { + actionThatThrows(); + } + catch (Exception ex) + { + // Ensure the thrown exception can be serialized and deserialized by binary formatter to keep compatibility with it, + // even though it is obsoleted and removed in .NET. + var mem = new MemoryStream(); + new BinaryFormatter().Serialize(mem, ex); + mem.Position = 0; + new BinaryFormatter().Deserialize(mem); + + return; + } + + throw new InvalidOperationException($"The provided '{nameof(actionThatThrows)}' did not throw any exception."); + } +} diff --git a/test/UnitTests/TestFramework.UnitTests/TestFramework.UnitTests.csproj b/test/UnitTests/TestFramework.UnitTests/TestFramework.UnitTests.csproj index 9f46799831..2230c33d50 100644 --- a/test/UnitTests/TestFramework.UnitTests/TestFramework.UnitTests.csproj +++ b/test/UnitTests/TestFramework.UnitTests/TestFramework.UnitTests.csproj @@ -14,7 +14,7 @@ - +