1

I have a big application which i want to test with phpunit. In the first step i want to test if every page is accessible and returns a response code 200.

This is how i do it:

public function testIndexActionCanBeAccessed()
{
    $this->dispatch('/');
    $this->assertResponseStatusCode(200);
}

But as i said before, my application is a big one. So there are many pages to test. These tests will take a long time.

Is there a way to speedup those tests or aren´t these tests relevant?

THANKS FOR HELP

2
  • 1
    Well, each dispatch is a full request, so naturally this takes time. You can decide for yourself if a unit-test like this is relevant. There is no way to magically speed up a request, only because it's a unit test ;) Commented Oct 30, 2013 at 18:30
  • Use Unit Tests for testing public methods. Aim for the smallest unit in your code. You might take a look at behat for testing repsonses, where you send a GET/POST/PUT or DELETE Request and verify the reponse including headers and body content. Commented Oct 30, 2013 at 19:01

2 Answers 2

2

I once had the situation that a rarely used page was broken due to a code change, and the management was loudly complaining. The failure would have been easily spotted by simply requesting that page with wget - and that's what I added as a regular job to our Jenkins then. That job never failed afterwards, happily requesting two or three pages day after day, month after month, for about two years (then the pages were removed).

If you are in a situation where there is no better testing available or would be too cumbersome to implement, testing for HTTP status codes would be a valid approach. But as you saw yourself, this will get way too slow very fast, because you must always test the whole system as one big black box. And you need everything working in a test environment.

If you have a nearly untestable system right now, using functional tests that remotely control the application with things like Selenium are a good first step to detect failure quickly. You probably want to execute these test in parallel, which would require proper hardware power, most likely in the form of multiple machines just to execute the Selenium clients.

On the other hand, you should aim for getting more unit tests. These will run really fast, because you will inject mock objects as a replacement for real things like the database connection. Without a database in the background, the test will not communicate with the network, not write and read from disk - the mock object is in memory and give the needed answers almost instantly.

In combination, the unit tests together with a selected set of functional tests (and probably some integration tests that put some bigger parts together and test them individually, but not the whole application) will show that everything works as expected.

But with functional tests alone, this will never be the case.

Sign up to request clarification or add additional context in comments.

Comments

-1

From your point of view, why such a test should be relevant ? You are running the test suite in test environment or on your dev machine, so the application will implicitly be "online". If you really need to test if the application responds, perform a single test for this, not one for each page.

3 Comments

What about a check to see or certain pages are giving the right status code.. there are more of them you know. For me for instance.. I need to check the response code for a rest api.. couple of examples 404, 400, 401, 422, 500 and yeah then you have the 200 too.
@FlipVernooij What you are talking about are integration tests, not unit tests. A unit test main scope is to test a single unit -> the unit currently under test, not an entire chain of services and dependencies. Also, an important side effect of not running your unit tests in isolation will be speed. HTTP requests takes time, the tests will run slowly and because of that the developer will stop running the test suite.
Perfect world scenarios are not that common, a lot of projects don't even have tests and sometimes the theory isn't that practical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.