1

I am using x-unit. We use test output. We have things in a base class. We currently inject ITestOutputHelper like this:

public class AnyTestClass : TestBase
{
   [Fact]
   public void SomeTest()
   {
   }

   public AnyTestClass( ITestOutputHelper output ) : base(output)
   {
   }
}

whereas I want the code to look like:

public class AnyTestClass : TestBase
{
   [Fact]
   public void SomeTest()
   {
   }
}

I very much hate having to put that delegating constructor in. It's always the same, it's work, it clutters up the code, it adds nothing for the reader, it's duplicated code... and it just feels very "wrong".

Is there any way of getting around it? I've been thinking x-unit has to exist in the stack, and one of those things in the stack has to have a reference to ITestOutputHelper, which I must be able to get out with reflection.... Anybody done it and can save me some time looking?

Basically - I'm prepared to do a ridiculous amount of extra work or extra compute to make our codebase a little cleaner - and this is a clear example of code that adds no value whatsoever to the reader.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Darren Oakey
  • 2,894
  • 3
  • 29
  • 55
  • Maybe in this case you can use property injection instead of constructor? – Quercus Sep 12 '22 at 06:31
  • does xunit support that? and can I put it in the base class in a protected variable? If yes to both - that's a perfect solution! – Darren Oakey Sep 12 '22 at 06:33
  • It seems that out-of-box xunit doesn't support property injection (actually there's a request to xunit team to add it). However, there's a project: https://github.com/pengweiqhca/Xunit.DependencyInjection which adds real DI to xunit. Same question here: https://stackoverflow.com/a/73141087/2109769 – Quercus Sep 12 '22 at 06:49

1 Answers1

0

Closest you'll get as a one liner thing is using XunitContext.Register (but unsurprisingly that alo relies on obtaining ITestOutputHelper via ctor injection)

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249