New in Symfony 4.3: Better test assertions

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Contributed by Alex Rock and Fabien Potencier in #30813.

In Symfony applications, creating functional tests is simple thanks to the utilities provided by the WebTestCase class and PHPUnit's assertions. However, for common use cases (such as testing that the response was successful or that it redirected to some URL) the assert___() methods are too basic and require writing too much code.

Lots of Symfony developers have created their own utility classes for tests to solve this issue. That's why in Symfony 4.3 we've decided to add the most common ones in a new Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait which is included by default in WebTestCase, so you only have to upgrade your application to Symfony 4.3 to start using them.

Example of checking if the response was successful:

// Before
$this->assertSame(200, $client->getResponse()->getStatusCode());
// After

Example of checking if the response redirects to some URL:

// Before
$this->assertSame(301, $client->getResponse()->getStatusCode());
$this->assertSame('', $client->getResponse()->headers->get('Location'));
// After
$this->assertResponseRedirects('', 301);

Example of checking if some element contains some text:

// Before
$this->assertContains('Hello World', $crawler->filter('h1')->text());
// After
$this->assertSelectorTextContains('h1', 'Hello World');

The new assertions will make your tests more readable and will boost your productivity. Here's the full list of assertions available:

  • assertClientCookieValueEquals()
  • assertClientHasCookie()
  • assertClientNotHasCookie()
  • assertClientRawCookieValueEquals()
  • assertHttpCodeEquals()
  • assertInputValueEquals()
  • assertInputValueNotEquals()
  • assertPageTitleContains()
  • assertPageTitleEquals()
  • assertRequestAttributeValueEquals()
  • assertResponseCookieValueEquals()
  • assertResponseCookieValueNotEquals()
  • assertResponseHasCookie()
  • assertResponseHasHeader()
  • assertResponseHeaderEquals()
  • assertResponseHeaderNotEquals()
  • assertResponseIsSuccessful()
  • assertResponseNotHasCookie()
  • assertResponseNotHasHeader()
  • assertResponseRedirects()
  • assertRouteEquals()
  • assertSelectorContainsText()
  • assertSelectorExists()
  • assertSelectorNotContainsText()
  • assertSelectorNotExists()
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.


Nice enhancement. Well done ;)
Thanks for this new feature.

I don't really like $this->assertResponseIsSuccessful(), because When It fails it says "Failing asserting false is true". And IMHO, this is not as explicit as "Failing asserting a 403 is 200".

`$this->assertResponseIsSuccessful()` will not fail saying "Failing asserting false is true". It will fail saying "Failing asserting that the response is successful".

A successful Response is just a Response with a status code in the 2xx range.

If you want to check for a specific status code, use `$this->assertResponseStatusCodeSame(200);`
Cool cool
Very nice :) this would be fun to implement in Rector
Nice, thanks!
Looks like some method from the list are missing in Symfony code, e.g assertHttpCodeEquals().
It was in #29990, but not in #30813 that replaced it.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.