REST API testing with SpecFlow

download REST API testing with SpecFlow

If you can't read please download the document

  • date post

  • Category


  • view

  • download


Embed Size (px)


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 of REST API testing with SpecFlow

  • 1. REST API testing with SpecFlow Aist Stiklit @ Visma Lietuva
  • 2. Agenda Why SpecFlow REST API How to Details & tips Live demo Bonus: other uses of SpecFlow
  • 3. Why SpecFlow SoapUI? Fitnesse? Cucumber?
  • 4. 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!
  • 5. Continuous integration
  • 6. BDD / Gherkin language GIVEN book with ISBN 1-84356-028-3 is in the system AND its available quantity is 9 WHEN I add a book with ISBN 1-84356-028-3 THEN the book is successfully added to the list AND available qty. for book with ISBN 1-84356-028-3 is 10
  • 7. 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!
  • 8. Rest API What it is Examples Why test it
  • 9. REST API Web architectural style with a set of constraints Web service APIs that adhere to the constraints - RESTful Frontend REST API Backend External system(s) External system(s)
  • 10. Rest API: typically used HTTP methods Resource GET PUT POST DELETE Collection URI, such as ources List collection's members Replace collection with another collection Create new entry in the collection Delete the entire collection Element URI, such as ources/item17 Retrieve the member of the collection Replace the member of the collection Not generally used Delete the member of the collection
  • 11. Example 1 Request GET Response Status: 200 OK { "id": 14765", title": Game of Thrones", author": George R. R. Martin", isbn": "1-84356-028-3", availableQty": 4" }
  • 12. Example 2 Request POST { title": 501 Spanish Verbs", author": C. Kendris", isbn": "1-84750-018-7", availableQty": 10" } Response Status: 200 OK { id": 78953 }
  • 13. Example 3 Requests DELETE GET PUT { [some wrong data] } Responses Status: 200 OK Status: 401 Unauthorized Status: 500 Internal Server Error
  • 14. 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
  • 15. How to write tests
  • 16. Step 1: know what your API should do Documentation Talk to developers Browsers Developer tools REST client (e.g. Postman on Chrome)
  • 17. Dev tools + REST client
  • 18. Step 2: write your test scenarios ListBooks.feature Scenario: There are no books in the system Given there are no books in the system When I retrieve list of all books Then I get empty list Scenario: There are less books than fit into 1 page Scenario: There are more books than fit into 1 page Scenario: Sort books Scenario: Search for a book
  • 19. Step 3: generate scenario steps [Given(@there are no books in the list)] public void GivenThereAreNoBooksInTheList() { ScenarioContext.Current.Pending(); }
  • 20. Step 4: implement scenario steps Heres where the Rest API calls go! Plain C# can be used or libraries // I use RestSharp Structure your project
  • 21. 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
  • 22. A quick look into code: Steps & Actions Steps Actions
  • 23. A quick look into code: Model
  • 24. A quick look into code: RestHelper.cs
  • 25. Step 5: run tests
  • 26. Step 6: add to Continuous Integration
  • 27. Details & Tips
  • 28. 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
  • 29. 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)); }
  • 30. Table and multiline step arguments
  • 31. Live Demo
  • 32. Bonus Other uses of SpecFlow
  • 33. Behaviour / Business Driven Development 1. PO / QA writes scenarios 2. Developer writes unit/integration tests 3. Developer writes code until tests pass
  • 34. Driving Selenium tests Scenario: Successful login Given I am at When I login with user John and password Password1 Then I am redirected to Scenario: Successful login Given I am in login page When I