Assertions
Learn how to use BELLATRIX API built-in response assertion methods.
Example
[TestFixture]
public class ApiAssertionsTests : APITest
{
private ApiClientService _apiClientService;
public override void TestInit()
{
_apiClientService = App.GetApiClientService();
}
[Test]
public void AssertSuccessStatusCode()
{
var request = new RestRequest("api/Albums");
var response = _apiClientService.Get(request);
response.AssertSuccessStatusCode();
}
[Test]
public void AssertStatusCodeOk()
{
var request = new RestRequest("api/Albums");
var response = _apiClientService.Get(request);
response.AssertStatusCode(HttpStatusCode.OK);
}
[Test]
public void AssertResponseHeaderServerIsEqualToKestrel()
{
var request = new RestRequest("api/Albums");
var response = _apiClientService.Get(request);
response.AssertResponseHeader("server", "Kestrel");
}
[Test]
public void AssertExecutionTimeUnderIsUnderOneSecond()
{
var request = new RestRequest("api/Albums");
var response = _apiClientService.Get(request);
response.AssertExecutionTimeUnder(1);
}
[Test]
public async Task AssertContentTypeJson()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertContentType("application/json; charset=utf-8");
}
[Test]
public async Task AssertContentContainsAudioslave()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertContentContains("Audioslave");
}
[Test]
public async Task AssertContentEncodingUtf8()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertContentEncoding("gzip");
}
[Test]
public async Task AssertContentEquals()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertContentEquals("{\"albumId\":10,\"title\":\"Audioslave\",\"artistId\":8,\"artist\":null,\"tracks\":[]}");
}
[Test]
public async Task AssertContentNotContainsRammstein()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertContentNotContains("Rammstein");
}
[Test]
public async Task AssertContentNotEqualsRammstein()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertContentNotEquals("Rammstein");
}
[Test]
public async Task AssertResultEquals()
{
var expectedAlbum = new Albums
{
AlbumId = 10,
};
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertResultEquals(expectedAlbum);
}
[Test]
public async Task AssertResultNotEquals()
{
var expectedAlbum = new Albums
{
AlbumId = 11,
};
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertResultNotEquals(expectedAlbum);
}
[Test]
public async Task AssertCookieExists()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertCookieExists("whoIs");
}
[Test]
public async Task AssertCookieWhoIsBella()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
response.AssertCookie("whoIs", "Bella");
}
[Test]
public async Task AssertMultiple()
{
var request = new RestRequest("api/Albums/10");
var response = await _apiClientService.GetAsync<Albums>(request);
Bellatrix.Assertions.Assert.Multiple(
() => response.AssertCookie("whoIs", "Bella"),
() => response.AssertCookieExists("whoIs"));
}
}
Explanations
BELLATRIX API library brings many convenient assertion methods on top of RestSharp. Of course, you can write similar methods yourself using MSTest or NUnit. All BELLATRIX assertions comes with full BDD logging and extensibility hooks.
response.AssertSuccessStatusCode();
Assert that the status code is successful.
response.AssertStatusCode(HttpStatusCode.OK);
Assert that the status code is OK.
response.AssertResponseHeader("server", "Kestrel");
Assert that the header named ‘server’ has the value ‘Kestrel’.
response.AssertExecutionTimeUnder(1);
Assert that the execution time of the GET request is under 1 second.
response.AssertContentType("application/json; charset=utf-8");
Assert that the content type is of the specified type.
response.AssertContentContains("Audioslave");
Assert that the native text content (JSON or XML) contains the specified value.
response.AssertContentEncoding("gzip");
Assert the response’s content encoding.
response.AssertContentEquals("{\"albumId\":10,\"title\":\"Audioslave\",\"artistId\":8,\"artist\":null,\"tracks\":[]}");
Assert the native text content.
response.AssertContentNotContains("Rammstein");
Assert that the native text content doesn’t contain specific text.
response.AssertContentNotEquals("Rammstein");
Assert that the native text content is not equal to a specific text.
response.AssertResultEquals(expectedAlbum);
Assert C# collections directly.
response.AssertResultNotEquals(expectedAlbum);
Assert response is not equal to an object.
response.AssertCookieExists("whoIs");
Assert that a specific cookie exists.
response.AssertCookie("whoIs", "Bella");
Assert that a cookie’s value is equal to a specific value.
Bellatrix.Assertions.Assert.Multiple(
() => response.AssertCookie("whoIs", "Bella"),
() => response.AssertCookieExists("whoIs"));
You can execute multiple assertions failing only once viewing all results.