kalebpederson / nunit.dependencyinjection

Add support to NUnit for constructor injection using an inversion control container, such as Unity or Autofac.
8 stars 2 forks source link

Using nunit.dependencyinjection with [SetUpFixture] #14

Open rebekevsha opened 2 years ago

rebekevsha commented 2 years ago

I need to use injection in my SetUpClass with [SetUpFixture] attribute to run SomeMethod with [OneTimeSetUp] attribute where i want use injection. Maby u can help.

kalebpederson commented 2 years ago

@rebekevsha, I don't understand what you're trying to accomplish. Can you elaborate?

rebekevsha commented 2 years ago

@kalebpederson Sorry, my english no so fine, but i try))

I need next:

For this case, NUnit have SetUpFixture attribute for class, and it say that it Once before all tests run method with [OneTimeSetUp] attribute and Once after all tests run method with [OneTimeTearDown] attribute. And I want use Your DI liberary to resolve my _kafkaConsumer in this class. Because when set [OneTimeSetUp] and [OneTimeTearDown] methods in my class with tests or in parent class, they called as many times as I have classes with tests.

Somethink like thiss:

01-02-2022 ⁄⁄⁄ 19-22-37

So, i think i need some custome attribute like [SetUpFixture] with all [SetUpFixture] functionality and additionally an injecting opportunity.

Can u help? Thanks!!!

rebekevsha commented 2 years ago

Maybe you give me some clue where from to start to implement. Maybe some technical nuance. Thanks!!!

kalebpederson commented 2 years ago

Maybe you give me some clue where from to start to implement. Maybe some technical nuance. Thanks!!!

You're pretty close. The [SetUpFixture] attribute needs to change to the [DependencyInjectingTestFixture] and then the constructor needs to become public:

  [DependencyInjectingTestFixture]
  public class KafkaTests
  {
    private readonly KafkaConsumer<TestType> _kafkaConsumer;

    public KafkaTests(KafkaConsumer<TestType> kafkaConsumer)
    {
        _kafkaConsumer = kafkaConsumer;
    }

    [OneTimeSetUp]
    public void OneTimeSetUp()
    {
        Task.Factory.StartNew(() => _kafkaConsumer.StartConsume(), ...);
    }

    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
        _kafkaConsumer.DisposeAsync().GetAwaiter().GetResult();
    }

    [Test]
    public void Test1()
    {
        // do something with _kafkaConsumer
    }

    [Test]
    public void Test2()
    {
        // do something with _kafkaConsumer
    }
  }
kalebpederson commented 2 years ago

@rebekevsha, my apologies. I didn't read what you posted carefully enough. I'll need to test that to confirm.

kalebpederson commented 2 years ago

@rebekevsha, I have confirmed that this can be implemented within NUnit.DependencyInjection and will plan on working on it as soon as opportunity allows.

rebekevsha commented 2 years ago

@kalebpederson There was no time to answer, since I'm from Ukraine and I think you know what's going on here. So I was a little busy.

Great!!!!! I'll wait!! Because this feature will help me make my tests more compact, flexible and faster.

Thank you!!!