Writing Your First Test
This section will describe how to configure Bard in order to write your first API test. This example demonstrates the simplest way to use Bard and does not use the more advanced StoryBook feature which we will cover in more detail in this section of the documentation.

Configure The Scenario

So before we can start we need to configure our Scenario. To do this we are going to need provide two things.
    A System.Net.Http.HttpClient in order to call our API
    A function to tell our Scenario how to log. This is optional but important because Bard provides detailed logs to the test output windows to help with writing and debugging test failures. If not supplied Bard will ouput all log messages to the Console window which may not appear in your test output.
1
var scenario = ScenarioConfiguration
2
.Configure(options =>
3
{
4
options.Client = httpClient;
5
options.LogMessage = output.WriteLine;
6
});
Copied!
The GitHub repository has some example tests you can see a working example here.

Write The First Test

We've configured our Scenario now. Assuming our API is running now we can start by calling it.
So lets give it a spin by writing a test that calls our fictitious Banking API and creating a new bank account. We expect our API to return a 201 Created response.
1
[Fact]
2
public void When_creating_a_bank_account()
3
{
4
Scenario
5
.When
6
.Post("/api/bankaccount/, new BankAccount
7
{
8
CustomerName = "Kilian Jornet"
9
});
10
​
11
Scenario
12
.Then
13
.Response
14
.ShouldBe
15
.Created();
16
}
Copied!
And we're done. If we look at the output from our Test we should see some nice logging that includes the request and the response.
1
****************************************
2
* WHEN *
3
****************************************
4
​
5
REQUEST: POST api/bankaccounts
6
{
7
"id": null,
8
"isActive": false,
9
"customerName": "Kilian Jornet",
10
"balance": 0.0
11
}
12
​
13
RESPONSE: Http Status Code: Created (201)
14
Header::Location http://localhost/api/bankaccounts/1
15
{
16
"id": 1,
17
"isActive": false,
18
"customerName": "Kilian Jornet",
19
"balance": 0.0
20
}
Copied!
​
Last modified 1yr ago
Export as PDF
Copy link