|
| 1 | +# Xunit Dependency Injection Library for .NET 9.0 |
| 2 | + |
| 3 | +Xunit Dependency Injection is a .NET library that brings Microsoft's dependency injection container to Xunit by leveraging Xunit's fixture pattern. This library enables dependency injection in xUnit tests using familiar Microsoft.Extensions.DependencyInjection patterns. |
| 4 | + |
| 5 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Prerequisites and Installation |
| 10 | +- **CRITICAL**: This project requires .NET 9.0 SDK (version 9.0.304 or later) and .NET 9.0 runtime (version 9.0.8 or later). |
| 11 | +- Install .NET 9.0 SDK: |
| 12 | + ```bash |
| 13 | + curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --version 9.0.304 --install-dir /tmp/dotnet |
| 14 | + export PATH="/tmp/dotnet:$PATH" |
| 15 | + export DOTNET_ROOT="/tmp/dotnet" |
| 16 | + ``` |
| 17 | +- Install .NET 9.0 runtime: |
| 18 | + ```bash |
| 19 | + curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --version 9.0.8 --runtime dotnet --install-dir /tmp/dotnet |
| 20 | + ``` |
| 21 | +- Verify installation: `dotnet --version` should return `9.0.304` or later |
| 22 | +- Verify runtime: `dotnet --list-runtimes` should show `Microsoft.NETCore.App 9.0.8` |
| 23 | + |
| 24 | +### Build and Test Commands |
| 25 | +- **Navigate to source directory**: `cd /path/to/xunit-dependency-injection/src` |
| 26 | +- **Restore packages**: `dotnet restore` -- takes ~8 seconds. NEVER CANCEL. Set timeout to 30+ seconds. |
| 27 | +- **Build library**: `dotnet build --configuration Release` -- takes ~5.5 seconds. NEVER CANCEL. Set timeout to 30+ seconds. |
| 28 | +- **Build examples**: The build command automatically builds both the main library and example tests. |
| 29 | +- **Run example tests**: |
| 30 | + ```bash |
| 31 | + cd ../examples/Xunit.Microsoft.DependencyInjection.ExampleTests |
| 32 | + dotnet test --configuration Release |
| 33 | + ``` |
| 34 | + -- takes ~10.8 seconds with 9 tests passing. NEVER CANCEL. Set timeout to 60+ seconds. |
| 35 | +- **Package library**: `dotnet pack --configuration Release` -- takes ~1.9 seconds. NEVER CANCEL. Set timeout to 30+ seconds. |
| 36 | + |
| 37 | +### Code Quality and Formatting |
| 38 | +- **Format code**: `dotnet format Xunit.Microsoft.DependencyInjection.sln` -- fixes whitespace and code style issues per .editorconfig |
| 39 | +- **Verify formatting**: `dotnet format Xunit.Microsoft.DependencyInjection.sln --verify-no-changes --verbosity diagnostic` |
| 40 | +- **ALWAYS** run `dotnet format` before committing changes to maintain code style consistency |
| 41 | +- The project uses .editorconfig with specific C# coding standards including tabs for indentation and CRLF line endings |
| 42 | + |
| 43 | +### Development Workflow |
| 44 | +- Always set `PATH="/tmp/dotnet:$PATH"` and `DOTNET_ROOT="/tmp/dotnet"` in your session when working with .NET 9.0 |
| 45 | +- Test your changes by running the example tests which demonstrate real usage scenarios |
| 46 | +- The library targets `net9.0` framework exclusively |
| 47 | +- Use Visual Studio Code tasks defined in `.vscode/tasks.json` for build, publish, and watch operations |
| 48 | + |
| 49 | +## Validation Scenarios |
| 50 | + |
| 51 | +After making any changes to the library code: |
| 52 | +1. **Build validation**: Run `dotnet build --configuration Release` and ensure it completes successfully |
| 53 | +2. **Test validation**: Run example tests with `dotnet test --configuration Release` and verify all 9 tests pass |
| 54 | +3. **Format validation**: Run `dotnet format` to ensure code follows project standards |
| 55 | +4. **Package validation**: Run `dotnet pack --configuration Release` to ensure the library can be packaged |
| 56 | + |
| 57 | +### Manual Testing Scenarios |
| 58 | +The example tests demonstrate complete usage patterns: |
| 59 | +- **Dependency injection setup**: Tests show how to configure services in `TestProjectFixture` |
| 60 | +- **Service resolution**: Tests verify both scoped and singleton service resolution |
| 61 | +- **Keyed services**: Tests validate keyed service injection (new in .NET 9.0) |
| 62 | +- **Configuration binding**: Tests demonstrate configuration file and user secrets integration |
| 63 | +- **Test ordering**: Tests show the test ordering feature with `TestOrder` attributes |
| 64 | + |
| 65 | +## Project Structure |
| 66 | + |
| 67 | +### Key Directories and Files |
| 68 | +- `/src/` - Main library source code |
| 69 | + - `Abstracts/TestBedFixture.cs` - Base class for test fixtures |
| 70 | + - `Abstracts/TestBed.cs` - Base class for test classes |
| 71 | + - `TestsOrder/TestPriorityOrderer.cs` - Test ordering implementation |
| 72 | + - `.editorconfig` - Code style configuration |
| 73 | +- `/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/` - Working examples and integration tests |
| 74 | + - `Fixtures/TestProjectFixture.cs` - Example fixture setup |
| 75 | + - Various test files demonstrating usage patterns |
| 76 | +- `.github/workflows/` - CI/CD automation (Azure Pipelines used, not GitHub Actions) |
| 77 | +- `.vscode/` - VS Code configuration for development |
| 78 | + |
| 79 | +### Important Configuration Files |
| 80 | +- `src/Xunit.Microsoft.DependencyInjection.csproj` - Main project file targeting net9.0 |
| 81 | +- `azure-pipelines.yml` - Build automation configuration |
| 82 | +- `examples/*/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj` - Example test project |
| 83 | + |
| 84 | +## Common Development Tasks |
| 85 | + |
| 86 | +### Adding New Features |
| 87 | +1. Modify source code in `/src/` directory |
| 88 | +2. Add corresponding tests in `/examples/` directory |
| 89 | +3. Run build and test validation |
| 90 | +4. Format code with `dotnet format` |
| 91 | +5. Verify all example tests still pass |
| 92 | + |
| 93 | +### Troubleshooting Build Issues |
| 94 | +- **SDK Version Error**: Ensure .NET 9.0 SDK is installed and in PATH |
| 95 | +- **Runtime Error during Tests**: Ensure .NET 9.0 runtime is installed and DOTNET_ROOT is set |
| 96 | +- **Format Issues**: Run `dotnet format` to auto-fix most style problems |
| 97 | +- **Missing Dependencies**: Run `dotnet restore` to restore NuGet packages |
| 98 | + |
| 99 | +### Working with Examples |
| 100 | +The examples project is a fully functional test suite that demonstrates: |
| 101 | +- Service registration and dependency injection patterns |
| 102 | +- Configuration file usage with `appsettings.json` |
| 103 | +- User secrets integration for sensitive data |
| 104 | +- Keyed services (Porsche/Toyota car maker examples) |
| 105 | +- Test ordering with custom attributes |
| 106 | +- Both synchronous and asynchronous test patterns |
| 107 | + |
| 108 | +Always use the examples to validate that your changes don't break real-world usage scenarios. |
| 109 | + |
| 110 | +## Build Times and Performance Expectations |
| 111 | +- **Package restore**: ~1-8 seconds (varies with cache state) |
| 112 | +- **Build (Release)**: ~4-6 seconds |
| 113 | +- **Test execution**: ~9-11 seconds (9 tests pass) |
| 114 | +- **Code formatting**: ~7-10 seconds |
| 115 | +- **Package creation**: ~1-2 seconds |
| 116 | +- **Complete workflow**: ~20-25 seconds total |
| 117 | + |
| 118 | +**CRITICAL**: NEVER CANCEL builds or tests. These times are normal. Set timeouts to 60+ minutes for safety, but actual operations complete much faster. |
| 119 | + |
| 120 | +## Azure DevOps Integration |
| 121 | +The project uses Azure Pipelines (not GitHub Actions) for CI/CD: |
| 122 | +- Build configuration in `azure-pipelines.yml` |
| 123 | +- Targets Ubuntu 22.04 build agents |
| 124 | +- Uses .NET 9.0.304 SDK version |
| 125 | +- Publishes to NuGet.org on successful builds |
| 126 | +- Example tests are run as part of the pipeline |
| 127 | + |
| 128 | +## Frequently Used Commands Summary |
| 129 | +```bash |
| 130 | +# Setup environment |
| 131 | +export PATH="/tmp/dotnet:$PATH" |
| 132 | +export DOTNET_ROOT="/tmp/dotnet" |
| 133 | + |
| 134 | +# Build and test workflow |
| 135 | +cd src |
| 136 | +dotnet restore # ~8s |
| 137 | +dotnet build --configuration Release # ~5.5s |
| 138 | +cd ../examples/Xunit.Microsoft.DependencyInjection.ExampleTests |
| 139 | +dotnet test --configuration Release # ~10.8s |
| 140 | + |
| 141 | +# Code quality |
| 142 | +cd ../../src |
| 143 | +dotnet format Xunit.Microsoft.DependencyInjection.sln |
| 144 | + |
| 145 | +# Package |
| 146 | +dotnet pack --configuration Release # ~1.9s |
| 147 | +``` |
| 148 | + |
| 149 | +Always validate your changes by running through this complete workflow before committing. |
0 commit comments