solidtoken / SpecFlow.DependencyInjection

SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.
BSD 3-Clause "New" or "Revised" License
35 stars 11 forks source link

ITestOutputHelper unavailable when using SpecFlow.xUnit #75

Closed rangers-globecar closed 2 years ago

rangers-globecar commented 2 years ago

Hi, We are using SpecFlow.xUnit; is there anyway to add/capture the ITestOutputHelper, in the same way [Binding] are being automatically ?

mbhoek commented 2 years ago

Interesting question @robert-stratecglobal -- although it's not related to the Microsoft.DependencyInjection framework specifically, I can understand you want this xUnit feature to work.

I've created a test which should cover this issue and committed it in the https://github.com/solidtoken/SpecFlow.DependencyInjection/tree/feature/75-ITestOutputHelper branch. Let me know if this truly the issue you're talking about.

The challenge imho is to solve this without SpecFlow.DependencyInjection needing a dependency on xUnit specifically. I'll need some time to further investigate and think of a correct way to solve this.

bobbyangers commented 2 years ago

The ISpecflowOutputHelper could be an alternative that would work as well.

Isn't there a way to "pickup" what is in the BoDi ? That way, any future enhancement to the DI and any third party plugin would automatically insert as well...

bobbyangers commented 2 years ago
 public class TestService : ITestService {

     public TestService(ISpecflowOutputHelper output)    //or ITestOutputHelper
     {
               //This is what I would like
     }
}
mbhoek commented 2 years ago

Isn't there a way to "pickup" what is in the BoDi ? That way, any future enhancement to the DI and any third party plugin would automatically insert as well...

That would make sense @bobbyangers but the current implementation completely replaces BoDi. So either it needs to fall back to BoDi if a dependency cannot be found in Microsoft's IServiceCollection (seems hard and might break existing implementations), or we add the dependencies available in BoDi to the IServiceCollection explicitly (requires bit of extra setup but should not break existing implementations).

I'll investigate the latter.

rangers-globecar commented 2 years ago

@mbhoek

Check out Corvus.Testing

they have an elegant way of handling the problem.... by using "hooks".... it's possible to "inject" more stuff and even do 'automatically' do some stuff in the background....

304NotModified commented 2 years ago

https://github.com/AdCodicem/SpecFlowMicrosoftDependencyInjection

solved it with the AddDelegated (so no dependency on xUnit from the lib)

    public void ConfigureServices(IServiceCollection services)
    {
        services
            // ITestOutputHelper is automatically registered by Specflow when using xUnit runner
            .AddDelegated<ITestOutputHelper>() ///!< ------------------
            // Calculator can use ITestOutputHelper as it's registered above to delegate the resolution to the original container
            .AddTransient<ICalculator, Calculator>();
    }

Good logging is essential, so I also need the ITestOutputHelper

mbhoek commented 2 years ago

@304NotModified Do you mind if I copy/paste your AddDelegated solution into this project?

304NotModified commented 2 years ago

It isn't mine ;)

mbhoek commented 2 years ago

Right, sorry. @AdCodicem was involved early on but created their own plugin instead, apologies for the mix-up. I'll see what I can do!

AdCodicem commented 2 years ago

@mbhoek You can do copy anything you need from my repo, I think it's better to have only one project to maintain, and as I started a company of my own, I have less time to spend on my project.

If it's ok for you I'll add a message to redirect the users to your project :slightly_smiling_face:

mbhoek commented 2 years ago

@AdCodicem Congratulations on starting your own company! I'm happy to keep maintaining this project, I think we've had a couple of great contributions over time. I do however struggle with available time too. I'll take some time this weekend to look at your code and @shlomiassaf 's PR #74 and see how we can make this the best MDI plugin available.

Of course I will keep on crediting all contributors. Thanks again!

mbhoek commented 2 years ago

Guess this plugin is not the only one struggling with this issue: https://github.com/SpecFlowOSS/SpecFlow/issues/2550

Also want to track this one: https://github.com/SpecFlowOSS/SpecFlow/issues/2353

AdCodicem commented 2 years ago

Thank you! Don't hesitate to ask me if you need any help 🙂

mbhoek commented 2 years ago

@AdCodicem I'm trying to figure out whether I want to copy your IServiceConfigurator pattern or just stick to the static implementation currently in this plugin. Can you explain a bit about your thinking behind it?

AdCodicem commented 2 years ago

@mbhoek the IServiceConfigurator purpose is to enable services registration in several configuration classes, at the time I needed to separate some configuration for readability.

You can see in this part of the code where the plugin search for IServiceConfigurator implementations :

https://github.com/AdCodicem/SpecFlowMicrosoftDependencyInjection/blob/435c84e052208bd1b207e9fafcced2fc62e94f9b/AdCodicem.SpecFlow.MicrosoftDependencyInjection/DependencyInjectionPlugin.cs#L42-L48

Edit: Also, I wanted to manage the creation of the Service collection and the interface allowed me to restrict the way the developer implemented the ConfigurationServices method and so, less issues ensued

mbhoek commented 2 years ago

@robert-stratecglobal @bobbyangers ISpecFlowOutputHelper (and other SpecFlow infrastructure) is available in release v3.9.3.