Reader small image

You're reading from  Building RESTful Web Services with PHP 7

Product typeBook
Published inSep 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787127746
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Waheed ud din
Waheed ud din
author image
Waheed ud din

Haafiz Waheed-ud-din Ahmad has been working in the IT industry since 2008. He has mostly worked in web application development and mostly used PHP at the server side. Although most of his experience is in PHP, he is a technology agnostic person and also likes to learn and adapt to new technologies. He also acts as an adviser for startups and new developers. He has worked on Python and JavaScript as well. He likes to experiment with new technologies, and he has also explored Golang, Scala, and Neo4J. He also has a keen interest in data science and big data domain and has worked on D3.js for data visualization. He is not just a technology enthusiast but also likes to solve day-to-day problems by the usage of technology. You can follow him on twitter at @Haafiz786.
Read more about Waheed ud din

Right arrow

Chapter 8. API Testing – Guards on the Gates

In the last chapter, we fixed the issues that we had identified and completed the remaining things in our RESTful web service. However, to ensure quality we need to test our endpoints, and manual testing is not enough. In real-world projects, we can't test each endpoint repeatedly because in the real world there are a lot more endpoints. So, we move towards automated testing. We write test cases and execute them in an automated way. In fact, it makes more sense to write test cases first, run them, and then write code to fulfill the requirements that test. This method of development is called TDD (Test-driven Development).

TDD is good and ensures that we are working exactly according to our test cases. However, in this book, we didn't use TDD because there were a lot of things to understand and we didn't want to include one more thing at the same time. So now, when we are done with the concepts, understanding, and writing the RESTful web service...

The need for automated tests


As we discussed earlier, in the real world, we can't test every repeatedly after every major feature or change. We can try but we are human and we can miss out on that. The bigger problem is that we may sometimes think that we tested it but miss it and because there is no record of what we tested, we can't know. If we have a separate quality assurance team or person, they will most probably test and keep a record of that. However, in case of a RESTful web service, it will take more of their time or the possibility is that the QA person will test the end product as a whole and not RESTful web service.

Just like the RESTful web service works as one component or one side of a product, there are more low-level components of the RESTful web service as well. Not just endpoints but those endpoints depends on more low-level code. So in order to make our debugging easier, we write tests for these low-level components as well. Also, this way we can ensure that these low...

Types of testing


There are different types of testing in different contexts. In our case, we will discuss major types of automated testing. These are the different types, based on how and what we test:

  • Unit testing
  • Integration testing
  • Functional testing
  • Acceptance testing

Unit testing

In unit testing, we test different units separately. By unit, we mean very independent components. Obviously, components depend on each other we consider a very small unit. In our case that small unit is class. The class is a unit that should have a single responsibility and it should be abstract from others and depend on the minimum number of other classes or components. Anyway, we can say in unit testing, we test class by creating an object of that class irrespective of whether it fulfills the required behavior.

One important thing to understand is that during unit tests, our test shouldn't touch code other than class/code under testing. If this code is interacting with some other object or external stuff during...

What type of testing will we do?


Every type of testing its own importance, especially unit testing. However, we will mainly do API testing that will be testing our RESTful web service endpoints. It never means that unit testing is less important, it is just that we are mainly focusing on API testing in this chapter because the book is focused on RESTful web services. In fact, testing is a big topic and you will be able to see complete books on testing and TDD.

Note

Nowadays, BDD (Behavior-driven Development) is a more popular term. It is not completely different than TDD. It is just a different way of stating test cases. In fact, in BDD, there are no test cases instead there are specs. They serve the same purpose but BDD has a more friendly way to address the problem that is by stating specs and implementing them and that's how TDD works. So TDD and BDD are not different, just a different way to address the same problem.

We can perform API testing in both functional and acceptance testing...

Testing frameworks


Just like we have frameworks for writing software, we have frameworks writing test cases as well. As we are PHP developers, we will use a testing framework that is written in PHP or in which we can write test cases in PHP so that we can use it easily.

First of all, there are different testing frameworks that we can use in PHP, no matter which development framework we are using for application development. However, there are also testing tools that come with Laravel and Lumen. We can also use them for writing test cases. In fact, it will be easier to write test cases for Lumen but it will be Lumen and Laravel specific. Here, we will use a framework that you will be able to use outside the Lumen and Laravel ecosystems as well as for any PHP project, no matter which development framework you are using to write the code.

There are many different testing frameworks in PHP so how will we decide which one we want to use? We are going to use a framework that will not be so low...

Summary


In this chapter, we learned testing types, the importance of automated testing, and wrote API tests for our RESTful web service endpoints. One thing I again want to say here is that we wrote only API tests to keep our focus on our topic but unit testing is of no less importance. However, testing is a huge topic and unit testing has its own complexity, so couldn't be discussed in this one chapter.

More resources

If you want to know more about automated testing in PHP, then here are some important resources:

Test Driven Laravel (Video course by Adam Wathan) https://adamwathan.me/test-driven-Laravel/, however this is mainly focused on Laravel. But still, this will teach you important things.

Similarly, there is Laravel Testing Decoded (an old book by Jeffrey Way) at https://leanpub.com/Laravel-testing-decoded

Again, this is a Laravel-specific book but teaches you a lot in general. There is Jeffrey Way's new upcoming book that is in general on PHP testing named Testing PHP: https://leanpub...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building RESTful Web Services with PHP 7
Published in: Sep 2017Publisher: PacktISBN-13: 9781787127746
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Waheed ud din

Haafiz Waheed-ud-din Ahmad has been working in the IT industry since 2008. He has mostly worked in web application development and mostly used PHP at the server side. Although most of his experience is in PHP, he is a technology agnostic person and also likes to learn and adapt to new technologies. He also acts as an adviser for startups and new developers. He has worked on Python and JavaScript as well. He likes to experiment with new technologies, and he has also explored Golang, Scala, and Neo4J. He also has a keen interest in data science and big data domain and has worked on D3.js for data visualization. He is not just a technology enthusiast but also likes to solve day-to-day problems by the usage of technology. You can follow him on twitter at @Haafiz786.
Read more about Waheed ud din