31

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?

JoshB
  • 742
  • 1
  • 8
  • 12

3 Answers3

15

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']);
}
Edson Horacio Junior
  • 3,033
  • 2
  • 29
  • 50
5

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.

fyr
  • 20,227
  • 7
  • 37
  • 53
  • Is there any advantage to using yaymock over the built-in PHPUnit features? – JoshB Jun 28 '11 at 18:53
  • Mock libraries are designed especially for the purpose of testing chains of object method invocations. Unit tests are not. There is a wikipage in the project page where you see some examples which demonstrate these tests([UsingMockObjects](http://code.google.com/p/yaymock/wiki/UsingMockObjects) you will see the advantage over normal unit tests. But to be clear mock tests are not a replacment for unit tests. – fyr Jun 28 '11 at 18:58
  • Thank you for that. This seems to be the way to go. – JoshB Jun 28 '11 at 19:02
  • 3
    Doesn't mocking "take out" (via "mocking" them) dependent libraries from your code being tested, thus not actually testing your integrations any longer? – fideloper Dec 10 '13 at 16:39
  • You are right that "mocking" takes always something out. Might be a library might be a module might be a dependent backend system but without doing so you can only do positive testing. Another aspect is coverage. By "mocking" things you should also take care that your system coverage is still high. – fyr Dec 11 '13 at 07:10
  • Imho you cannot rely on a Mock service/controller layer.. It will not be affected by external software at any point, thus it is not an integration to anything. If youre running a web-app for instance, you need a running `server process` (initial measure - response time / stress threshold - it COULD crash) and serialization/desiralization (interpretation of the browsers GET/POST payloads to your controllers basically) over the `network protocol` - also you need a defined set of `sequential actions` (maybe targeting various controllers / endpoints - thus testing them `together` - `integraded`). – mschr Jul 12 '21 at 15:49
1

In case you want to whitebox test specific classes that interact with infrastructure, I've created a PHPUnit extension for it.

https://github.com/hrodic/php-integration-testing

It's minimal and simple. You just need to have PHPUnit.

Rodrigo
  • 11
  • 1