Functional Test Generation with symfony 1.2

Introduction

Functional tests are very important to test your application. Once defined, you just have to runs them to make sure there are no side effects when you add new features.

Now, in real life, it is always time consuming to write functionnal test, or just to remember all the options from the tester class to the request parameters for each page, most of us, just skip this important step. (not to mention that project manager can just simply say : "we have to time to code all the tests, bla bla bla ..., client wants this project done for last week.... ")

Last day I introduced the swToolboxPlugin which provides a handy tool to send email. It also contains a specific filter which just records user's interactions in a symfony Functional Test format.

So mainly you navigate accross your application, submit your forms and click on links, and boom ... you have a functional test *almost* ready to use.

Installation

1. Install swToolboxPlugin
 - svn version at http://svn.symfony-project.com/plugins/swToolboxPlugin/sf1.2/trunk/
 - there is not package version for now

2. clear your cache

3. Edit the filters.yml file and add these configuration lines after the rendering filter

 functional_test: 
    class: swFilterFunctionalTest

4. Make sure the debug panel is enabled

Usage

A functional test menu should appear on the right side of the debug panel. When you click on the menu, you will get two options :
 - Reset : reset the content of the current functional test
 - Activate : activate the filter to record action.

when you are ready to create a functional test, just activate the filter, perform your actions on the website. When you are done reopen the menu and copy the generated code to a new test file.


include(dirname(__FILE__).'/../../bootstrap/functional.php');

$browser = new sfTestFunctional(new sfBrowser());
$test    = $browser->test();
$conn    = Doctrine::getConnectionByTableName('sw_blog_posts'); // I changed this to match the test

$conn->beginTransaction();
$browser
  ->call('/en/articles/2008/03/13/using-soap-php-with-ntlm-authentication', 'get', array())
  ->with('request')->begin()
    ->isParameter('module', 'swBlogPosts')
    ->isParameter('action', 'view')
  ->end()
  ->with('response')->begin()
    ->isStatusCode(200)
  ->end()
;

$browser
  ->call('/en/articles/add-comment/1', 'post', array (
  'sw_blog_comment' =>
  array (
    'name' => 'thomas',
    'email' => '',
    'url' => '',
    'message' => 'my message',
  ),
  'module' => 'swBlogPosts',
  'action' => 'addComment',
  'sf_culture' => 'en',
  'id' => '1',
))
  ->with('request')->begin()
    ->isParameter('module', 'swBlogPosts')
    ->isParameter('action', 'addComment')
  ->end()
;

$conn->rollback();

if you run the test you should have :



You are free now to customize the generated code to add more test. In this case I might add this following code

$form = $browser->getContext()->getActionStack()->getLastEntry()->getActionInstance()->getVar('form');
$comment = $form->getObject();

$test->is($comment->getArticleId() == 1, 'Check if the comment belong to the correct article');

Conclusion

As you can see it is quite easy to create complete functional test, just use your application. Please keep in mind, this tools just generated basic test from navigation, you are strongly advice to spend more time to check database relation.

Note : The "generated test" uses doctrine database connection to make a transaction and rollback the change at this end, so your database is not mess up.

Comments

jukea
about 2 hours after
wow, this is much appreciated. Thx
damien
about 7 hours after
Awesome. I tried it and it rocks! I lost a few requests during my first try but a second attempt allowed me to complete my functional test code. I don't know if the error came from me or your plugin.
Bernhard
about 8 hours after
Simple yet impressive idea! I'm going to try it.
Noel
about 10 hours after
wow, it's really impressive! Great work!
Mark
about 19 hours after
Really great work again. This makes things so much easier.

Mark
Éric
5 days after
Idea of the year !
dreur
25 days after
Is it possible there is an error while parsing links with an image.
It seems to add a greater than > between the two tags.
Tooo
3 months after
Thanks!
James Cauwelier
5 months after
I wrote something similar which records user interactions and gives an iMacro macro as output. The macro is generated and could be mailed upon exception catching.

I 'll certainly check this out to see if we can combine our efforts.
k
6 months after
Thanks, brilliant post, this will save us a ton of time!
Alex
6 months after
Hello,

Can someone tell me which filters.yml to edit? I'm assuming frontend, but am having no luck.

Thanks.
Tom
7 months after
Thanks for the information!
Sample Essays
Christopher Shennan
9 months after
I'm trying to create a functional test to test some functionality which makes use of transaction but the functional test fails because it results in nested transactions.

Has anyone experienced this before and know how to resolve it?

Many Thanks.

Chris.
Rafael Vences
10 months after
After install it, it has error, i have Symfony 1.2.9

Any idea, Thanks

Fatal error: Class 'swFilterFunctionalTest' not found in /Users/rvences/Sites/symfony/sett/cache/frontend/dev/config/modules_agenda_config_filters.yml.php on line 12
Olivier Loynet
12 months after
Hi Thomas,

Is your plugin working with sf1.4 ?

I start to record a sequence, and at the end, the generated code don't change when I reopen the toolbar

Any idea !

Great job, hope he can help me soon.

Olivier
Olivier Loynet
12 months after
It works well will sf1.4.

My mistake was I put the configuration into the rendering filter and not as asked 'after the rendering filter'

Olivier
Eric Delord
about 1 year after
Nice,

but why don't you check that sf_web_debug is enabled to activate the filter ?
if(!sfConfig::get('sf_web_debug'))
{
$filterChain->execute();
return;
}
This would make sense no ?
Oferte Last Minute
about 1 year after
Hi!
Veri nice!
Tkx!

Add comment











A la recherche de projets symfony ?

Tags

Last posts