ApiTestFramework 0.1.1
See the version list below for details.
dotnet add package ApiTestFramework --version 0.1.1
NuGet\Install-Package ApiTestFramework -Version 0.1.1
<PackageReference Include="ApiTestFramework" Version="0.1.1" />
<PackageVersion Include="ApiTestFramework" Version="0.1.1" />
<PackageReference Include="ApiTestFramework" />
paket add ApiTestFramework --version 0.1.1
#r "nuget: ApiTestFramework, 0.1.1"
#:package ApiTestFramework@0.1.1
#addin nuget:?package=ApiTestFramework&version=0.1.1
#tool nuget:?package=ApiTestFramework&version=0.1.1
ApiTestFramework & GeoAPI Integration Tests
This solution provides a lightweight API test framework plus in-process integration tests for the GeoAPI endpoints.
Projects
ApiTestFramework(class library): HTTP client, authentication helper, data readers (CSV/JSON/DB), loggers (file + AppInsights).GeoAPI.IntegrationTests(xUnit): Integration tests hosted withWebApplicationFactory<Program>so no external server startup required.
Key Features
- Enhanced
ApiClientwith optional JWT auth, configurable timeout, improved error handling, case-insensitive JSON. - Data-driven capability (CSV/JSON/DB readers) ready for future parameterized tests.
- File logger capturing test run details (
integration-tests.log). - In-memory hosting of GeoAPI for fast, isolated tests.
Running Tests
From repository root:
# Restore & run tests
dotnet test .\ApiTestFramework\GeoAPI.IntegrationTests\GeoAPI.IntegrationTests.csproj -c Debug
Configuration
testsettings.json in the test project controls base URL & timeout. With in-memory hosting the baseUrl is largely informational, but can be repurposed for external environment runs.
Adding New Endpoint Tests
- Create a new
*.csfile inGeoAPI.IntegrationTests. - Inject
ApiTestFixtureviaIClassFixture<ApiTestFixture>. - Use
_fx.Client.GetAsync<T>("Endpoint")etc. - Log with
_fx.Logger.LogInfo(...).
Error Handling
Non-success HTTP responses throw HttpRequestException including status code & body. Assert 404 scenarios with try/catch blocks.
Next Steps
- Add data-driven
Theorytests sourcing from CSV usingCsvDataReader<T>. - Integrate AppInsights logger once instrumentation key/config is available.
- Extend
ApiClientfor PATCH support if needed.
NuGet Packaging
You can create a NuGet package of the framework for reuse across multiple API projects:
dotnet pack .\ApiTestFramework\ApiTestFramework.csproj -c Release -o .\nupkg
Key metadata (PackageId, Description, SourceLink, symbols) is already configured in ApiTestFramework.csproj. Publish by pushing the .nupkg & .snupkg to an internal feed (e.g., Azure Artifacts, GitHub Packages, NuGet.org):
dotnet nuget push .\nupkg\ApiTestFramework.*.nupkg --source "YourFeedUrl" --api-key "YOUR_KEY"
Consuming the Package
In a test project:
dotnet add package ApiTestFramework --version 0.1.0
Then instantiate an ApiClient (example):
var client = new ApiClient(baseUrl: "https://localhost", token: jwtToken);
var about = await client.GetAsync<About>("About");
Allure Reporting Integration
To enable rich test reports with Allure in your (external) test project:
- Add packages:
<PackageReference Include="xunit" Version="2.6.2" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" /> <PackageReference Include="Allure.Xunit" Version="3.0.0.10" /> <PackageReference Include="Allure.Commons" Version="3.0.0.10" /> - Implement an Allure reporter in your test project (keep Allure dependencies out of the core package for lean installs). Create a class that wraps
AllureLifecyclefor steps/attachments. - Annotate tests:
using Allure.Xunit; [AllureSuite("About")] [AllureFeature("Metadata")] public class AboutTests : IClassFixture<ApiTestFixture> { /* ... */ } - Run tests producing Allure results:
$env:ALLURE_CONFIG="allureConfig.json" # if you have a config file dotnet test .\GeoAPI.IntegrationTests\GeoAPI.IntegrationTests.csproj --logger "trx" --results-directory .\allure-results - Generate report (after installing Allure CLI):
allure generate .\allure-results --clean -o .\allure-report allure open .\allure-report
Minimal config file (allureConfig.json):
{
"allure": { "directory": "allure-results" }
}
Sample Custom Allure Reporter (in test project)
public class AllureReporter
{
private readonly AllureLifecycle _l = AllureLifecycle.Instance;
public void Step(string name, Action action)
{
var uuid = Guid.NewGuid().ToString();
_l.StartStep(uuid, new Allure.Commons.Model.StepResult { name = name });
try { action(); _l.UpdateStep(uuid, s => s.status = Allure.Commons.Model.Status.passed); }
catch { _l.UpdateStep(uuid, s => s.status = Allure.Commons.Model.Status.failed); throw; }
finally { _l.StopStep(uuid); }
}
public void Attach(string name, string text)
{
var file = Path.GetTempFileName(); File.WriteAllText(file, text);
_l.AddAttachment(name, "text/plain", file);
}
}
Attachments Strategy
- Use
AttachTextfor small JSON payloads (< ~64KB). - For larger bodies, write to temp file then call
reporter.AttachText("LargeResponse", path)(overload you can add).
CI Example (GitHub Actions Snippet)
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- name: Restore
run: dotnet restore ApiTestFramework/ApiTestFramework.csproj
- name: Build
run: dotnet build ApiTestFramework/ApiTestFramework.csproj -c Release --no-restore
- name: Test (Allure)
run: dotnet test GeoAPI.IntegrationTests/GeoAPI.IntegrationTests.csproj -c Release --results-directory allure-results --logger "trx"
- name: Publish Allure Report
uses: simple-elf/allure-report-action@v1
with:
allure_results: allure-results
report_name: GeoAPI Test Report
Roadmap Suggestions
- Add richer Allure step modeling (use lifecycle Start/Stop for granular timing).
- Provide
IApiClientretry & circuit-breaker via Polly. - Token caching in
JwtAuthenticatorwith expiry tracking. - Data-driven
Theoryexamples pulling fromCsvDataReaderandJsonDataReader. - Switch deprecated
System.Data.SqlClienttoMicrosoft.Data.SqlClient.
Generating Documentation
XML docs are enabled; after packing you can host generated API docs or use DocFX.
License
MIT (placeholder – adjust as needed).
Troubleshooting
- If tests fail due to missing models, ensure project references are intact.
- For debugging responses, temporarily log raw JSON by adding a decorator around
HandleResponse<T>.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- CsvHelper (>= 30.0.1)
- Microsoft.ApplicationInsights (>= 2.21.0)
- Microsoft.Data.SqlClient (>= 5.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.