rest api testing with specflow
DESCRIPTION
Introduction to using SpecFlow tool for testing REST API. For beginners that are at least a bit familiar with test automation, and gives some details and hints.TRANSCRIPT
![Page 1: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/1.jpg)
REST API testing with SpecFlowAistė Stikliūtė @ Visma Lietuva
![Page 2: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/2.jpg)
Agenda
Why SpecFlow REST API How to
Details & tips Live demo
Bonus:other uses
of SpecFlow
![Page 3: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/3.jpg)
Why SpecFlow• SoapUI?
• Fitnesse?
• Cucumber?
![Page 4: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/4.jpg)
Why not…
SoapUI
Free version issues
Tests less easy to read
Thought there was no CI
Fitnesse
Works poorly with .NET
Wiki markup and tables interface is tiring
Cucumber
Yes – SpecFlow is Cucumber for .NET!
![Page 5: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/5.jpg)
Continuous integration
![Page 6: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/6.jpg)
BDD / Gherkin language
GIVEN book with ISBN “1-84356-028-3” is in the systemAND it’s available quantity is 9WHEN I add a book with ISBN “1-84356-028-3”THEN the book is successfully added to the listAND available qty. for book with ISBN “1-84356-028-3” is 10
![Page 7: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/7.jpg)
The environment
Same tool as for GUI
tests (Visual Studio, C#)
Same solution as GUI tests and even the whole
system
Convenient!
Developers are
integrated!
![Page 8: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/8.jpg)
Rest API• What it is
• Examples
• Why test it
![Page 9: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/9.jpg)
REST API
Web architectural style with a set of constraints
Web service APIs that adhere to the constraints - RESTful
Frontend
Backend
REST APIExternal
system(s)External system(s)
![Page 10: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/10.jpg)
Rest API: typically used HTTP methods
Resource GET PUT POST DELETE
Collection URI,such as http
://example.com/resources
Listcollection's members
Replacecollection with another collection
Create new entry in the collection
Delete the entire collection
Element URI,such as http
://example.com/resources/item17
Retrievethe member of the collection
Replace the member of the collection
Not generally used
Delete the member of the collection
![Page 11: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/11.jpg)
Example 1
Request
GET https://eshop.com/books/14765
Response
Status: 200 OK
{ "id": “14765", “title": “Game of Thrones", “author": “George R. R. Martin", “isbn": "1-84356-028-3", “availableQty": “4"}
![Page 12: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/12.jpg)
Example 2
Request
POST https://eshop.com/books
{ “title": “501 Spanish Verbs", “author": “C. Kendris", “isbn": "1-84750-018-7", “availableQty": “10"}
Response
Status: 200 OK
{ “id": “78953“}
![Page 13: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/13.jpg)
Example 3
Requests
DELETE https://eshop.com/book/84523
GET https://eshop.com/admin
PUT https://eshop.com/book/24552
{ [some wrong data] }
Responses
Status: 200 OK
Status: 401 Unauthorized
Status: 500 Internal Server Error
![Page 14: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/14.jpg)
Why test Rest API?
If used by external applications: this is your UI!
As your system layer: Can help find / isolate problems:
Security
Performance
Robustness
Functionality
May be more simple to test / automate than GUI tests
![Page 15: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/15.jpg)
How to……write tests
![Page 16: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/16.jpg)
Step 1: know what your API should do
Documentation
Talk to developers
Browser’s Developer tools
REST client (e.g. Postman on Chrome)
![Page 17: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/17.jpg)
Dev tools + REST client
![Page 18: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/18.jpg)
Step 2: write your test scenarios
ListBooks.feature
Scenario: There are no books in the systemGiven there are no books in the systemWhen I retrieve list of all booksThen I get empty list
Scenario: There are less books than fit into 1 pageScenario: There are more books than fit into 1 pageScenario: Sort booksScenario: Search for a book
![Page 19: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/19.jpg)
Step 3: generate scenario steps
[Given(@”there are no books in the list”)]public void GivenThereAreNoBooksInTheList(){
ScenarioContext.Current.Pending();}
![Page 20: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/20.jpg)
Step 4: implement scenario steps
Here‘s where the Rest API calls go!
Plain C# can be used or libraries // I use RestSharp
Structure your project
![Page 21: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/21.jpg)
Project structure
Features: all features, can have subfolders
Steps: bindings of features to actions
Actions: where things happen
Helpers: among others, RestHelper.cs
Model: classes matching our API data format
App.config: holds base URI
![Page 22: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/22.jpg)
A quick look into code: Steps & ActionsSteps
Actions
![Page 23: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/23.jpg)
A quick look into code: Model
![Page 24: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/24.jpg)
A quick look into code: RestHelper.cs
![Page 25: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/25.jpg)
Step 5: run tests
![Page 26: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/26.jpg)
Step 6: add to Continuous Integration
![Page 27: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/27.jpg)
Details & Tips
![Page 28: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/28.jpg)
Hooks (event bindings)
Before / after test run static
Before / after feature static
Before / after scenario
Before / after scenario block (given / when / then)
Before / after step
![Page 29: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/29.jpg)
Step scope and reusing
Steps are global! Naming: “when I update it” “when I update the book”
Scoped bindings:
[Scope(Tag="my tag", Feature=“my feature", Scenario=“my scenario")]
Reusing step in other steps
[Given(@"(.*) is logged in")]public void GivenIsLoggedIn(string name) { Given(string.Format("the user {0} exists", name)); Given(string.Format("I log in as {0}", name));}
![Page 30: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/30.jpg)
Table and multiline step arguments
![Page 31: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/31.jpg)
Live Demo
![Page 32: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/32.jpg)
BonusOther uses of SpecFlow
![Page 33: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/33.jpg)
Behaviour /Business Driven Development
1. PO / QA writes scenarios
2. Developer writes unit/integration tests
3. Developer writes code until tests pass
BDD
![Page 34: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/34.jpg)
Driving Selenium tests
Scenario: Successful loginGiven I am in login pageWhen I login with correct
credentialsThen I am redirected to main page
Scenario: Successful loginGiven I am at http://eshop.com/loginWhen I login with user “John” and password
“Password1”Then I am redirected to http://eshop.com/main
![Page 35: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/35.jpg)
SpecFlow & Seleniumusing scoped bindings
[When(@"I perform a simple search on '(.*)'", Scope(Tag = “api"))]public void WhenIPerformASimpleSearchOn(string searchTerm) {
var api = new CatalogApi();actionResult = api.Search(searchTerm);
}
[When(@"I perform a simple search on '(.*)'"), Scope(Tag = "web")]public void PerformSimpleSearch(string title) {
selenium.GoToThePage("Home"); selenium.Type("searchTerm", title);
selenium.Click("searchButton");}
![Page 36: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/36.jpg)
Test case management / Test reporting
One functional test suite? Manual
Automated API
Automated GUI
One report with test list that can be read by POs and manual QAs
management
customer
![Page 37: REST API testing with SpecFlow](https://reader037.vdocuments.us/reader037/viewer/2022102500/5485fa79b4af9fb55d8b4ab3/html5/thumbnails/37.jpg)
Thank you!Time for questions