Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

If I want to unit-test some of my javascript functions, would it be more efficient (and doable) to use the already installed PHP unit-testing framework, or should I use a javascript unit-testing framework?

EDIT: It seems I have to clarify things a bit. I'm using javascript for form validation. It seems that some logic doesn't work as I want it to be, and I thought to myself that would be a good point to begin to use unit-testing (something that came to my scope only recently). I already began to use SimpleTest on a little project of mine, but never to test logic that is generated client-side.

Now I know that SimpleTest have the possibility to emulate a browser for example. But I was wondering what other method would they be to unit-test my form validation. While I was searching for idea, I realized that there is also unit-testing framework for javascript, but after a bit of searching on some of the SE sites, I didn't find anything about use of PHP and Javascript Framework together, so I wondered how common such a situation it was, and how do other people manage in such an environment.

share|improve this question
    
There are javascript and jQuery frameworks available for form validation in the browser. If you use one of those, a lot of your unit testing will already have been accomplished by someone else. –  Robert Harvey Dec 20 '10 at 16:38
add comment

3 Answers

up vote 3 down vote accepted

The short answer is no.

You cannot effectively test JavaScript with PHP. To do it effectively, you need to use JavaScript.

More than likely you will have to use a JavaScript unit testing library such as the one used for Prototype.js. Essentially, you need to be able to execute the JavaScript in browser, and get the results of the JavaScript run. You won't be able to do that from the server side (PHP).

Unit testing frameworks are most efficient and useful when you are using the same language to test as you are to write the code in. For that reason we have PHP unit testing frameworks, Java unit testing frameworks, C#/.Net unit testing frameworks, and yes, JavaScript unit testing frameworks. A big part of the reason for this is that your unit tests need to examine the results of what your code is doing in the environment where it runs. SimpleTest runs in the PHP environment, and examines to assert that your PHP functions are doing what you expect. It doesn't run directly in the client browser, but as all PHP code it runs on the server and generates HTML output.

In order to run and test JavaScript functionality--particularly when the problem might be how a one browser implements it--you need to execute and assert functionality in JavaScript. Now, you can roll your own JavaScript unit testing framework for your environment, or you can use one that already exists.

The JavaScript lives 100% on the client side. That's why you didn't see anything about running JavaScript and PHP together--just as you won't find information on Java and JavaScript together (in the same context we are talking here).

share|improve this answer
    
Well I'll have to beg to differ, because there is always the possibility to call dynamically a PHP script and display the result with AJAX. –  Eldros Dec 20 '10 at 16:15
    
Maybe I'm misunderstanding the question, but it looks like you are trying to unit test the JavaScript--not the server responses from AJAX. What are you trying to accomplish with the test? That the JavaScript works across different browsers? If so the JavaScript has to be executed in those browsers. –  Berin Loritsch Dec 20 '10 at 16:19
    
Well I see that I need to clarify a few things, see my edit. –  Eldros Dec 20 '10 at 16:37
    
I've expanded my answer, but the conclusion hasn't changed. –  Berin Loritsch Dec 20 '10 at 17:41
    
One last thing, to see if I understand the crux of your answer: Let's I've got this PHP script (not the unit-test one) that is called dynamically through an AJAX call, so it's content would be loaded in a div. To unit-test this story, I'll have to (1) unit-test the AJAX call with a javascript framework and (2) unit-test the script itself with my PHP framework, am I correct? –  Eldros Dec 21 '10 at 7:18
show 1 more comment

You could probably hack it in, but I can't see the end result being particularly pretty or easy to use. Are you just looking to use the PHP testing framework to report test results, or are you hoping to call the javascript via the PHP?

Personally I'd use something like qunit; the jquery testing framework. It's very easy to use and lets you use javascript to test javascript within a browser. You can also link it into Rhino (with env.js) to allow for automated headless test runs.

share|improve this answer
    
See my edit for more clarification. –  Eldros Dec 20 '10 at 16:37
add comment

Why aren't you using Firebug? In particular there is an extension called Firesymfony that integrates PHP debugging support from the Symfony framework but there are other extensions that might be more suitable for what you are trying to do.

share|improve this answer
    
I use Firebug, but it hasn't the same role as a unit-testing framework. There is times where the code would run without errors, but would not behave as expected. In TTD, one write the unit-test beforehand to ensure that the developed function would do what it is supposed to do. –  Eldros Dec 21 '10 at 11:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.