Test plan,test cases with Arrange-Act-Assert pattern
AAA or Arrange-Act-Assert is the industry standard pattern used to write test cases. So most test cases will contain these three sections of code- Arrange, Act, Assert.
Arrange section is where you write to setup the test. This is where you setup all the pre-requisites before the actual test is run. For example, if your test expects specific data to exist in the database, you will need to setup the database with the exact data.
Act is the section where perform the actions for the test. For example for a UI test this could be clicking on a button. For an Http REST API test, this could be making the actual API request.
Assert is the section where you make sure the test ran with the right results. This is where you verify that the test came back with the data you expected. For example for an UI application, you would check that the color of a box changed when you clicked the button. For an API test, this will be where you verify that the HTTP response came back with a 200 HTTP status code.
Testing API Routes
GET /todos
1.0 When there are no todos in the database, and then I add a todo item, GET /todos API response body should have an array of length 1
it('1.0 When there are no todos in the database, and then I add a todo item, GET /todos API response body should have an array of length 1', () => {
//Arrange
deleteAll()
var todoItem = {'id': '105', 'task':'first task'}
addItem(todoItem)
//Act
getItems()
//Assert
.should('have.length',1)
});
1.1 When there are no todos in the database, and then I add a todo item, GET /todos API response should include the item that I added
it('1.1 when there are no todos and then i add a todo item, GET /todos should list the item that i added', () => {
//Arrange
deleteAll()
var todoItem = {'id': '105', 'task':'first task'}
addItem(todoItem)
//Act
getItems()
.each(item => {
//Assert
cy.wrap(item).should('deep.equal', todoItem)
})
});
1.2 when there are no todos, GET /todos API response body should return an array of zero length
it('1.2 when there are no todos, GET /todos should return a list of zero length', () => {
//Arrange
deleteAll()
//Act
getItems()
//Assert
.should('have.length', 0)
});
1.3 When a todo item is available, GET /todos should return status code of 200
it('1.3 When a todo item is available, GET /todos should return status code of 200', () => {
//Arrange
deleteAll()
var todoItem = {'id': '105', 'task':'first task'}
addItem(todoItem)
//Act
cy.request('/todos')
.then((response) => {
//Assert
expect(response.status).to.eq(200)
})
});
GET /todos/
2.0 When I have a todo item with id 105 in the database, GET /todos/105 API response should have the item with id 105
it('2.0 When I have a todo item with id 105 in the database, GET /todos/105 API response should have the item with id 105', () => {
//Arrange
deleteAll()
var todoItem = {'id': '105', 'task':'first task'}
addItem(todoItem)
//Act
getItem('105')
.its('body')
//Assert
.should('deep.equal', todoItem)
});
2.1 A GET /todos/ with an id that is not present in the database, should return a 404 status code
it('2.1 A GET /todos/<id> with an id that is not present in the database, should return a 404 status code', () => {
//Arrange
deleteAll()
var todoItem = {'id': '105', 'task':'first task'}
cy.request('POST', '/todos', todoItem)
//Act
cy.request({
method: 'GET',
url: '/todos/107',
failOnStatusCode: false
})
.then(response => {
//Assert
expect(response.status).to.eq(404)
expect(response.status).not.eql(200)
})
});
The code for this is available in https://github.com/swiftparrot/todos-api-test-automation