You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Hooks are ignored if the class in which they are defined lives in a different assembly than the tests themselves. #556
The example use case here would be a class library that sets up things for multiple test projects in a consistent way so that those test projects can reference this one library and run consistently across the board. MSTest, NUnit, and xUnit all support this scenario so, hopefully, this is indeed a bug, and not the intended behaviour.
Repro Steps:
create 2 projects; project A contains some tests, while project B contains a class with some hooks (e.g. before/after test)
make sure project A references project B, and make the class containing the tests inherit from the class in project B
put some breakpoints on the hooks and observe that they never get hit
Hi @thomhurst, thank you for getting back to me so quickly. After looking at your unit tests, I've done more digging and finally discovered what the problem was. I was using TUnit.Core in my library, and it turns out that I needed TUnit.Engine instead. After sorting this out, however, I'm now getting test discovery exceptions in my NUnit and xUnit projects, so at this point it's probably worth providing additional context around what I'm building so you can tell me if my potentially very specific scenario can be covered. Essentially I'm working on a library to unify unit testing across my entire organisation (probably around 300-ish developers and QA people who also write tests). This library, amongst other things, sets up the exact same before/after assembly/fixture/test hooks for MSTest, NUnit, xUnit, Reqnroll (SpecFlow successor), and now I'm trying to add TUnit to that list too, so that everybody across the organisation can get the exact same experience regardless of which unit testing framework their team might prefer, and I should probably note that it's not only about preference, but also about being able to retro-fit this library into pre-existing test projects.
So... to make a long story short, TUnit doesn't seem to play well with the others in this potentially very specific scenario.
... I start getting a bunch of errors on solution build, along the lines of ...
Unable to connect to testing platform runner process 'path\to\XXX.Core.Tests.NUnit.exe'.
or
Unable to connect to testing platform runner process 'path\to\XXX.Core.Tests.xUnit.exe'.
... where these binaries represent the multiple test projects that I'm using to test the library.
Any thoughts that you might have on this would be greatly appreciated. I would very much like to promote TUnit internally as the preferred framework of choice, but I can only do that if I can integrate it successfully.
Ah yes. I need to do some refactoring as I'd like to have it so that you only need to import the "Core" package for libraries. I'll raise an issue so I can hopefully sort it, but as you've realised for now you can just use the standard TUnit package.
I believe your error is due to mixing the old VSTest and the new Microsoft.Testing.Platform. They're essentially the old MS runner and the new MS runner, and I think you basically have to say which you want to use.
When you install TUnit, it implicitly sets some project properties for you so you don't have to do it manually. So I think what's happening is it's basically telling your project to run in the new Microsoft.Testing.Platform mode, but your other test frameworks don't currently support that, and hence you get that error.
You should be able to explicitly set these properties in your csproj to disable this, but you'll need to still enable it for TUnit.
The easiest way to get this running is probably by having an environment variable set for when you want to use TUnit, and then using property conditions in your csproj.
Indeed, your analysis of the problem is spot on, however the suggested solution doesn't seem to be doing the trick for me. I've even tried to specify the runners explicitly, e.g. ...
I also tried taking a different approach, by upgrading all the other projects to Microsoft.Testing.Platform and that seems to kind of solve the problem, but all the dependencies required for the upgrade are fairly unstable, so I don't think I can do that just yet.
On top of that, after the upgrade, I keep getting warnings left and right, e.g. The type 'SelfRegisteredExtensions' in '...\XXX.Core.Tests.TUnit\obj\Debug\net9.0\AutoRegisteredExtensions.cs' conflicts with the imported type 'SelfRegisteredExtensions' in 'XXX.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in '...\XXX.Core.Tests.TUnit\obj\Debug\net9.0\AutoRegisteredExtensions.cs'. or Method 'TestingPlatformEntryPoint.Main(string[])' will not be used as an entry point because a synchronous entry point 'AutoGeneratedEntryPoint.Main(string[])' was found. which can be resolved with <GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>.
I think that for the time being I'll wait until you manage to find some time for refactoring the bits that allow libraries to only require TUnit.Core and after that attempt an upgrade to Microsoft.Testing.Platform across the board again.
Many thanks for the help and the quick responses, and congratulations for making it on Nick Chapsas' vlog today. I think a lot of people are really excited about what you're building here and I can't wait to see what's coming next, and even though for this one project I'll put TUnit on the backburner for a little while until I can get everything to play nice together, TUnit is already my framework of choice and I will continue to try to break it in many interesting ways.
Thanks for the update @Xen0byte ! I'll try and work on sorting out the core package soon to hopefully unblock you. In regards to upgrading your other projects to the testing platform, this won't work. They will need new code changes to interact with the new testing protocol. NUnit and xUnit have been working on it, but I don't know if it'll work for your existing projects. I think you'll have to stick to vstest for now, but I'll try to unblock the core package problems still which I think should hopefully still sort your problems!
Hi @thomhurst, thank you so much! It would appear that I'm now getting the following exception on test discovery:
Process: path\to\XXX.Core.Tests.TUnit\bin\Debug\net9.0\XXX.Core.Tests.TUnit.exe, Exception: StreamJsonRpc.RemoteInvocationException: System.TypeLoadException: Could not load type 'System.Runtime.InteropServices.MemoryMarshal' from assembly 'System.Runtime, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
at <PrivateImplementationDetails>.InlineArrayAsSpan[TBuffer,TElement](TBuffer& buffer, Int32 length)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance[T](IServiceProvider provider, Object[] parameters)
at TUnit.Engine.Extensions.ServiceCollectionExtensions.<>c__DisplayClass1_0.<AddTestEngineServices>b__1(IServiceProvider x)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at TUnit.Engine.Framework.TUnitTestFramework..ctor(IExtension extension, IServiceProvider frameworkServiceProvider, ITestFrameworkCapabilities capabilities)
at TUnit.Engine.Extensions.TestApplicationBuilderExtensions.<>c__DisplayClass0_0.<AddTUnit>b__1(ITestFrameworkCapabilities capabilities, IServiceProvider serviceProvider)
at Microsoft.Testing.Platform.Hosts.TestHostBuilder.BuildTestFrameworkAsync(TestFrameworkBuilderData testFrameworkBuilderData) in /_/src/Platform/Microsoft.Testing.Platform/Hosts/TestHostBuilder.cs:line 647
at Microsoft.Testing.Platform.Hosts.ServerTestHost.ExecuteRequestAsync(RequestArgsBase args, String method, ServiceProvider perRequestServiceProvider) in /_/src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs:line 451
at Microsoft.Testing.Platform.Hosts.ServerTestHost.HandleRequestCoreAsync(RequestMessage message, RpcInvocationState rpcInvocationState) in /_/src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs:line 398
at Microsoft.Testing.Platform.Hosts.ServerTestHost.HandleRequestAsync(RequestMessage request, CancellationToken serverClosing) in /_/src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs:line 312
at StreamJsonRpc.JsonRpc.InvokeCoreAsync[TResult](RequestId id, String targetName, IReadOnlyList`1 arguments, IReadOnlyList`1 positionalArgumentDeclaredTypes, IReadOnlyDictionary`2 namedArgumentDeclaredTypes, CancellationToken cancellationToken, Boolean isParameterObject)
at Microsoft.VisualStudio.TestWindow.Internal.RemoteAgent.TestingPlatform.RpcClient.DiscoverTestsAsync(Guid runId, CancellationToken cancellationToken)
at Microsoft.VisualStudio.TestWindow.Internal.RemoteAgent.TestingPlatform.Clients.TestingPlatformClient.<>c__DisplayClass24_0.<<DiscoverTestsAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.VisualStudio.Threading.ThreadingTools.WithCancellationSlow[T](Task`1 task, CancellationToken cancellationToken)
at Microsoft.VisualStudio.TestWindow.Internal.RemoteAgent.TestingPlatform.Clients.TestingPlatformClient.ExecuteRpcCallAsync[T](Func`1 rpcFunction, CancellationToken cancellationToken)
I'm assuming this is probably specific to the .NET 9 SDK. I'll confirm this tomorrow morning and I will also put together a minimal solution that I can share, for testing purposes.
Indeed! I've disabled testing platform server mode and now I get this:
Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Could not find testhost
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting.DotnetTestHostManager.GetTestHostProcessStartInfo(IEnumerable`1 sources, IDictionary`2 environmentVariables, TestRunnerConnectionInfo connectionInfo) in /_/src/Microsoft.TestPlatform.TestHostProvider/Hosting/DotnetTestHostManager.cs:line 432
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable`1 sources, String runSettings) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs:line 226
at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.InitializeDiscovery(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler, Boolean skipDefaultAdapters) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs:line 151
Hmm ... interesting. I use the preview distribution as my main IDE, so maybe that's the cause of the problem. I'll install the stable distribution and give it a try and report back the outcome.
Hmm ... interesting. I use the preview distribution as my main IDE, so maybe that's the cause of the problem. I'll install the stable distribution and give it a try and report back the outcome.
I don't think the stable version has the option for the new testing platform yet. So this is odd. What .net SDK version are you on?
The funny thing is that I tried it on my home computer, on the latest preview version, and with testing platform server mode enabled, and it works fine ...
... so at this point, the code itself seems to be working fine, but I still need to find out where the problem is on my work machine. I will update that to the latest preview version too, and if that doesn't sort it out I will do a settings export/import too from my home machine, and in theory that should solve the problem.
These are the SDKs I've got installed on my home machine on which the solution works ...
... and I'll check my work machine tomorrow too.
Just knowing that it works is incredibly helpful, thank you so much for finding time for refactoring the code required to use just TUnit.Core in libraries! Having this confirmation, all I need to do is put on my detective hat and figure out what the difference is.
Hi @thomhurst, I've updated my Visual Studio to the latest preview version and imported my home machine settings, and everything is running fine now, so while I don't specifically know what the problem was, it's no longer there. Many thanks for all the work you've put into unblocking me!
Activity
thomhurst commentedon Sep 20, 2024
@Xen0byte Can you try on version
0.1.773?I'm successfully hitting breakpoints in the hook class:

