ConfirmSteps.Net is a simple library to verify the correct sequence of steps with a focus on Rest APIs.
Write a sequence of steps to confirm inside a unit test with your favorite testing framework:
[Test]
public async Task GettingStartedWithConfirmStepsNet()
{
// Arrange
const string baseUrl = "{{url}}";
Scenario<HttpScenarioData> scenario =
Scenario.New<HttpScenarioData>("Getting started with ConfirmSteps.Net")
.WithGlobals(e => e
.UseConst("url", "https://jsonplaceholder.typicode.com")
.UseObject("userId", d => d.UserId)
)
.WithSteps(s => s
// GET https://jsonplaceholder.typicode.com/users/1
.HttpStep("[Step-001-User-Read]",
() => RequestBuilder.Get(baseUrl).AppendPathSegments("users", "{{userId}}"),
step => step
.VerifyJson((response, _) =>
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
})
.Extract(e => e
.ToVars("website", FromJsonBodyToString("$.website"))
)
)
.WaitStep(100, 200)
// POST https://jsonplaceholder.typicode.com/todos
.HttpStep("[Step-002-Todo-Add]",
() => RequestBuilder.Post(baseUrl).AppendPathSegment("todos")
.WithHeaders(h => h
.Header(HeaderNames.ContentType, MediaTypeNames.Application.Json)
)
.WithBody(
"""
{
"userId": {{userId}},
"title": "Update home page of {{website}}",
"completed": false
}
"""
),
step => step
.VerifyJson((response, _) =>
{
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
Assert.AreEqual("Update home page of hildegard.org",
response.SelectString("$.title"));
})
.Extract(e => e
.ToData(d => d.ToDoId!, FromJsonBodyToNumber("$.id"))
)
)
)
.Build()
;
HttpScenarioData data = new()
{
UserId = 1,
};
// Act
ConfirmStepResult<HttpScenarioData> confirmResult =
await scenario.ConfirmSteps(data, CancellationToken.None);
// Assert
Assert.AreEqual(ConfirmStatus.Success, confirmResult.Status);
Assert.AreEqual(201, confirmResult.Data.ToDoId);
}
First, install NuGet. Then, install ConfirmSteps.Net from the package manager console:
PM> Install-Package ConfirmSteps.Net
Or from the .NET CLI as:
dotnet add package ConfirmSteps.Net
If you're still running into problems, file an issue above.
ConfirmSteps.Net is Copyright © 2023 Grégory Célet and other contributors under the MIT license.