Testing a Laravel REST API with Sample Data in a CSV File

in #utopian-io6 years ago (edited)

Repository

https://github.com/programarivm/laravel-api-ddtd-demo

premier-league-logo.png

So you have to write a brand new API from scratch and don't know how to start off? If that sounds like you, keep reading!

I've got some good news. There's no need to scratch your head any more. Today I'd like to encourage you to stick with a step-by-step methodology to build your API.

This post is similar to that one entitled A Data-Driven Test Development (DDTD) Approach with PHPUnit which I published on my blog on a different occasion. However, this one is intended to Laravel developers rather than to Symfony developers.

If you're a Python, Ruby, JavaScript, or any other language or framework programmer, my Laravel repo will hopefully help you understand the important general testing idea as well.

testing.jpg

Technology Stack

PHP 7.2
Laravel 5.7
MySQL 5.7
NGINX 1.15
Docker 18.06 CE

Please read the README.md file to learn how to get the app running with Docker.

Let's Take a Data-Driven Test Development Approach

programarivm/laravel-api-ddtd-demo is a handy boilerplate implementing a data-driven test development (DDTD) approach from minute one, ready to be forked. It is a REST API authenticated with JWT that performs CRUD operations on the UK Premier League.

The repo is, well, still a demo, meaning it is quite basic – contributions are welcome! The important thing though is the methodology being used.

This is the very first series of commits I made on Oct 19, 2018:

  • first commit
  • Docker setup
  • first Laravel setup
  • Frontend scaffolding removed successfully
  • team/create/HttpStatus201Test.php fails
  • team/create/HttpStatus201Test.php passes

From then on I decided to continue the project by sending PRs instead of doing git push origin master.

Note that apart from not being too relevant in terms of the testing methodology, the first four commits are pretty self-explanatory: first commit, Docker setup, first Laravel setup and Frontend scaffolding removed successfully.

So, let's say that in the beginning we want to program the /team/create endpoint. We start our API by writing some sample data, and then making the corresponding test fail:

The next step consists in refactoring the code until the test passes -- and the data in laravel-api-ddtd-demo/tests/team/create/data/http_status_201.json is stored into the database:

Remember, this is how to run the tests according to the README.md:

docker exec -it --user 1000:1000 football_php_fpm php artisan migrate
docker exec -it --user 1000:1000 football_php_fpm php artisan db:seed --class=UsersTableSeeder
docker exec -it --user 1000:1000 football_php_fpm php vendor/bin/phpunit

api.jpg

Roadmap

The step-by-step methodology I mentioned in the beginning of this post consists in completing a TDD cycle as the described below.

  • Add a test
  • Write something in the test
  • Let it fail
  • Refactor the code
  • Make the test pass

As you can see, we are using sample data in CSV format, and we interact with our API through Laravel's HTTP Tests.

The same cycle goes for developing more API endpoints:

  • /team/{season}
  • /team/update/{id}
  • /team/delete/{id}
  • /auth

Here are the PRs implementing the endpoints above through a DDTD cycle:

It is always the same process taking place, so feel free to add more endpoints or test more HTTP status codes if you like this project.

How to Contribute

  • Feel free to send a pull request
  • Drop an email at info@programarivm.com with the subject "Laravel API DDTD Demo"
  • Leave me a comment on Twitter
  • Say hello on Google+

Thanks so very much.

GitHub Account

https://github.com/programarivm

Sort:  

Thank you for your contribution. A very well written post. Is it only for Lavarel Rest Api or it can be used with any API, because all REST API needs sample data for testing. It would better if you write concise commits, also comments in your code.


Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hi @codingdefined, thanks for the review.

It can be used with any API. Here is an example for Symfony apps:

https://github.com/programarivm/data-driven-test-development-demo

Thank you for your review, @codingdefined! Keep up the good work!

Hi @programarivm!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Congratulations @programarivm! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

You can upvote this notification to help all Steem users. Learn how here!