views:

59

answers:

2

In Java or Ruby, there are many talks about test/TDD/BDD. and there are many tests in Java/Ruby projects.

But I notice that, there are less tests in PHP project, and It seems like PHPer does not like to write tests, I just want to know the reason. thanks

+2  A: 

I've used several applications or libraries, in a variety of languages, that did not include unit tests. This is not something isolated to the PHP community.

In fact, PHP has several testing frameworks available. Developers may either choose not to use them for whatever reason, or may be unaware of them. After all, a lot of PHP developers are self-taught with no formal training in computer sciences.

Honestly, most PHP developers I know that do use testing frameworks and other lifecycle tools tend to have been in the field for a long time, or come from another background like Java.

Ryan Chouinard
+1  A: 

IMO there are multiple contributing factors:

  • It's a pretty new concept for PHPers. I don't use it myself. Most applications are written without contractual API design, hence code preceedes tests (in practice).
  • The test frameworks (PHPUnit and SimpleTest) are not natural.
  • PHP runs in a different environment than Ruby/Java. Moreover application parts are split into include scripts, which have different semantics from packages; sometimes locations variate.
  • PHP isn't as object-oriented to the core as other languages. Most code is procedural / mixed or hybrid.

I would go so far and ask about the merits of Unit-Testing for PHPs runtime environment. Selenium might be a better choice. But personally I'm looking for a simpler approach: http://stackoverflow.com/questions/3853901/simple-non-unit-test-framework-similar-to-phpt-should-evaluate-output-header

mario
Depends a great deal on your application architecture. A solid object oriented PHP application is very easy to test with PHPUnit. Procedural code, with components split into include scripts as you say, may not test well. This is a shortcoming of the developer/architect, not PHP or the testing framework.
Ryan Chouinard
mario
IMHO, the business logic and the view/template logic are things which can/should be tested independently. Business logic with unit and integration tests, and views/templates with Selenium and similar tools. I think the lack of testing stems mostly from developer ignorance or misinformation, but that it's certainly not isolated to PHP. PHP developers simply have little to no training available, especially for these topics.
Ryan Chouinard
I suppose I said all that to reiterate the point that the problem lies with the developer, not the language as your answer seems to imply. Yes, a LOT of PHP projects use a mish-mash of procedural and OO. That's certainly not because PHP forces that style.
Ryan Chouinard
Actually, many famous projects, such as Drupal, I found less test code. I guess PHP has a special runtime
why
Well-known !== well-written. Just because a project is widely used does not make the code a shining example of how to use the language. It just means the concept of the application is popular.
Ryan Chouinard
@Ryan: True. It's foremost a developer problem. The lack of interest in writing tests is obvious. But when I look at SimpleTest, I see where I'd put the other half of the blame.
mario
Why do you make out Unit-Testing (PHPUnit is my choice) as against Selenium? Both can be used and I would argue should be used, to test different things.
James