microsoft / testfx

MSTest framework and adapter
MIT License
679 stars 246 forks source link

Question: Unloading of assemblies using MSTest.Sdk/3.x.x #3084

Open MarcZw opened 3 weeks ago

MarcZw commented 3 weeks ago

Question

Is there a way to unload assemblies using the MSTest.Sdk/3.x.x?

Problem

When registering MSTest via the extension point you need to provide an Assembly loaded in the default AssemblyLoadContext. However, after loading this Assembly the corresponding .dll file gets locked from any io-operation, which the program needs.

See the code below for the current way of loading assemblies

TestApplication

var builder = await TestApplication.CreateBuilderAsync([]);

builder.AddMSTest(() =>
{
    var assembly = Assembly.LoadFrom(_assemblyPath);
    return [assembly];
});

using var app = await builder.BuildAsync();
await app.RunAsync();
Evangelink commented 3 weeks ago

Would you mind providing more details about the use case you are trying to cover?

MarcZw commented 3 weeks ago

At the moment the program (Stryker.NET) does an initial test run of the unit tests to determine whether any tests are failing before mutating the source code. To run these unit tests, the mstest extension needs a reference to an assembly loaded in the default loading context.

After the initial test run has finished, Stryker mutates the source code and tries to inject it in the .dll of the already ran unit tests. Unfortunately this injecting will always fail, because the file is locked by loading the assembly into the default loading context.

MarcoRossignoli commented 3 weeks ago

After the initial test run has finished, Stryker mutates the source code and tries to inject it in the .dll of the already ran unit tests. Unfortunately this injecting will always fail, because the file is locked by loading the assembly into the default loading context.

Why don't shutdown the app, instrument(I suppose you're rewriting it on disk and you're now using a profiler) and restart it?