Daily Usage Issues

Ignore test projects based on target frameworks

Started by distilled on 2,506 views

I use NCrunch in solutions with .NET Standard projects. When I create test projects for them, I multi-target the runtimes that I expect my .NET Standard libraries to run on (so if Foo.csproj targets NETStandard2.0, FooTests.csproj probably targets net46, netcoreapp3.1, etc.). NCrunch treats each target runtime as separate projects and runs them independently - which is the behavior that I would expect. The downside of this is that it multiplies the number of tests that NCrunch monitors and processes.

I like the way that NCrunch handles this situation by default and I wouldn't want that behavior to go away. However, there are many times when I'd like to be able to tell NCrunch to only automatically run tests for one specific runtime, and ignore the other runtimes unless I manually run them. For example, assuming I have FooTests.csproj with <TargetFrameworks>net48;netcoreapp2.2;netcoreapp3.1</TargetFrameworks>:

FooTests.csproj (net48): automatically run any of these that are impacted by my changes
FooTests.csproj (netcoreapp2.2): don't automatically run these even if they're impacted. I'll run them manually if or when I need to
FooTests.csproj (netcoreapp3.0): same as above, only run manually

Is there a way to configure NCrunch that accomplishes this?
Hi, thanks for posting.

The easiest way I can think of doing this is to introduce assembly-level category attributes in your test project which are controlled by framework-specific compile switches.

For example:

#if NETCOREAPP2_2
[assembly: NCrunch.Framework.Category("NetCore2_2")]
#endif

#if NETCOREAPP3_0
[assembly: NCrunch.Framework.Category("NetCore3_0")]
#endif

#if NET48
[assembly: NCrunch.Framework.Category("Net48")]
#endif

In this way, every test in the assembly will have a category assigned to it that is tied to the platform it targets.

You can then set the Tests to execute automatically setting with consideration to the above categories, ideally doing so with some custom engine modes. This way you can set the engine to run tests for specific platforms depending on your selected engine mode.
That sounds reasonable, I'll take that approach. Thanks!

Post a reply

Log in to reply.