Skip to content

Creating API Unit Tests

JainalGandhi edited this page May 16, 2020 · 16 revisions

Pre-requisites

To get started with the NUnit Testing Framework, run the testing project, WediumTestSuite. Doing so will install all required NuGet packages:

  • Microsoft.AspNetCore
  • Microsoft.NET.Test.SDK
  • NUnit
  • NUnit3TestAdaptor

Creating Test Files

When creating new test files in the WediumTestSuite project, ensure the correct naming conventions are followed (file name should be suffixed by Test).

Information regarding how to write NUnit test cases can be found here.

A basic template of what all Wedium test classes should contain can be found here.

In-memory Database

Wedium's backend test suite utilises an in-memory database to allow tests to be offline and fully independent. Each TestServerHandler instance has its own independent database. In order to create and use the database then TestServerHandler must be instantiated. This is done as follows:

TestServerHandler testServer = new TestServerHandler();

The above code should be added to the [SetUp] method of test files. The handling of the testServer lifecycle is automatically handled; it does not require disposal on completion by the test fixture.

To gain direct access to the in-memory database then the following should be done:

using (WediumContext db = new WediumContext(testServer.getWediumContextOptions())
{
    // Your code which accesses db here
}

The initial state of the in-memory database can be modified in MockDBContextInitializer. However, please keep this as minimal and clean/consistent as possible.

Creating Integration Testing

Creating integration tests requires a TestServer to imitate the execution of WediumAPI. This can be instantiated as follows:

TestServerHandler testServer = new TestServerHandler();

The above code should be added to the [SetUp] method of test files. The handling of the testServer lifecycle is automatically handled; it does not require disposal on completion by the test fixture.

Clients can then be instantiated and endpoints can be called as follows:

HttpClient client = testServer.CreateClient();
HttpResponseMessage response = await client.GetAsync(<endpoint:string>);
string responseContent = await response.Content.ReadAsStringAsync();

Authenticating Clients

To enter endpoints decorated with the [Authorize] annotation, a claim can be utilized. Currently, the JWT token claim value is configured to use a user's UserId. With a valid UserId an authenticated client can be created as follows:

HttpClient client = testServerHandler.CreateClient([userId:int]);

If a client already exists, it can be authenticated with testServerHandler.AuthenticateClient(<client:HttpClient>, <userId:int>);.

Resolving AppSetting Values in Tests

The values within appsettings.json may be required in the test fixtures. This can be resolved as follows:

<T> value = AppSettingsResolver.GetSetting<T>(<key:string>);

The key for nested json properties are delimited by the : character (e.g. The "GetPostBatchSize" property located in "Options" can be retrieved with the key "Options:GetPostBatchSize").

Clone this wiki locally