New in Symfony 4.3: Better test assertions
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:
1 2 3 4 | // Before
$this->assertSame(200, $client->getResponse()->getStatusCode());
// After
$this->assertResponseIsSuccessful();
|
Example of checking if the response redirects to some URL:
1 2 3 4 5 | // Before
$this->assertSame(301, $client->getResponse()->getStatusCode());
$this->assertSame('https://example.com', $client->getResponse()->headers->get('Location'));
// After
$this->assertResponseRedirects('https://example.com', 301);
|
Example of checking if some element contains some text:
1 2 3 4 | // 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()
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.
New in Symfony 4.3: Better test assertions symfony.com/blog/new-in-symfony-4-3-better-test-assertions
Tweet thisComments
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);`
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.
Nicolas Scolari said on Apr 3, 2019 at 07:43 #1