BaseTestKestrel
BaseTestKestrel provides an ASP.NET Core test host using a real local web server. It supports testing framework actions and controller routes through HTTP requests.
Features
- Local hosting: Starts a
WebApplicationon a configured local port. - Action mapping: Maps public methods ending in
__Actionto HTTP endpoints. - Controller testing: Supports requests to arbitrary application routes.
- Custom configuration: Supports callbacks for services, application configuration, and logging.
- HTTP helpers: Provides helpers for action-based requests and controller routes.
- Host reuse: Reuses the running application for tests sharing the same port.
Behavior
The host is initialized lazily when Client is first accessed. HostStart() triggers this initialization.
Each port identifies one shared WebApplication instance. Derived test classes using the same port therefore share the same host.
Public methods ending in __Action are mapped to endpoints using their method names. Actions can accept no parameters or one string parameter containing the raw query string.
Actions must return string. Exceptions thrown by an action result in an HTTP 500 response containing a JSON error object.
GetActionResponseText() and GetActionResponseMessage() target the action endpoint matching the calling test method's name.
GetControllerResponseText() and GetControllerResponseMessage() send requests to arbitrary application routes.
HostStop() stops the application associated with the configured port.
Example
public class UserTests : BaseTestKestrel
{
protected int Port = 41021;
public string GetUser__Action(string query)
{
return userService.GetUser(query).Json();
}
[TestMethod]
public void GetUser()
{
HostStart();
var response = GetActionResponseMessage("id=1");
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
Caveats
- Each independent test host should use a unique port.
- Classes using the same port share the same application instance.
- Action methods must return string and accept either no parameters or one string parameter.
- Missing action endpoints return HTTP 404 with a message identifying the expected action name.
- Non-success responses from the request helpers are logged but do not automatically fail the test.
- outputLogPath on HostStart() is currently only included in the console output; it does not configure file logging.