---
title: "Discussion on \"Using Dependency Injection in Units and Integrations Tests\" | Hashnode"
description: "Discussion on \"Using Dependency Injection in Units and Integrations Tests\". Hello everyone, after a long period without publishing an article, today \nI'm back with this one. I will like to share with you a little thing about Unit Tests, on how to use dependency injection in our test.\nWe always use direct instantiation in our..."
url: "https://hashnode.com/posts/using-dependency-injection-in-units-and-integrations-tests/61d2ce66d335286be6cddce0"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Using Dependency Injection in Units and Integrations Tests](https://eliezerbwana.hashnode.dev/using-dependency-injection-in-units-and-integrations-tests?utm_source=hashnode&utm_medium=feed)

Hello everyone, after a long period without publishing an article, today I'm back with this one. I will like to share with you a little thing about Unit Tests, on how to use dependency injection in our test. We always use direct instantiation in our...

Jan 3, 2022 · 3 min read · eliezerbwana.hashnode.dev  
Read the article

-   [#csharp](https://hashnode.com/tag/csharp)
-   [#unit-testing](https://hashnode.com/tag/unit-testing)
-   [#net](https://hashnode.com/tag/net)

## Written by

[Eliezer Bwana](https://hashnode.com/@EliezerBwana)

@EliezerBwana

  
[View profile](https://hashnode.com/@EliezerBwana)

.Net full stack developer

## Responses 1

Markdown supported · ⌘↵ to post

[Александр Шмыков](https://hashnode.com/@ADSHMYKOV)

[Aug 14, 2024](https://hashnode.com/posts/using-dependency-injection-in-units-and-integrations-tests/61d2ce66d335286be6cddce0/comment/66bc6e895ecbcf805c5255c2)

Something wrong My test marked as attribute \[NUnit.Extension.DependencyInjection.DependencyInjectingTestFixture\] can not run . Building Test Projects ========== Starting test run ========== ========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ========== Building Test Projects Executing all tests in file: SshToolClientTests.cs ========== Starting test run ========== ========== Test run aborted: 0 Tests (0 Passed, 0 Failed, 0 Skipped) run in < 1 ms ==========

[Александр Шмыков](https://hashnode.com/@ADSHMYKOV)

[Aug 14, 2024](https://hashnode.com/posts/using-dependency-injection-in-units-and-integrations-tests/61d2ce66d335286be6cddce0/comment/66bc6e895ecbcf805c5255c2#reply-66bcd68c673cfd6d68778c13)

I inject dependency in test by anther trick

```
  [TestFixture]
  public class SshToolClientTests
  {
      private ServiceProvider serviceProvider { get; set; }
      private ISshToolClient client=> serviceProvider.GetService<ISshToolClient>();

      public SshToolClientTests() {
          serviceProvider = new ServiceCollection()
         .AddTransient<ISshToolClient, SshToolClient.SshToolClient>()
         .BuildServiceProvider();

      }


      [OneTimeTearDown]
      public void OneTimeTeardown()
      {
          serviceProvider.Dispose();
      }
      [SetUp]
      public void Setup()
      {
      }

      [Test]
      public void GenerateConfigFile( )
      {
          ISshConfig config= client.GetDefaultConfiguration();
          Assert.IsNotNull(config);
          var json=JsonConvert.SerializeObject(config, Formatting.Indented);
          string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "generatedSshConfig.json");
          File.WriteAllText(path, json);
          TestContext.WriteLine(@$"file:///{path.Replace('\\','/')}");
      }
  }
```

[Александр Шмыков](https://hashnode.com/@ADSHMYKOV)

[Aug 14, 2024](https://hashnode.com/posts/using-dependency-injection-in-units-and-integrations-tests/61d2ce66d335286be6cddce0/comment/66bc49039212975c61bba78e)

Thank you .You are save my time:)!
