CommonApiTestLib 1.0.3
See the version list below for details.
dotnet add package CommonApiTestLib --version 1.0.3
NuGet\Install-Package CommonApiTestLib -Version 1.0.3
<PackageReference Include="CommonApiTestLib" Version="1.0.3" />
paket add CommonApiTestLib --version 1.0.3
#r "nuget: CommonApiTestLib, 1.0.3"
// Install CommonApiTestLib as a Cake Addin #addin nuget:?package=CommonApiTestLib&version=1.0.3 // Install CommonApiTestLib as a Cake Tool #tool nuget:?package=CommonApiTestLib&version=1.0.3
Steps to use NuGet package CommonApiTestLib
- Create NUnit Test project <br />
- Search and download the lastest NuGet package
CommonApiTestLib
<br /> - Add dependencies files
test.runsettings
and envrionment config fileappsetting.{env}.json
example see below<br /> - Create endpoint by inheriting from the
EndpointCommon
abstract class. <br />- HTTP Method and Resource: It defines a constant HTTP method (Method.Post) and a resource path ("/auth") specific to the authentication endpoint.<br />
- Constructor: The constructor initializes the AuthEndpoint instance by calling the protected constructor of the base EndpointCommon class with the specified HTTP method and resource path. This constructor ensures that the AuthEndpoint adheres to the structure defined by the abstract class.
public class AuthEndpoint : EndpointCommon
{
private const Method method = Method.Post;
private const string Resource = "/auth";
protected override string? Schema { get; set; } = "auth.json";
public AuthEndpoint() : base(method, Resource)
{
}
public void SetRequest(object jsonBody)
{
AddJsonBody(jsonBody);
}
- Create Tests after defining endpoint, you can use instances in your Test Class and also consume core function from NuGet Package
[Test]
public async Task AuthTests()
{
// Step 1: Define API Endpoint and Request Body
var endpoint = new AuthEndpoint();
var jsonBody = new
{
username = "admin",
password = "password123"
};
// Step 2: Send Request
endpoint.SetRequest(jsonBody);
var responseBody = await endpoint.ExecuteAsync<Auth>();
// Step 3 : Assertion
Assert.That(endpoint.AssertSuccessResponse(responseBody),"Response Code is not success ");
Assert.That(endpoint.ValidateResponseWithJsonSchema(responseBody), "Response body schema check has failed");
Assert.That(responseBody.Data.token,Is.Not.Empty);
}
Create a NuGet Package
Create and publish a NuGet package by using Visual Studio
- step1: update .csproj metadata
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MyNuGetPackage</PackageId>
<Version>1.0.0</Version>
<Authors>Your Name</Authors>
<Company>Your Company</Company>
</PropertyGroup>
</Project>
step2: Select Build > Configuration Manager, and then set the Active solution configuration to Release.
step3: Visual Studio builds the project and creates the .nupkg file under Release folder
Generate NuGet package on build
Expand the Package node, select General, and then select Generate NuGet package on build.
dependencies or configuration requirements example
// test.runsettings
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<TestRunParameters>
<Parameter name="someParameterName" value="someExampleValue" />
</TestRunParameters>
<RunConfiguration>
<EnvironmentVariables>
<EnvironmentName>qa</EnvironmentName>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
//appsettings.qa.json
{
"Api": {
"BaseUrl": "https://restful-booker.herokuapp.com",
"Username":"abc",
"Password": "password"
},
"ConnectionStrings": {
"DefaultConnection": "Server=YOURSERVERNAME; Database=YOURDATABASENAME; Trusted_Connection=True; MultipleActiveResultSets=true"
}
}
Summary
By following these steps, you should be able to successfully consume the abstract class from the NuGet package in your project. Remember to handle any dependencies or configuration requirements specified by the package documentation.
Todo:
Tell difference between two test methods async Task
and void
- tests are involving asynchronous operations (todo) eg: api response using async method
- tradtionaly can use
void
for simplicity
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 was computed. 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. |
-
net7.0
- coverlet.collector (>= 3.2.0)
- Microsoft.Extensions.Configuration (>= 7.0.0)
- Microsoft.Extensions.Configuration.Json (>= 7.0.0)
- Microsoft.NET.Test.Sdk (>= 17.5.0)
- Newtonsoft.Json.Schema (>= 3.0.15)
- NUnit (>= 3.13.3)
- NUnit.Analyzers (>= 3.6.1)
- NUnit3TestAdapter (>= 4.4.2)
- RestSharp (>= 110.2.0)
- RichardSzalay.MockHttp (>= 7.0.0)
- Serilog (>= 3.0.1)
- Serilog.Sinks.Console (>= 4.1.0)
- Serilog.Sinks.Debug (>= 2.0.0)
- Serilog.Sinks.File (>= 5.0.0)
- System.Data.SqlClient (>= 4.8.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.