testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Make Exceptions Serializable

Open pwiens opened this issue 9 years ago • 8 comments

Description

Currently AssertFailedException, AssertInconclusiveException, InternalTestFailureException, and UnitTestException are not marked serializable.

This presents a problem when executing tests in an different AppDomain. Since the exceptions are not serializable the runtime can't move them between app domains and throws SerializationException.

AB#1575813

pwiens avatar Mar 15 '17 17:03 pwiens

Thanks for that feedback @arkasha. AppDomain's are only specific to the Desktop platform however and the core test framework as such is platform agnostic. This seems to be a candidate for extensibility however - we could make the piece that handles assertion failures extensible. So the users would be free to choose how to handle failures - it could be log to a file/stream or throw a custom(serializable) exception etc. Drawing a very rough sketch it could look like below:

Assert.SetupHandleFailures(() => throw new SerializableAssertFailure(message))

And all assert failures would now start throwing exceptions of this type.

your thoughts?

AbhitejJohn avatar Mar 16 '17 06:03 AbhitejJohn

That could be useful. What's the drawback to marking the actual exception classes serializable though? The AppDomain scenario is just an example. There could be others. The reason I brought this up in the first place is that we had these unit tests working fine under the legacy mstest framework and after upgrading to v2 they broke. If you look at the documentation for the previous version you'll see that the exceptions are serializable. https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assertfailedexception.aspx

[SerializableAttribute]
public class AssertFailedException : UnitTestAssertException

pwiens avatar Mar 16 '17 15:03 pwiens

That's true. This was serializable in the old framework. However the MSTest V2 Core Framework is actually a PCL that is designed to work seamlessly across all platforms. Serializable/AppDomains however are only specific to the Desktop platform and would end up fragmenting the design if added in Core.

AbhitejJohn avatar Mar 17 '17 06:03 AbhitejJohn

@AbhitejJohn , @pvlakshm : Any news about this problem? I must migrate a lot of existing test projects to the new MsTest.TestFramework, but many of them use AppDomains (to be able to unload assemblies). If AssertFailedException etc are not serializable, the output of a failed test is just "AssertFailedException in assembly 'Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable", instead of the real failure reason.

PablitoPaul avatar Dec 04 '18 14:12 PablitoPaul

I've since been working on other projects. @cltshivash and @jayaranigarg would be good contacts to help get traction on this.

AbhitejJohn avatar Jun 27 '19 21:06 AbhitejJohn

@Haplois The effort is minimal so that's probably still worth implementing it even if we know that app domains are no longer a thing for .net core.

Evangelink avatar Jul 12 '22 17:07 Evangelink

@Evangelink I agree.

Haplois avatar Jul 12 '22 17:07 Haplois

We need a bit of investigation but maybe we will have to wait for the v3 as SerializableAttribute isn't available in portable class libs.

Evangelink avatar Jul 12 '22 18:07 Evangelink