Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Dependency tests with NetArchTest failing
gwholdom
#1 Posted : Monday, May 22, 2023 11:46:39 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/8/2020(UTC)
Posts: 3
Location: New Zealand

Hi,

I'm using NetArchTest nuget package for some design and dependency tests.

Unfortunately my dependency tests fail in NCrunch, but not with VS2022 testing.

e.g. This Test fails in NCrunch (but not VS2022):

Assert.True(
Types.InCurrentDomain()
.That().ResideInNamespace("MyNamespace.Domain")
.And().HaveName("TestingClass")
.Should()
.OnlyHaveDependenciesOn("System")
.GetResult()
.IsSuccessful);

The class is:

namespace MyNamespace.Domain;

public sealed class TestingClass
{
public Guid Id { get; set; }

public string RoleId { get; set; } = null!;

public Testing() { }
}

PS: I can run my design tests fine with NetArchTest and NCrunch by ensuring [Copy referenced assemblies to workspace] = True and [Copy referenced assemblies to workspace] = True.


Ta,
Grant.
Remco
#2 Posted : Tuesday, May 23, 2023 5:15:38 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi Grant,

Thanks for sharing this issue.

Looking at this particular framework and from what you've described, my expectation here is that the framework is taking responsibility for its own assembly loading, and as such would be fundamentally incompatible with NCrunch's optimisations.

It might be possible to force it to work with NCrunch by turning on the pre-load assembly references setting, but I suspect that enabling the 'Copy referenced assemblies to workspace' setting may be your only real option here.

Unfortunately with NCrunch there is a balancing act between compatibility and performance. With toolsets like this, we need to be less performant and more compatible.
gwholdom
#3 Posted : Tuesday, May 23, 2023 6:39:29 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/8/2020(UTC)
Posts: 3
Location: New Zealand

Thanks for the reply.

I have both [Pre-load all assembly references into test environment] and [Copy referenced assemblies to workspace] set to true but still no dice.

Bit of a bummer because NetArchTest looks useful.


Thanks for your help,
Grant.
Remco
#4 Posted : Tuesday, May 23, 2023 8:33:17 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
gwholdom;16661 wrote:

I have both [Pre-load all assembly references into test environment] and [Copy referenced assemblies to workspace] set to true but still no dice.


Sorry for the confusion ... I just want to confirm. Does NCrunch not work when the 'Copy referenced assemblies to workspace' setting is turned on? I.e. it's impossible to work at all with this library?
gwholdom
#5 Posted : Wednesday, May 24, 2023 8:20:34 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 1/8/2020(UTC)
Posts: 3
Location: New Zealand

Yes - I tried all the combos of [Pre-load all assembly references into test environment] and [Copy referenced assemblies to workspace] but without any luck.
Depending on the tests and properties set, I was getting a mixture of false positives or false negatives, but getting correct results from VS Unit Testing.
Remco
#6 Posted : Thursday, May 25, 2023 12:16:39 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Can you confirm the exceptions you're seeing? Are these assembly resolution issues? If so, is there a specific assembly they are having trouble resolving?
talbrecht
#7 Posted : Monday, June 12, 2023 12:14:40 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/10/2019(UTC)
Posts: 20
Location: Germany

Thanks: 8 times
Was thanked: 3 time(s) in 3 post(s)
Hi,

I've always thought that NCrunch enhances the code for testing with NCrunch somehow, e.g. for tracking code lines hit by tests. Therefore the added code must reference NCrunch and breaks your NetArchTest test code which looks at the referenced assemblies during test execution and allows dependencies to System.* only.

Maybe you can debug your test via NCrunch and look which namespaces/assemblies are added by NCrunch and allow them, too.* Just an idea.

Best regards,
Thomas

*) or you need to peek into the NCrunch enhanced DLL in NCrunch's workspace to see these additions. I've never needed to look into such assemblies but I saw the complex workspace structure with lots of built DLLs on our grid servers... and these are/were run by our grid nodes, aka must be enhanced somehow - at least if they are not get patched at runtime.
Remco
#8 Posted : Monday, June 12, 2023 12:33:16 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
talbrecht;16672 wrote:

I've always thought that NCrunch enhances the code for testing with NCrunch somehow, e.g. for tracking code lines hit by tests. Therefore the added code must reference NCrunch and breaks your NetArchTest test code which looks at the referenced assemblies during test execution and allows dependencies to System.* only.


Thanks for taking the time you share your thoughts on this. I can provide some extra details about NCrunch's instrumentation in the hope this might help both you and others.

Just after NCrunch builds your assembly in its workspace using MSBuild, it will load the assembly into memory in binary form and will manipulate it, adding extra instructions to each method (instrumentation) to allow it to track code coverage, as well as a few other things.

You can examine this instrumentation using a disassembler like ILDASM, ILSpy, dnSpy, etc. You can find the instrumented assembly by right clicking on the project in the NCrunch Tests window, then choose Advanced->Browse to workspace.

The instrumentation consists of a series of calls to nCrunch.TestRuntime.dll. This is a relatively lightweight assembly and is the only assembly referenced by NCrunch's instrumentation. In turn, this assembly references nothing else outside of system libraries.

NCrunch normally installs this assembly into the GAC when you run the installer, but since .NET Core (and newer versions of .NET), the GAC is not in the runtime's search path. To compensate for this, NCrunch will copy nCrunch.TestRuntime.dll (or nCrunch.TestRuntime.DotNetCore.dll for .NET Core and .NET5+) into the build output directory of your test project. As such, it's collocated with your assembly and usually should resolve without problems.
1 user thanked Remco for this useful post.
talbrecht on 6/19/2023(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.068 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download