Neptunee.xApi
1.0.5
dotnet add package Neptunee.xApi --version 1.0.5
NuGet\Install-Package Neptunee.xApi -Version 1.0.5
<PackageReference Include="Neptunee.xApi" Version="1.0.5" />
paket add Neptunee.xApi --version 1.0.5
#r "nuget: Neptunee.xApi, 1.0.5"
// Install Neptunee.xApi as a Cake Addin #addin nuget:?package=Neptunee.xApi&version=1.0.5 // Install Neptunee.xApi as a Cake Tool #tool nuget:?package=Neptunee.xApi&version=1.0.5
xApi - Single line to test your api !
Easy way to build integration test using xUnit & WebApplicationFactory
Basic Usage
_fixture.Api.Rout<SampleControllerTest>(nameof(SampleControllerTest.GetAll))
.FromQuery(nameof(name),name)
.Send()
.AssertSuccessStatusCode();
- OR :
_fixture.Api.Rout(HttpMethod.Get, "api/something/getbyid")
.FromRoute(nameof(id),id)
.Send()
.AssertSuccessStatusCode();
Setup
create Fixture class to use it in all tests
public class IntegrationTestFixture : IntegrationTest<Startup>
{
public IntegrationTestFixture()
{
Configure(webApplicationBuilder: builder => builder.UseEnvironment(Environments.Development),
clientBuilder: options => options.AllowAutoRedirect = false);
}
}
Example how to use IntegrationTestFixture
for SampleControllerTest
api testing
public class SampleControllerTest : IClassFixture<IntegrationTestFixture>
{
private readonly IntegrationTestFixture _fixture;
public SampleControllerTest(IntegrationTestFixture fixture, ITestOutputHelper output)
{
_fixture = fixture;
_fixture.SetApiOutput(output); // to dispaly some info while sending apis (url , request , response , status code ..)
}
[Fact]
public Task AnyApiTest()
{
// logic
}
}
How To Send Query / Routes
var id = Guid.NewGuid();
_fixture.Api.Rout<SampleControllerTest>(nameof(SampleControllerTest.Get))
.FromQuery(nameof(id),id) // key & value
.FromQuery(new // anonymos object
{
id,
})
.FromQuery(new AnyDto() // object like dto, mv, ..etc
{
Id = id,
})
same for Routes just use FromRoute()
instead of FromQuery()
.
How To Send Json / FormData
_fixture.Api.Rout<SampleControllerTest>(nameof(SampleControllerTest.Add))
.FromBody(new AddRequstClass // for Json body request
{
Id = Guid.NewGuid(),
Email = "test@xapi.com",
})
.FromForm(new AddRequstClass // for Multipart Form Data body request
{
Name = "Simple Test",
ImageFile = _fixture.FormFile("AssetsFolder/test.png")
})
Assert Status Code
**You can use this methods 😗*
AssertSuccessStatusCodeAsync()
/AssertSuccessStatusCode()
when its range 200-299.AssertNotSuccessStatusCodeAsync()
/AssertNotSuccessStatusCode()
when its otherwise range 200-299.AssertStatusCodeEqualsAsync(HttpStatusCode.OK)
/AssertStatusCodeEquals(HttpStatusCode.OK)
AssertStatusCodeNotEqualsAsync(HttpStatusCode.Conflict)
/AssertStatusCodeNotEquals(HttpStatusCode.Conflict)
Working With Files
You have FormFile()
method in IntegrationTest<TStartup>
to help you to send files as IFormFile with 3 overloads :
public IFormFile FormFile(string path, string? contentType = null)
public IFormFile FormFile(Stream stream, string fileName, string? contentType = null)
public IFormFile FormFile(byte[] bytes, string fileName, string? contentType = null)
Note: the optional contentType parameter throw null exception if TryGetContentType() returns false, means contentType is unknown.
PS: can send empty Files with FakeFormFile()
method.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
-
net6.0
- Microsoft.AspNetCore.Mvc.Testing (>= 6.0.10)
- Newtonsoft.Json (>= 13.0.2)
- xunit.abstractions (>= 2.0.3)
- xunit.assert (>= 2.4.2)
- Xunit.Microsoft.DependencyInjection (>= 7.0.3)
-
net7.0
- Microsoft.AspNetCore.Mvc.Testing (>= 6.0.10)
- Newtonsoft.Json (>= 13.0.2)
- xunit.abstractions (>= 2.0.3)
- xunit.assert (>= 2.4.2)
- Xunit.Microsoft.DependencyInjection (>= 7.0.3)
-
net8.0
- Microsoft.AspNetCore.Mvc.Testing (>= 6.0.10)
- Newtonsoft.Json (>= 13.0.2)
- xunit.abstractions (>= 2.0.3)
- xunit.assert (>= 2.4.2)
- Xunit.Microsoft.DependencyInjection (>= 7.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.