Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform integration testing in PHP?

I am currently performing unit tests on my code (using PHPUnit and Jenkins) but I have read a lot about integration testing.

  • Are there any tools to perform this in php (preferably automated)?

  • How would I go about implementing it? Are there any good tutorials anywhere?

like image 919
JoshB Avatar asked Jun 28 '11 18:06

JoshB


People also ask

What is PHP integration testing?

Integration testing is a method of testing by passing in the real dependencies and thus testing out the integration between two or more objects.

Which tool is used for integration testing?

Citrus. It is the most commonly used integration testing tool, which is a test framework and written in the Java programming language.

What is integration testing with example?

This type of testing follows the natural control flow hierarchy, i.e., top to bottom. For example, you have a fitness app with four modules – A login page, a profile page, a workout page, and a payment page. The testing of the application will start from the crucial top-level module.


2 Answers

Many years later... there's Codeception framework now that you can use to do Unit, Integration and Functional tests.

Codeception uses PHPUnit as a backend for running it's tests. Thus, any PHPUnit test can be added to a Codeception test suite.

In unit tests, you would mock database access, file system, HTTP Requests and other components to isolate code and make it faster.
Integration tests doesn’t require the code to be executed in isolation, that means you will be using those components for real and check the output/results for what was expected.

To ilustrate, take a look at this integration test example from Codeception DOC:

<?php function testSavingUser() {     $user = new User();     $user->setName('Miles');     $user->setSurname('Davis');     $user->save();     $this->assertEquals('Miles Davis', $user->getFullName());     $this->tester->seeInDatabase('users', ['name' => 'Miles', 'surname' => 'Davis']); } 
like image 64
Edson Horacio Junior Avatar answered Sep 23 '22 00:09

Edson Horacio Junior


Basically the way to go is to implement besides unit tests also mock tests which are not solely testing a single unit more like a group of units bunched together and you see them as a logical unit which should behave in a certain way while handing in some input or calling methods.

One possible library for this is yaymock in the google code repository. Its a php5 mock library.

Further integration tests are more or less only tests which test the complete system behavior. The basic thing is setting a test environment up and deploy your application afterwards. You can do this kind of testing also with a unit test framework or a mock library. As you wish. Integration tests in detail in your case are http requests, based on some data in your database and an expected possible "html" output.

To automate this you can use some continous integration frameworks... either Hudson, Arbit or phpUnderControl. For setting up php with hudson and some nice testing plugins there is a pretty good tutorial. It mentions also some useful plugins like Code-Coverage checks, etc ... which could be integrated inside the environment.

like image 38
fyr Avatar answered Sep 27 '22 00:09

fyr