Automated Tests in a REST API
by Luís Nóbrega
Regression Tests - benefits
Fast feedback
Discover introduced software bugs (regression)
Verify bug fixes on existing functionality
Validate changes against other components
Include re-running of previous tests
Can be executed on each code change
Regression Tests - when to do it?
Specification has changed
Code has been changed
Add new test cases - increase
coverage
New bugs were detected
Create a new test that reproduces the
bug.
Regression Tests - State of the Art
Unit Tests - Test Driven Development (TDD) and Behavior
Driven Development (BDD)
Tools: PHPUnit and phpspec
Acceptance Tests
Tool: Behat
Unit Tests
Must run successfully on each change.
Tests should be written with/before code. We send inputs and verify outputs.
All dependencies are mocked. No hitting the database, file system or network.
Tests written by developers for developers.
Benefits
Allow safe refactoring of code parts.
It can work as documentation.
Gives higher confidence.
Unit Tests - When to Create?
Always, if it makes sense.
When you are fixing a bug. Reproduce it with a test.
When implementing new features or when specification has
changed.
Check code coverage report to add new test cases
Unit Tests - How to execute?
PHPUnit
Execute: bin/phpunit -c app/
phpspec
Execute: bin/phpspec run
Create (describe) a new spec: bin/phpspec desc ‘<namespace>’
Execute with code coverage: bin/phpspec run --config=<config-file>.yml
Code coverage will be exported to build/coverage folder
Acceptance Tests - Behat
Verify the functional requirements
Ensure that the overall application is working
Test specification provided
Prevent regression problems
“Behat is an open source behavior-driven development
framework (...) What is behavior-driven development, you ask?
It’s the idea that you start by writing human-readable sentences
that describe a feature of your application and how it should
work, and only then implement this behavior in software.”
Behat - Database for test environment
Two concerns: performance and isolation
Performance
Isolation: each test data is only visible for that test.
How to setup the database before each test? Approaches:
Obelix doctrine commands - 3 minutes
Drop, create and add only structure using entities
MySQL via command line - 1 minute
Behat - Database data fixtures
app/config/automated_tests.yml has data fixtures config with tables and data
files
Data files must exist in app/data/test/mysql
In the scenario we should add a tag with the data fixture we need
Basic authentication is always inserted in the database
Behat - Lifecycle request
REST API
in test environment
image: Symfony lifecycle request
Behat
sends HTTP request
Behat
asserts HTTP response
Feature Scenario
Before Scenario hook
setup database:
- create an database dump
- drop test database
- create test database
- import dump
- import data
Given - precondition
When - action
Then - testable outcome
is achieved
runs iteratively
Loop over features -> loop over scenarios of the feature
Behat
Event system
The future - Continuous Integration
Triggers test chain on each pull request
Verifies state before merge
Using the GitHub API to validate pull-request even before they are merged!
https://developer.github.com/v3/repos/statuses/
Continuous Integration Best Practices
(...)
Make the build self-testing
Once the code is built, all tests should run to confirm
that it behaves as the developers expect it to behave.
Lessons learned - Benefits and Difficulties
Commitment to TDD needed
Testing influences software design
Testing takes time
Full test coverage is almost unachievable
Testing is needed to ensure software quality
Questions?
Thanks

Automated tests to a REST API

  • 1.
    Automated Tests ina REST API by Luís Nóbrega
  • 2.
    Regression Tests -benefits Fast feedback Discover introduced software bugs (regression) Verify bug fixes on existing functionality Validate changes against other components Include re-running of previous tests Can be executed on each code change
  • 3.
    Regression Tests -when to do it? Specification has changed Code has been changed Add new test cases - increase coverage New bugs were detected Create a new test that reproduces the bug.
  • 4.
    Regression Tests -State of the Art Unit Tests - Test Driven Development (TDD) and Behavior Driven Development (BDD) Tools: PHPUnit and phpspec Acceptance Tests Tool: Behat
  • 5.
    Unit Tests Must runsuccessfully on each change. Tests should be written with/before code. We send inputs and verify outputs. All dependencies are mocked. No hitting the database, file system or network. Tests written by developers for developers. Benefits Allow safe refactoring of code parts. It can work as documentation. Gives higher confidence.
  • 6.
    Unit Tests -When to Create? Always, if it makes sense. When you are fixing a bug. Reproduce it with a test. When implementing new features or when specification has changed. Check code coverage report to add new test cases
  • 7.
    Unit Tests -How to execute? PHPUnit Execute: bin/phpunit -c app/ phpspec Execute: bin/phpspec run Create (describe) a new spec: bin/phpspec desc ‘<namespace>’ Execute with code coverage: bin/phpspec run --config=<config-file>.yml Code coverage will be exported to build/coverage folder
  • 8.
    Acceptance Tests -Behat Verify the functional requirements Ensure that the overall application is working Test specification provided Prevent regression problems “Behat is an open source behavior-driven development framework (...) What is behavior-driven development, you ask? It’s the idea that you start by writing human-readable sentences that describe a feature of your application and how it should work, and only then implement this behavior in software.”
  • 9.
    Behat - Databasefor test environment Two concerns: performance and isolation Performance Isolation: each test data is only visible for that test. How to setup the database before each test? Approaches: Obelix doctrine commands - 3 minutes Drop, create and add only structure using entities MySQL via command line - 1 minute
  • 10.
    Behat - Databasedata fixtures app/config/automated_tests.yml has data fixtures config with tables and data files Data files must exist in app/data/test/mysql In the scenario we should add a tag with the data fixture we need Basic authentication is always inserted in the database
  • 11.
    Behat - Lifecyclerequest REST API in test environment image: Symfony lifecycle request Behat sends HTTP request Behat asserts HTTP response Feature Scenario Before Scenario hook setup database: - create an database dump - drop test database - create test database - import dump - import data Given - precondition When - action Then - testable outcome is achieved runs iteratively Loop over features -> loop over scenarios of the feature
  • 12.
  • 13.
    The future -Continuous Integration Triggers test chain on each pull request Verifies state before merge Using the GitHub API to validate pull-request even before they are merged! https://developer.github.com/v3/repos/statuses/ Continuous Integration Best Practices (...) Make the build self-testing Once the code is built, all tests should run to confirm that it behaves as the developers expect it to behave.
  • 14.
    Lessons learned -Benefits and Difficulties Commitment to TDD needed Testing influences software design Testing takes time Full test coverage is almost unachievable Testing is needed to ensure software quality
  • 15.
  • 16.

Editor's Notes

  • #4 https://github.com/phpspec/phpspec/issues/269
  • #11 demonstrar no IDE: feature files data fixtures automated_tests.yml file tags, comando behat_profiles.yml