I've added a test too to verify that the hooks were hit:
https://github.com/thomhurst/TUnit/blob/main/TUnit.TestProject/BasicTests_HooksFromLibrary.cs
thomhurst commentedon Sep 20, 2024
The above was Rider ^
And it's hitting is Visual Studio too:
Xen0byte commentedon Sep 20, 2024
Hi @thomhurst, thank you for getting back to me so quickly. After looking at your unit tests, I've done more digging and finally discovered what the problem was. I was using
TUnit.Corein my library, and it turns out that I neededTUnit.Engineinstead. After sorting this out, however, I'm now getting test discovery exceptions in my NUnit and xUnit projects, so at this point it's probably worth providing additional context around what I'm building so you can tell me if my potentially very specific scenario can be covered. Essentially I'm working on a library to unify unit testing across my entire organisation (probably around 300-ish developers and QA people who also write tests). This library, amongst other things, sets up the exact same before/after assembly/fixture/test hooks for MSTest, NUnit, xUnit, Reqnroll (SpecFlow successor), and now I'm trying to add TUnit to that list too, so that everybody across the organisation can get the exact same experience regardless of which unit testing framework their team might prefer, and I should probably note that it's not only about preference, but also about being able to retro-fit this library into pre-existing test projects.So... to make a long story short, TUnit doesn't seem to play well with the others in this potentially very specific scenario.
For instance ...
... works fine, but the TUnit hooks don't get hit, and when I fix this by replacing ...
... with ...
... I start getting a bunch of errors on solution build, along the lines of ...
Unable to connect to testing platform runner process 'path\to\XXX.Core.Tests.NUnit.exe'.or
Unable to connect to testing platform runner process 'path\to\XXX.Core.Tests.xUnit.exe'.... where these binaries represent the multiple test projects that I'm using to test the library.
Any thoughts that you might have on this would be greatly appreciated. I would very much like to promote TUnit internally as the preferred framework of choice, but I can only do that if I can integrate it successfully.
thomhurst commentedon Sep 20, 2024
Ah yes. I need to do some refactoring as I'd like to have it so that you only need to import the "Core" package for libraries. I'll raise an issue so I can hopefully sort it, but as you've realised for now you can just use the standard TUnit package.
I believe your error is due to mixing the old VSTest and the new Microsoft.Testing.Platform. They're essentially the old MS runner and the new MS runner, and I think you basically have to say which you want to use.
When you install TUnit, it implicitly sets some project properties for you so you don't have to do it manually. So I think what's happening is it's basically telling your project to run in the new Microsoft.Testing.Platform mode, but your other test frameworks don't currently support that, and hence you get that error.
You should be able to explicitly set these properties in your csproj to disable this, but you'll need to still enable it for TUnit.
The easiest way to get this running is probably by having an environment variable set for when you want to use TUnit, and then using property conditions in your csproj.So do something like this:
And then when you do want to use TUnit, just set the environment variable EnableTUnit=true
Or on the CLI pass in
--property:EnableTUnit=trueGive that a go if you can and let me know if it helps!
Edit: See comment below
thomhurst commentedon Sep 20, 2024
Oh actually looking at your screenshot, you have separate test projects for each framework. In the non-TUnit ones just add to their csprojs
Xen0byte commentedon Sep 20, 2024
Indeed, your analysis of the problem is spot on, however the suggested solution doesn't seem to be doing the trick for me. I've even tried to specify the runners explicitly, e.g. ...
... and, respectively ...
I also tried taking a different approach, by upgrading all the other projects to
Microsoft.Testing.Platformand that seems to kind of solve the problem, but all the dependencies required for the upgrade are fairly unstable, so I don't think I can do that just yet.On top of that, after the upgrade, I keep getting warnings left and right, e.g.
The type 'SelfRegisteredExtensions' in '...\XXX.Core.Tests.TUnit\obj\Debug\net9.0\AutoRegisteredExtensions.cs' conflicts with the imported type 'SelfRegisteredExtensions' in 'XXX.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in '...\XXX.Core.Tests.TUnit\obj\Debug\net9.0\AutoRegisteredExtensions.cs'.orMethod 'TestingPlatformEntryPoint.Main(string[])' will not be used as an entry point because a synchronous entry point 'AutoGeneratedEntryPoint.Main(string[])' was found.which can be resolved with<GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>.I think that for the time being I'll wait until you manage to find some time for refactoring the bits that allow libraries to only require
TUnit.Coreand after that attempt an upgrade toMicrosoft.Testing.Platformacross the board again.Many thanks for the help and the quick responses, and congratulations for making it on Nick Chapsas' vlog today. I think a lot of people are really excited about what you're building here and I can't wait to see what's coming next, and even though for this one project I'll put TUnit on the backburner for a little while until I can get everything to play nice together, TUnit is already my framework of choice and I will continue to try to break it in many interesting ways.
thomhurst commentedon Sep 20, 2024
Thanks for the update @Xen0byte ! I'll try and work on sorting out the core package soon to hopefully unblock you. In regards to upgrading your other projects to the testing platform, this won't work. They will need new code changes to interact with the new testing protocol. NUnit and xUnit have been working on it, but I don't know if it'll work for your existing projects. I think you'll have to stick to vstest for now, but I'll try to unblock the core package problems still which I think should hopefully still sort your problems!
thomhurst commentedon Sep 23, 2024
@Xen0byte I've just pushed out v0.1.807.
It should allow you to use TUnit.Core in your library projects and hopefully sort out your dependency issues.
Let me know if you still run into problems!
Xen0byte commentedon Sep 24, 2024
Hi @thomhurst, thank you so much! It would appear that I'm now getting the following exception on test discovery:
I'm assuming this is probably specific to the .NET 9 SDK. I'll confirm this tomorrow morning and I will also put together a minimal solution that I can share, for testing purposes.
thomhurst commentedon Sep 24, 2024
I take it you're using the preview version of VS and have enabled testing server mode in the preview settings?
Xen0byte commentedon Sep 25, 2024
Indeed! I've disabled testing platform server mode and now I get this:
EDIT: Example Solution Attached
thomhurst commentedon Sep 25, 2024
@Xen0byte that attached solution works fine for me. Maybe try checking your VS is up to date?
Xen0byte commentedon Sep 25, 2024
Hmm ... interesting. I use the preview distribution as my main IDE, so maybe that's the cause of the problem. I'll install the stable distribution and give it a try and report back the outcome.
thomhurst commentedon Sep 25, 2024
I don't think the stable version has the option for the new testing platform yet. So this is odd. What .net SDK version are you on?
Xen0byte commentedon Sep 25, 2024
The funny thing is that I tried it on my home computer, on the latest preview version, and with testing platform server mode enabled, and it works fine ...
... so at this point, the code itself seems to be working fine, but I still need to find out where the problem is on my work machine. I will update that to the latest preview version too, and if that doesn't sort it out I will do a settings export/import too from my home machine, and in theory that should solve the problem.
These are the SDKs I've got installed on my home machine on which the solution works ...
... and I'll check my work machine tomorrow too.
Just knowing that it works is incredibly helpful, thank you so much for finding time for refactoring the code required to use just TUnit.Core in libraries! Having this confirmation, all I need to do is put on my detective hat and figure out what the difference is.
Xen0byte commentedon Sep 26, 2024
Hi @thomhurst, I've updated my Visual Studio to the latest preview version and imported my home machine settings, and everything is running fine now, so while I don't specifically know what the problem was, it's no longer there. Many thanks for all the work you've put into unblocking me!
thomhurst commentedon Sep 26, 2024
Brilliant, glad you got it working! Might have just been VS update issues since this is still preview I'm sure there's some stuff to iron out still