Slide 1

Slide 1 text

@cmaiacd ScanAPI Automated Integration Testing and Live Documentation for your API Tech Talk @ Red Hat - October 2020 Camila Maia

Slide 2

Slide 2 text

@cmaiacd You find this presentation at: speakerdeck.com/cmaiacd

Slide 3

Slide 3 text

@cmaiacd Motivation Everything started in a week which I as Firefighter

Slide 4

Slide 4 text

@cmaiacd I work at...

Slide 5

Slide 5 text

@cmaiacd Who am I? - Brazilian Backend Developer - Bachelor of Computer Information System - Coding since 2010 - Python and Ruby - Events: Pyjamas, EuroPython, Python Brasil...

Slide 6

Slide 6 text

@cmaiacd Motivation Everything started in a week which I as Firefighter

Slide 7

Slide 7 text

@cmaiacd Motivation Integration Errors - Client sending fields different than what’s expected - Frontend receiving fields different than what’s expected

Slide 8

Slide 8 text

@cmaiacd Motivation - Missing endpoints - Missing fields - Misinformation Outdated documentation

Slide 9

Slide 9 text

@cmaiacd Motivation /reserve/:seat_id flight_id? airplane_id? passenger_id? Is the airplane available? ???? Hard to recreate scenarios

Slide 10

Slide 10 text

@cmaiacd Motivation Hard to recreate scenarios

Slide 11

Slide 11 text

@cmaiacd

Slide 12

Slide 12 text

@cmaiacd Proposal - Open Source Framework - Command Line Interface (CLI) - Written in Python 1. Provide a Live Documentation 2. Tool to implement Integration Tests

Slide 13

Slide 13 text

@cmaiacd PokéAPI: https://pokeapi.co $ http https://pokeapi.co/api/v2/pokemon/ How does it work? Example: PokéAPI

Slide 14

Slide 14 text

@cmaiacd

Slide 15

Slide 15 text

@cmaiacd

Slide 16

Slide 16 text

@cmaiacd How does it work? Installing: $ pip install scanapi Example: PokéAPI

Slide 17

Slide 17 text

@cmaiacd # scanapi.yaml endpoints: - name: pokeapi path: https://pokeapi.co/api/v2/ endpoints: - name: pokemon path: pokemon requests: - name: list_all method: get path: / How does it work? Example: PokéAPI path: https://pokeapi.co/api/v2/ pokemon / name: pokeapi :: pokemon :: list_all

Slide 18

Slide 18 text

@cmaiacd How does it work? Running Example: PokéAPI

Slide 19

Slide 19 text

@cmaiacd How does it work? Documentation

Slide 20

Slide 20 text

@cmaiacd

Slide 21

Slide 21 text

@cmaiacd

Slide 22

Slide 22 text

@cmaiacd ... requests: - name: list_all method: get tests: - name: status_code_is_200 assert: ${{ response.status_code == 200 }} - name: response_time_is_under_half_second assert: ${{ response.elapsed.total_seconds() < 0.5 }} - name: results_length_is_20 assert: ${{ len(response.json()["results"]) == 20 }} - name: count_is_gte_1050 assert: ${{ response.json()["count"] >= 1050 }} How does it work? https://requests.readthedocs.io Integration tests ✅

Slide 23

Slide 23 text

@cmaiacd

Slide 24

Slide 24 text

@cmaiacd How does it work? Chaining Requests ⛓ - Get details of a Pokémon - In this case, the data is “static”, but it might not be. https://pokeapi.co/api/v2/pokemon/bulbasaur

Slide 25

Slide 25 text

@cmaiacd How does it work? ... endpoints: - name: pokemon path: pokemon requests: - name: list_all method: get vars: pokemon_name: ${{ response.json()["results"][0]["name"] }} tests: ... - name: details method: get path: ${pokemon_name} pokemon/${pokemon_name}

Slide 26

Slide 26 text

@cmaiacd

Slide 27

Slide 27 text

@cmaiacd How does it work? And there is more - Language-independent - It also accepts API spec in JSON - Environment variables - Hide sensitive info in the report - Multiple files API specification - Delay between requests - Custom templates - VS Code Plugin

Slide 28

Slide 28 text

@cmaiacd Adding ScanAPI to your project - CIs - GitHub Action

Slide 29

Slide 29 text

@cmaiacd Adding ScanAPI to a project

Slide 30

Slide 30 text

@cmaiacd Adding ScanAPI to a project Example: CircleCI

Slide 31

Slide 31 text

@cmaiacd # my_project/.circleci/config.yml ... scanapi: docker: - image: camilamaia/scanapi:2.1.0 steps: - checkout - run: name: Run ScanAPI command: | scanapi run scanapi/scanapi.yaml -c scanapi/scanapi.conf -o scanapi/report.html - store_artifacts: path: scanapi/report.html workflows: version: 2 main: jobs: - scanapi: filters: branches: only: - master - build-push: name: build-push-staging env: staging requires: - scanapi filters: branches: only: - master

Slide 32

Slide 32 text

@cmaiacd Adding ScanAPI to a project Example: GitHub Action

Slide 33

Slide 33 text

@cmaiacd # my_project/.github/workflows/scanapi.yml on: push: branches: [master] name: Run ScanAPI jobs: scanapi: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Run automated API tests uses: scanapi/github-action@v1 with: scanapi_version: "==2.1.0" arguments: "run ./pokeapi/scanapi.yaml" - name: Upload scanapi-report.html uses: actions/upload-artifact@v2 with: name: ScanAPI Report path: scanapi-report.html

Slide 34

Slide 34 text

@cmaiacd scanapi.dev

Slide 35

Slide 35 text

@cmaiacd Next Steps What about the future? - Missing HTTP methods (current: GET, POST, PUT, PATCH, DELETE) - Improve Documentation - Create a Tutorial - Improve Contributing Documentation And what if we could…. - Export to: OpenAPI, Postman Collection, Insomnia… - Call python modules

Slide 36

Slide 36 text

@cmaiacd - GitHub Issues (per project) - Dashboards. Ex: Hacktoberfest - Spectrum (events, sprints...) Communication Let’s get in touch!

Slide 37

Slide 37 text

@cmaiacd github.com/scanapi ⭐

Slide 38

Slide 38 text

@cmaiacd THANK YOU! @cmaiacd camilamaia ‍ cmaiacd.com