xunit-dependency-injection
:fire: A small library to help .NET developers leverage Microsoft's dependency injection framework in their Xunit-powered test projects
Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (12.6%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
:fire: A small library to help .NET developers leverage Microsoft's dependency injection framework in their Xunit-powered test projects
Basic Info
- Host: GitHub
- Owner: Umplify
- License: mit
- Language: C#
- Default Branch: main
- Homepage: https://umplify.github.io/xunit-dependency-injection/
- Size: 371 KB
Statistics
- Stars: 57
- Watchers: 2
- Forks: 15
- Open Issues: 1
- Releases: 75
Topics
Metadata Files
README.md
Xunit Dependency Injection framework - .NET 9.0
Xunit does not support any built-in dependency injection features, therefore developers have to come up with a solution to recruit their favourite dependency injection framework in their tests.
This library brings in Microsoft's dependency injection container to Xunit by leveraging Xunit's fixture.
Important: xUnit versions
- For xUnit packages use Xunit.Microsoft.DependencyInjection versions up to 9.0.5
- For xUnit.v3 packages use Xunit.Microsoft.DependencyInjection versions from 9.1.0
Also please check the migration guide from xUnit for test authors.
Example on how to reference xunit.v3
xml
<PackageReference Include="xunit.v3" Version="2.0.3" />
Getting started
Nuget package
First add the following nuget package to your Xunit project:
ps
Install-Package Xunit.Microsoft.DependencyInjection
Setup your fixture
The abstract class of Xunit.Microsoft.DependencyInjection.Abstracts.TestBedFixture contains the necessary functionalities to add services and configurations to Microsoft's dependency injection container. Your concrete test fixture class must derive from this abstract class and implement the following two abstract methods:
csharp
protected abstract void AddServices(IServiceCollection services, IConfiguration? configuration);
protected abstract IEnumerable<TestAppSettings> GetTestAppSettings();
protected abstract ValueTask DisposeAsyncCore();
GetConfigurationFiles(...) method returns a collection of the configuration files in your Xunit test project to the framework. AddServices(...) method must be used to wire up the implemented services.
Secret manager
Secret manager is a great tool to store credentials, API keys, and other secret information for development purposes. This library has started supporting user secrets from version 8.2.0 onwards. To utilize user secrets in your tests, simply override the virtual method below from the TestBedFixture class:
csharp
protected override void AddUserSecrets(IConfigurationBuilder configurationBuilder);
Access the wired up services
There are two method that you can use to access the wired up service depending on your context:
csharp
public T GetScopedService<T>(ITestOutputHelper testOutputHelper);
public T GetService<T>(ITestOutputHelper testOutputHelper);
To access async scopes simply call the following method in the abstract fixture class:
csharp
public AsyncServiceScope GetAsyncScope(ITestOutputHelper testOutputHelper);
Accessing the keyed wired up services in .NET 9.0
You can call the following method to access the keyed already-wired up services:
csharp
T? GetKeyedService<T>([DisallowNull] string key, ITestOutputHelper testOutputHelper);
Adding custom logging provider
Test developers can add their own desired logger provider by overriding AddLoggingProvider(...) virtual method defined in TestBedFixture class.
Preparing Xunit test classes
Your Xunit test class must be derived from Xunit.Microsoft.DependencyInjection.Abstracts.TestBed<T> class where T should be your fixture class derived from TestBedFixture.
Also, the test class should be decorated by the following attribute:
csharp
[CollectionDefinition("Dependency Injection")]
Clearing managed resources
To have managed resources cleaned up, simply override the virtual method of Clear(). This is an optional step.
Clearing managed resourced asynchronously
Simply override the virtual method of DisposeAsyncCore() for this purpose. This is also an optional step.
Running tests in order
The library also has a bonus feature that simplifies running tests in order. The test class does not have to be derived from TestBed<T> class though and it can apply to all Xunit classes.
Decorate your Xunit test class with the following attribute and associate TestOrder(...) with Fact and Theory:
csharp
[TestCaseOrderer("Xunit.Microsoft.DependencyInjection.TestsOrder.TestPriorityOrderer", "Xunit.Microsoft.DependencyInjection")]
Supporting configuration from UserSecrets
This library's TestBedFixture abstract class exposes an instance of IConfigurationBuilder that can be used to support UserSecrets when configuring the test projects:
csharp
public IConfigurationBuilder ConfigurationBuilder { get; private set; }
Examples
- Please follow this link to view a couple of examples on utilizing this library.
- Digital Silo's unit tests and integration tests are using this library.
One more thing
Do not forget to include the following nuget packages to your Xunit project:
- Microsoft.Extensions.DependencyInjection
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Options
- Microsoft.Extensions.Configuration.Binder
- Microsoft.Extensions.Configuration.FileExtensions
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.Logging
- Microsoft.Extensions.Configuration.EnvironmentVariables
Owner
- Name: Umplify
- Login: Umplify
- Kind: organization
- Location: Toronto, Canada
- Repositories: 3
- Profile: https://github.com/Umplify
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If you use this library, please cite it as below." authors: - family-names: "Sabet" given-names: "Arash" orcid: "https://orcid.org/0009-0004-0416-9935" title: "Xunit Dependency Injection" version: 8.2.2 # doi: 10.5281/zenodo.1234 date-released: 2024-09-23 url: "https://github.com/Umplify/xunit-dependency-injection"
GitHub Events
Total
- Create event: 28
- Release event: 15
- Issues event: 26
- Watch event: 12
- Delete event: 18
- Issue comment event: 11
- Push event: 30
- Pull request review event: 9
- Pull request event: 27
- Fork event: 2
Last Year
- Create event: 28
- Release event: 15
- Issues event: 26
- Watch event: 12
- Delete event: 18
- Issue comment event: 11
- Push event: 30
- Pull request review event: 9
- Pull request event: 27
- Fork event: 2
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Arash A. Sabet | 2****t | 187 |
| dependabot[bot] | 4****] | 83 |
| dependabot-preview[bot] | 2****] | 1 |
| Christian Dörnen | c****n@e****e | 1 |
| Christian Dörnen | 3****n | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 62
- Total pull requests: 165
- Average time to close issues: about 1 month
- Average time to close pull requests: 3 days
- Total issue authors: 4
- Total pull request authors: 5
- Average comments per issue: 0.37
- Average comments per pull request: 0.38
- Merged pull requests: 119
- Bot issues: 0
- Bot pull requests: 104
Past Year
- Issues: 16
- Pull requests: 22
- Average time to close issues: 4 days
- Average time to close pull requests: 8 days
- Issue authors: 2
- Pull request authors: 3
- Average comments per issue: 0.13
- Average comments per pull request: 0.27
- Merged pull requests: 19
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- Arash-Sabet (63)
- mstortz (1)
- nabeelio (1)
- adipuscasu (1)
- hubert-associates (1)
Pull Request Authors
- dependabot[bot] (124)
- Arash-Sabet (77)
- ChrisDoernen (9)
- frogcrush (2)
- dependabot-preview[bot] (1)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- Microsoft.Extensions.Configuration 6.0.1
- Microsoft.Extensions.Configuration.Binder 6.0.0
- Microsoft.Extensions.Configuration.FileExtensions 6.0.0
- Microsoft.Extensions.Configuration.Json 6.0.0
- Microsoft.Extensions.DependencyInjection 6.0.0
- Microsoft.Extensions.Logging 6.0.0
- Microsoft.Extensions.Options 6.0.0
- Microsoft.NET.Test.Sdk 17.3.0
- coverlet.collector 3.1.2
- xunit 2.4.2
- xunit.runner.visualstudio 2.4.5
- Microsoft.Extensions.Configuration.EnvironmentVariables 6.0.1
- Microsoft.Extensions.Configuration.FileExtensions 6.0.0
- Microsoft.Extensions.Configuration.Json 6.0.0
- Microsoft.Extensions.DependencyInjection.Abstractions 6.0.0
- Microsoft.Extensions.Logging 6.0.0
- xunit.abstractions 2.0.3
- xunit.core 2.4.2
- actions/checkout v2 composite
- github/codeql-action/analyze v1 composite
- github/codeql-action/autobuild v1 composite
- github/codeql-action/init v1 composite
- actions/stale v1 composite