testfx
testfx copied to clipboard
STA extension for MSTEST adapter does not work on .Net Core
Description
We are porting the Windows Community Toolkit's Win32 project from .NET framework to multitarget .NET framework and core, and we are having issues with our tests for the Windows Forms project. We require this tests to be run from a STA Thread, and the current recommended solution is to use https://www.nuget.org/packages/MSTest.STAExtensions (https://github.com/saikrishnav/testfxSTAext). That package is not maintained and they currently only support .NET full framework, so there is no way to use the package. I copied the source and tried compiling it myself on our test project (.net core), but the code still throws an exception because that solution does not work if your TestInitialize needs to run on STA, which is our case. https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/pull/7#issuecomment-454189440 This will be more and more common with .NET Core 3.0 and WinForms tests, so there needs to be a built in solution for this.
Steps to reproduce
Clone https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/tree/islandsnetcore30, build the solution and try to run the tests on "Tests/UnitTests.WebView.WinForms"
Expected behavior
I should be able to select which thread compartment my test initialize run, even for the TestInitialize section.
Actual behavior
Can't select thread compartment.
Environment
Only makes sense on Windows. OS: Win 10 (Insider Fast latest build) Build version of vstest.console: 16.0.0-preview-20181128-01 Package version of MSTest framework: 1.4.0 Package version of MSTest adapter: 1.4.0
Any news on this ? recently hit this up trying to port MSTest project to .net core 3
@azchohfi Did you ever manage to work around this? I'm hitting the same issue with the WPF Xaml Behaviors project as well. Initialize/Cleanup fails
@azchohfi Here's what I did to get around it: Just added an attribute that automatically gets picked instead, since the namespace match the unit tests: https://github.com/dotMorten/XamlBehaviorsWpf/blob/b289e419a832f64e4e4277715cb7eb75e2c41721/Test/UnitTests/STATestMethodAttribute.cs
That's how I fixed it: https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/commit/88e2a236885501c4c1e39dfa91ad4d75e5b0434c
@azchohfi Ugh that's a lot of changes. I'd suggest considering my approach as you don't have to touch any of the test code, so it's easier to maintain in the long run
The simple Attribute-Workaround may work for "99,9%(TM)" of cases. Our company now needs to migrate applications from net471 to net6. There are IntegrationTests in place, that check Forms/UserControls for design consistency. To do that it will create an instance of each Form, and read it's containing controls and properties. Just using reflection, or Roslyn Analyzers is not enough, as we need to ensure it can be instantiated, and that everything is completely initialized. Since some Controls/Properties/Features are not Design-Time, and by far harder to read/calculate with Roslyn. Also this made us aware about the new Application Default Font in net6, which influences DPI/Scaling - which may not be the case with an analyzer. This now breaks apart, because some Controls need an STA Thread. But with this Attribute, we get a bunch of other Exceptions. Like:
System.Runtime.Serialization.SerializationException: Type 'System.Text.UTF8Encoding' in Assembly 'System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' is not marked as serializable.
We cannot "split" the test, because some Forms would throw both (STA, and SerializationException).
We are also hesitant to switch Testing Frameworks, because we implemented some workarounds already for MsTest under Net6 for some of our classes (eg wanting to read testhost.config), and that would require "everything" be also aware of the new Framework/Runner
We are not maintaining the project quoted in the description and we will work on providing our own implementation of "STA" test methodsin #1674