{"id":19990813,"url":"https://github.com/pengweiqhca/Xunit.DependencyInjection","last_synced_at":"2025-05-04T10:30:46.411Z","repository":{"id":39619945,"uuid":"120432743","full_name":"pengweiqhca/Xunit.DependencyInjection","owner":"pengweiqhca","description":"Use Microsoft.Extensions.DependencyInjection to resolve xUnit test cases.","archived":false,"fork":false,"pushed_at":"2025-04-22T03:52:53.000Z","size":566,"stargazers_count":391,"open_issues_count":12,"forks_count":53,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-22T05:18:44.047Z","etag":null,"topics":["ioc","xunit"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pengweiqhca.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-06T09:36:52.000Z","updated_at":"2025-04-22T04:59:39.000Z","dependencies_parsed_at":"2023-02-08T21:15:48.575Z","dependency_job_id":"df370f1b-ca7f-4038-bc95-956b9bc5ecf0","html_url":"https://github.com/pengweiqhca/Xunit.DependencyInjection","commit_stats":{"total_commits":245,"total_committers":15,"mean_commits":"16.333333333333332","dds":"0.42040816326530617","last_synced_commit":"5558ef8534318f3eefb3afa513dad7efbf6d6a12"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengweiqhca%2FXunit.DependencyInjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengweiqhca%2FXunit.DependencyInjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengweiqhca%2FXunit.DependencyInjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pengweiqhca%2FXunit.DependencyInjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pengweiqhca","download_url":"https://codeload.github.com/pengweiqhca/Xunit.DependencyInjection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252319985,"owners_count":21729050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ioc","xunit"],"created_at":"2024-11-13T04:51:28.937Z","updated_at":"2025-05-04T10:30:46.393Z","avatar_url":"https://github.com/pengweiqhca.png","language":"C#","funding_links":[],"categories":["C\\#"],"sub_categories":[],"readme":"﻿# Use `Microsoft.Extensions.DependencyInjection` to resolve xUnit test cases\n\n\u003e XUnit v2 users: please use [v2](https://github.com/pengweiqhca/Xunit.DependencyInjection/tree/v2) branch, Xunit.DependencyInjection.xRetry depend on upstream packages, waiting for update.\n\u003e Xunit.DependencyInjection.SkippableFact are obsoleted on xunit.v3.\n\n## How to use\n\nInstall the [Nuget](https://www.nuget.org/packages/Xunit.DependencyInjection) package.\n\n```sh\ndotnet add package Xunit.DependencyInjection\n```\n\nIn your testing project, add the following framework\n\n```cs\nnamespace Your.Test.Project\n{\n    public class Startup\n    {\n        public void ConfigureServices(IServiceCollection services)\n        {\n            services.AddTransient\u003cIDependency, DependencyClass\u003e();\n        }\n    }\n}\n```\n\nExample test `class`.\n\n```cs\npublic interface IDependency\n{\n    int Value { get; }\n}\n\ninternal class DependencyClass : IDependency\n{\n    public int Value =\u003e 1;\n}\n\npublic class MyAwesomeTests\n{\n    private readonly IDependency _d;\n\n    public MyAwesomeTests(IDependency d) =\u003e _d = d;\n\n    [Fact]\n    public void AssertThatWeDoStuff()\n    {\n        Assert.Equal(1, _d.Value);\n    }\n}\n```\n\n\u003e  `Xunit.DependencyInjection` is built into the generic host and fully supports its lifecycle, allowing you to use all features supported by the generic host, including but not limited to `IHostedService`.\n\n## Integration asp.net core TestHost(3.0+)\n\n### Asp.Net Core Startup\n\n```sh\ndotnet add package Microsoft.AspNetCore.TestHost\n```\n\n```C#\npublic class Startup\n{\n    public void ConfigureHost(IHostBuilder hostBuilder) =\u003e hostBuilder\n        .ConfigureWebHost[Defaults](webHostBuilder =\u003e webHostBuilder\n        .UseTestServer(options =\u003e options.PreserveExecutionContext = true)\n        .UseStartup\u003cAspNetCoreStartup\u003e());\n}\n```\n\n### MinimalApi\n\nIf you use MinimalApi rather than asp.net core Startup class.\n\nAdd package reference for `Xunit.DependencyInjection.AspNetCoreTesting`\n\n```sh\ndotnet add package Xunit.DependencyInjection.AspNetCoreTesting\n```\n\n```C#\npublic class Startup\n{\n    public IHostBuilder CreateHostBuilder() =\u003e MinimalApiHostBuilderFactory.GetHostBuilder\u003cProgram\u003e();\n}\n```\n\n\u003e Maybe your asp.net core project should InternalsVisibleTo or add `public partial class Program {}` in the end of `Program.cs`;\n\u003e\n\u003e Detail see [Xunit.DependencyInjection.Test.AspNetCore](https://github.com/pengweiqhca/Xunit.DependencyInjection/tree/main/test/Xunit.DependencyInjection.Test.AspNetCore)\n\n## `Startup` limitation\n\n`Startup` supports two configuration styles, and the `Configure` method is supported by both.\n\n### HostApplicationBuilder-style\n\n* `ConfigureHostApplicationBuilder` method (distinguish by this method)\n\n    ``` C#\n    public void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder) { }\n    ```\n\n* `BuildHostApplicationBuilder` method\n\n  \u003e **NOTE**: If this method signature is not found, the host is built by simply calling `hostApplicationBuilder.Build();`.\n\n  ``` C#\n  public IHost BuildHostApplicationBuilder(HostApplicationBuilder hostApplicationBuilder)\n  {\n      return hostApplicationBuilder.Build();\n  }\n  ```\n\n### Startup-style\n\n* `CreateHostBuilder` method\n\n  ``` C#\n  public class Startup\n  {\n      public IHostBuilder CreateHostBuilder([AssemblyName assemblyName]) { }\n  }\n  ```\n\n* `ConfigureHost` method\n\n    ```C#\n    public class Startup\n    {\n        public void ConfigureHost(IHostBuilder hostBuilder) { }\n    }\n    ```\n\n* `ConfigureServices` method\n\n    ```C#\n    public class Startup\n    {\n        public void ConfigureServices(IServiceCollection services[, HostBuilderContext context]) { }\n    }\n    ```\n\n* `BuildHost` method\n\n  \u003e **NOTE**: If this method signature is not found, the host is built by simply calling `hostBuilder.Build();`.\n\n    ```C#\n    public class Startup\n    {\n        public IHost BuildHost([IHostBuilder hostBuilder]) { return hostBuilder.Build(); }\n    }\n    ```\n\n## How to find `Startup`?\n\n### 1. Specific startup\n\nDeclare [Startup] on test class\n\n### 2. Nested startup\n\n```C#\npublic class TestClass1\n{\n    public class Startup\n    {\n        public void ConfigureServices(IServiceCollection services) { }\n    }\n```\n\n### 3. Closest startup\n\nIf the class type full name is \"A.B.C.TestClass\", find Startup in the following order:\n\n1. `A.B.C.Startup`\n2. `A.B.Startup`\n3. `A.Startup`\n4. `Startup`\n\n### 4. Default startup\n\n\u003e Default startup is required before 8.7.0, is optional in some case after 8.7.0.\n\u003e\n\u003e If is required, please add a startup class in your test project.\n\nDefault is find `Your.Test.Project.Startup, Your.Test.Project`.\n\nIf you want to use a special `Startup`, you can define `XunitStartupAssembly` and `XunitStartupFullName` in the `PropertyGroup` section\n\n```xml\n\u003cProject\u003e\n  \u003cPropertyGroup\u003e\n    \u003cXunitStartupAssembly\u003eAbc\u003c/XunitStartupAssembly\u003e\n    \u003cXunitStartupFullName\u003eXyz\u003c/XunitStartupFullName\u003e\n  \u003c/PropertyGroup\u003e\n\u003c/Project\u003e\n```\n\n| XunitStartupAssembly | XunitStartupFullName | Startup                                      |\n| -------------------- | -------------------- | -------------------------------------------- |\n|                      |                      | Your.Test.Project.Startup, Your.Test.Project |\n| Abc                  |                      | Abc.Startup, Abc                             |\n|                      | Xyz                  | Xyz, Your.Test.Project                       |\n| Abc                  | Xyz                  | Xyz, Abc                                     |\n\n## Parallel\n\nBy default, xUnit runs all test cases in a test class synchronously. This package can extend the test framework to execute tests in parallel.\n\n```xml\n\u003cProject\u003e\n\n  \u003cPropertyGroup\u003e\n    \u003cParallelizationMode\u003e\u003c/ParallelizationMode\u003e\n  \u003c/PropertyGroup\u003e\n\n\u003c/Project\u003e\n```\n\nThis package has two policies to run test cases in parallel.\n\n1. Enhance or true\n\n   Respect xunit [parallelization](https://xunit.net/docs/running-tests-in-parallel) behavior.\n2. Force\n\n   Ignore xunit [parallelization](https://xunit.net/docs/running-tests-in-parallel) behavior and force running tests in parallel.\n\nIf [`[Collection]`](https://github.com/xunit/xunit/issues/1227#issuecomment-297131879)(if ParallelizationMode is not `Force`), `[CollectionDefinition(DisableParallelization = true)]`, `[DisableParallelization]` declared on the test class, the test class will run sequentially. If `[DisableParallelization]`, `[MemberData(DisableDiscoveryEnumeration = true)]` declared on the test method, the test method will run sequentially.\n\n**It is recommended to use xunit 2.8.0+ and without setting `parallelAlgorithm`**\n\n\u003e Thanks [Meziantou.Xunit.ParallelTestFramework](https://github.com/meziantou/Meziantou.Xunit.ParallelTestFramework)\n\n## How to disable Xunit.DependencyInjection\n\n```xml\n\u003cProject\u003e\n    \u003cPropertyGroup\u003e\n        \u003cEnableXunitDependencyInjectionDefaultTestFrameworkAttribute\u003efalse\u003c/EnableXunitDependencyInjectionDefaultTestFrameworkAttribute\u003e\n    \u003c/PropertyGroup\u003e\n\u003c/Project\u003e\n```\n\n## How to inject ITestOutputHelper\n\n```C#\ninternal class DependencyClass : IDependency\n{\n    private readonly ITestOutputHelperAccessor _testOutputHelperAccessor;\n\n    public DependencyClass(ITestOutputHelperAccessor testOutputHelperAccessor)\n    {\n        _testOutputHelperAccessor = testOutputHelperAccessor;\n    }\n}\n```\n\n## Write `Microsoft.Extensions.Logging` to `ITestOutputHelper`\n\nAdd package reference for `Xunit.DependencyInjection.Logging`\n\n```sh\ndotnet add package Xunit.DependencyInjection.Logging\n```\n\n\u003e The call chain must be from the test case. If not, this feature will not work.\n\n```C#\npublic class Startup\n{\n    public void ConfigureServices(IServiceCollection services) =\u003e services\n        .AddLogging(lb =\u003e lb.AddXunitOutput());\n}\n```\n\n## How to inject `IConfiguration` or `IHostEnvironment` into `Startup`?\n\n```C#\npublic class Startup\n{\n    public void ConfigureHost(IHostBuilder hostBuilder) =\u003e hostBuilder\n        .ConfigureServices((context, services) =\u003e { context.XXXX });\n}\n```\n\nor\n\n```C#\npublic class Startup\n{\n    public void ConfigureServices(IServiceCollection services, HostBuilderContext context)\n    {\n        context.XXXX;\n    }\n}\n```\n\n## How to configure `IConfiguration`?\n\n```C#\npublic class Startup\n{\n    public void ConfigureHost(IHostBuilder hostBuilder) =\u003e hostBuilder\n        .ConfigureHostConfiguration(builder =\u003e { })\n        .ConfigureAppConfiguration((context, builder) =\u003e { });\n}\n```\n\n## [MemberData] how to inject?\n\nUse **[MethodData]**\n\n## Integrate OpenTelemetry\n\n```C#\nTracerProviderBuilder builder;\n\nbuilder.AddSource(\"Xunit.DependencyInjection\");\n```\n\n## Do something before and after test case\n\nInherit `BeforeAfterTest` and register as `BeforeAfterTest` service.\n\n[See demo](https://github.com/pengweiqhca/Xunit.DependencyInjection/blob/main/test/Xunit.DependencyInjection.Test/BeforeAfterTestTest.cs#13).\n\n## Initialize some data on startup.\n\nIf it is synchronous initialization, you can use the `Configure` method. If it is asynchronous initialization, you should use `IHostedService`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpengweiqhca%2FXunit.DependencyInjection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpengweiqhca%2FXunit.DependencyInjection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpengweiqhca%2FXunit.DependencyInjection/lists"}