Re: [PHP] Re: Unit Testing
You are right, when doing unit tests you only want to test the functionality of the current class and so if there are errors its easier to track down which class is causing the problem. I personally use an api called SimpleTest for my php testing and what you can do with this is create Mock objects for substituting the objects that you dont need to test. A Mock object is basically a clone of a classes structure which then allows you to do stuff like set the return values of each function or perform tests on the parameters passed to each function. Ive not used PHPUnit so im not sure if you can do something similar. On Mon, Apr 27, 2009 at 7:20 PM, Philip Thompson philthath...@gmail.comwrote: On Apr 27, 2009, at 11:38 AM, Simon wrote: As a programmer, i always test what I'm coding as i code it (mostly to make sure i dont include typos), but i feel it is best to make proper testing once the component is ready and working fine. If your project is large, it might be a good idea to break it in several 'modules' or section if possible and treat each of them as separate projects (testing would be done on a module once this one is ready). You may be interested in looking for information on the net on 'IT project management' which usually describe when is the best time to test according to a certain structure... Simon On Mon, Apr 27, 2009 at 12:16 PM, Nathan Rixham nrix...@gmail.com wrote: Philip Thompson wrote: Hi. I did some searching in the archives, but didn't quite find what I was looking for. Maybe a few of you can assist me... We have an application that's currently in production, but we're constantly modifying/upgrading it. We did not do unit testing early on because of the lack of time. Now that some time has opened up, we're considering unit testing. My question is. is it reasonable to start unit testing at this point in time with the application mostly built? Besides being really time-consuming, what are the pitfalls of starting unit testing at this stage? Thanks in advance, ~Philip maybe a useless answer, but, no pitfalls - just do it - I'm always surprised by my unit test results, its invaluable and it's never too late to start. just think about the next years worth of bugs found by the client not being there! A question I have about unit testing. The point is to test individual units... correct? So, let's say I have this core class which creates instances of other classes. Well, if I only want test the core class, I don't want to instantiate the other classes... correct? Example: ?php // Core.php require ('Class1.php'); require ('Class2.php'); class Core { public function __construct () { $this-class1 = new Class1 ($this); $this-class2 = new Class2 ($this); } } // CoreTest.php require ('../PHPUnit/Framework.php'); require ('../includes/Core.php'); class CoreTest extends PHPUnit_Framework_TestCase { protected function setUp () { $this-core = new Core(); } } ? So, here, Class1 and Class2 will be instantiated. However, I don't really care for them to be so that I can test all the methods in the core class. Is this a perfect example of how the original design of the core class is not conducive to implementing unit tests? Without rewriting the core class, is there a way to test this? Thanks, ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Unit Testing
Philip Thompson wrote: On Apr 27, 2009, at 11:38 AM, Simon wrote: As a programmer, i always test what I'm coding as i code it (mostly to make sure i dont include typos), but i feel it is best to make proper testing once the component is ready and working fine. If your project is large, it might be a good idea to break it in several 'modules' or section if possible and treat each of them as separate projects (testing would be done on a module once this one is ready). You may be interested in looking for information on the net on 'IT project management' which usually describe when is the best time to test according to a certain structure... Simon On Mon, Apr 27, 2009 at 12:16 PM, Nathan Rixham nrix...@gmail.com wrote: Philip Thompson wrote: Hi. I did some searching in the archives, but didn't quite find what I was looking for. Maybe a few of you can assist me... We have an application that's currently in production, but we're constantly modifying/upgrading it. We did not do unit testing early on because of the lack of time. Now that some time has opened up, we're considering unit testing. My question is. is it reasonable to start unit testing at this point in time with the application mostly built? Besides being really time-consuming, what are the pitfalls of starting unit testing at this stage? Thanks in advance, ~Philip maybe a useless answer, but, no pitfalls - just do it - I'm always surprised by my unit test results, its invaluable and it's never too late to start. just think about the next years worth of bugs found by the client not being there! A question I have about unit testing. The point is to test individual units... correct? So, let's say I have this core class which creates instances of other classes. Well, if I only want test the core class, I don't want to instantiate the other classes... correct? Example: ?php // Core.php require ('Class1.php'); require ('Class2.php'); class Core { public function __construct () { $this-class1 = new Class1 ($this); $this-class2 = new Class2 ($this); } } // CoreTest.php require ('../PHPUnit/Framework.php'); require ('../includes/Core.php'); class CoreTest extends PHPUnit_Framework_TestCase { protected function setUp () { $this-core = new Core(); } } ? So, here, Class1 and Class2 will be instantiated. However, I don't really care for them to be so that I can test all the methods in the core class. Is this a perfect example of how the original design of the core class is not conducive to implementing unit tests? Without rewriting the core class, is there a way to test this? Thanks, ~Philip well Class1 and Class2 should have there own unit tests, so by the time you get to Core you know the others are good so it doesn't matter if they are called or not. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Unit Testing
Philip Thompson wrote: Hi. I did some searching in the archives, but didn't quite find what I was looking for. Maybe a few of you can assist me... We have an application that's currently in production, but we're constantly modifying/upgrading it. We did not do unit testing early on because of the lack of time. Now that some time has opened up, we're considering unit testing. My question is. is it reasonable to start unit testing at this point in time with the application mostly built? Besides being really time-consuming, what are the pitfalls of starting unit testing at this stage? Thanks in advance, ~Philip maybe a useless answer, but, no pitfalls - just do it - I'm always surprised by my unit test results, its invaluable and it's never too late to start. just think about the next years worth of bugs found by the client not being there! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Unit Testing
As a programmer, i always test what I'm coding as i code it (mostly to make sure i dont include typos), but i feel it is best to make proper testing once the component is ready and working fine. If your project is large, it might be a good idea to break it in several 'modules' or section if possible and treat each of them as separate projects (testing would be done on a module once this one is ready). You may be interested in looking for information on the net on 'IT project management' which usually describe when is the best time to test according to a certain structure... Simon On Mon, Apr 27, 2009 at 12:16 PM, Nathan Rixham nrix...@gmail.com wrote: Philip Thompson wrote: Hi. I did some searching in the archives, but didn't quite find what I was looking for. Maybe a few of you can assist me... We have an application that's currently in production, but we're constantly modifying/upgrading it. We did not do unit testing early on because of the lack of time. Now that some time has opened up, we're considering unit testing. My question is. is it reasonable to start unit testing at this point in time with the application mostly built? Besides being really time-consuming, what are the pitfalls of starting unit testing at this stage? Thanks in advance, ~Philip maybe a useless answer, but, no pitfalls - just do it - I'm always surprised by my unit test results, its invaluable and it's never too late to start. just think about the next years worth of bugs found by the client not being there! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- When Earth was the only inhabited planet in the Galaxy, it was a primitive place, militarily speaking. The only weapon they had ever invented worth mentioning was a crude and inefficient nuclear-reaction bomb for which they had not even developed the logical defense. - Asimov -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Unit Testing
On Apr 27, 2009, at 11:38 AM, Simon wrote: As a programmer, i always test what I'm coding as i code it (mostly to make sure i dont include typos), but i feel it is best to make proper testing once the component is ready and working fine. If your project is large, it might be a good idea to break it in several 'modules' or section if possible and treat each of them as separate projects (testing would be done on a module once this one is ready). You may be interested in looking for information on the net on 'IT project management' which usually describe when is the best time to test according to a certain structure... Simon On Mon, Apr 27, 2009 at 12:16 PM, Nathan Rixham nrix...@gmail.com wrote: Philip Thompson wrote: Hi. I did some searching in the archives, but didn't quite find what I was looking for. Maybe a few of you can assist me... We have an application that's currently in production, but we're constantly modifying/upgrading it. We did not do unit testing early on because of the lack of time. Now that some time has opened up, we're considering unit testing. My question is. is it reasonable to start unit testing at this point in time with the application mostly built? Besides being really time-consuming, what are the pitfalls of starting unit testing at this stage? Thanks in advance, ~Philip maybe a useless answer, but, no pitfalls - just do it - I'm always surprised by my unit test results, its invaluable and it's never too late to start. just think about the next years worth of bugs found by the client not being there! A question I have about unit testing. The point is to test individual units... correct? So, let's say I have this core class which creates instances of other classes. Well, if I only want test the core class, I don't want to instantiate the other classes... correct? Example: ?php // Core.php require ('Class1.php'); require ('Class2.php'); class Core { public function __construct () { $this-class1 = new Class1 ($this); $this-class2 = new Class2 ($this); } } // CoreTest.php require ('../PHPUnit/Framework.php'); require ('../includes/Core.php'); class CoreTest extends PHPUnit_Framework_TestCase { protected function setUp () { $this-core = new Core(); } } ? So, here, Class1 and Class2 will be instantiated. However, I don't really care for them to be so that I can test all the methods in the core class. Is this a perfect example of how the original design of the core class is not conducive to implementing unit tests? Without rewriting the core class, is there a way to test this? Thanks, ~Philip -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Unit testing ?
* mbneto [EMAIL PROTECTED] : Thanks for the reply. Your email confirmed what I've read/thought about the tests. I'll look this SimpleTest even tough PHPUnit2 seems to do the job fine. Use the unit testing framework with which you are most comfortable; the ideas remain the same, just the details differ. If you have more info (like books, urls, examples) please send me. Unfortunately, no. Most of this is personal experience, a little of it was garnered from php|Tropics, and that portion wasn't an official part of Jason Sweat's presentation. On 6/2/05, Matthew Weier O'Phinney [EMAIL PROTECTED] wrote: * mbneto [EMAIL PROTECTED] : I am trying the phpunit2 for unit testing but the examples found in the documentation are few and do not address, for example, tests when database access is involved. Perhaps this belongs to a more general question (i.e strategies for unit testing) so any urls, docs would be great. Jason Sweat covered this at php|Tropics, using SimpleTest as the unit testing framework. I use phpt unit tests (developed for testing php itself, and used by the PEAR project for regression tests). The principles are the same regardless of framework, however. The fundamental problem is: your code may depend on the results of a DB operation -- it's primary purpose may even be to perform a DB operation. While you can test the code, you still need to test whether or not your code can successfully perform the DB operation as well. A common problem I find is that I'm building SQL on the fly -- and that process may build shoddy SQL. It may be building exactly what I designed it to do, but the RDBMS will never be able to actually utilize the SQL I build. Tests can help catch these issues. snip -- full explanation -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Unit testing ?
Hi Matthew, Thanks for the reply. Your email confirmed what I've read/thought about the tests. I'll look this SimpleTest even tough PHPUnit2 seems to do the job fine. If you have more info (like books, urls, examples) please send me. regards. On 6/2/05, Matthew Weier O'Phinney [EMAIL PROTECTED] wrote: * mbneto [EMAIL PROTECTED]: I am trying the phpunit2 for unit testing but the examples found in the documentation are few and do not address, for example, tests when database access is involved. Perhaps this belongs to a more general question (i.e strategies for unit testing) so any urls, docs would be great. Jason Sweat covered this at php|Tropics, using SimpleTest as the unit testing framework. I use phpt unit tests (developed for testing php itself, and used by the PEAR project for regression tests). The principles are the same regardless of framework, however. The fundamental problem is: your code may depend on the results of a DB operation -- it's primary purpose may even be to perform a DB operation. While you can test the code, you still need to test whether or not your code can successfully perform the DB operation as well. A common problem I find is that I'm building SQL on the fly -- and that process may build shoddy SQL. It may be building exactly what I designed it to do, but the RDBMS will never be able to actually utilize the SQL I build. Tests can help catch these issues. Basically, when testing code that interacts with a database, you've got two basic strategies: (1) test against the DB, or (2) use mock objects. The latter is a tricky subject, as it assumes you're using a DB abstraction layer, and because you then have to mimic how that abstraction layer works. Basically, you end up doing a lot of code simply to test. Which brings us back to (1), test against the DB. The way to do this is to have some code that sets up and tears down a TEST database -- not the one with your live data. It should likely create and populate any tables you need, and then be able to tear them down again. The reason behind this is that you can then have a set of consistent data to test against -- once you run tests, chances are likely that you've altered the data. Each test you run should tear down the DB and then recreate and/or repopulate it. If you use the phpt unit tests, the place to do this is in your setup.php.inc file. I then create a file with the raw SQL for setting up and populating a test table, slurp it in with file_get_contents, and pass it on to the DB from within a function in that setup file. Then another function can truncate or delete all records from the tables utilized. The code within the test might then look like this: ?php include_once dirname(__FILE__) . '/setup.php.inc'; setupDb(); // do a test teardownDb(); setupDb(); // do another test teardownDb(); ? That's kind of a long-winded answer to your question, but it's not something you see a lot of information on. I hope that it helps. -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Unit testing ?
* mbneto [EMAIL PROTECTED]: I am trying the phpunit2 for unit testing but the examples found in the documentation are few and do not address, for example, tests when database access is involved. Perhaps this belongs to a more general question (i.e strategies for unit testing) so any urls, docs would be great. Jason Sweat covered this at php|Tropics, using SimpleTest as the unit testing framework. I use phpt unit tests (developed for testing php itself, and used by the PEAR project for regression tests). The principles are the same regardless of framework, however. The fundamental problem is: your code may depend on the results of a DB operation -- it's primary purpose may even be to perform a DB operation. While you can test the code, you still need to test whether or not your code can successfully perform the DB operation as well. A common problem I find is that I'm building SQL on the fly -- and that process may build shoddy SQL. It may be building exactly what I designed it to do, but the RDBMS will never be able to actually utilize the SQL I build. Tests can help catch these issues. Basically, when testing code that interacts with a database, you've got two basic strategies: (1) test against the DB, or (2) use mock objects. The latter is a tricky subject, as it assumes you're using a DB abstraction layer, and because you then have to mimic how that abstraction layer works. Basically, you end up doing a lot of code simply to test. Which brings us back to (1), test against the DB. The way to do this is to have some code that sets up and tears down a TEST database -- not the one with your live data. It should likely create and populate any tables you need, and then be able to tear them down again. The reason behind this is that you can then have a set of consistent data to test against -- once you run tests, chances are likely that you've altered the data. Each test you run should tear down the DB and then recreate and/or repopulate it. If you use the phpt unit tests, the place to do this is in your setup.php.inc file. I then create a file with the raw SQL for setting up and populating a test table, slurp it in with file_get_contents, and pass it on to the DB from within a function in that setup file. Then another function can truncate or delete all records from the tables utilized. The code within the test might then look like this: ?php include_once dirname(__FILE__) . '/setup.php.inc'; setupDb(); // do a test teardownDb(); setupDb(); // do another test teardownDb(); ? That's kind of a long-winded answer to your question, but it's not something you see a lot of information on. I hope that it helps. -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Unit Testing
Hi I'm interested in locating something straight forward to teach to our programming students... Am appreciating the discussion so far... Trevor Nesbit MBS BSc BCom CA PGDipBusAdmin Degree Leader for Bachelor of ICT Programme Leader for Graduate Diploma in eCommerce School of Computing - www.cpit.ac.nz/computing School of Business Christchurch Polytechnic Institute of Technology Torsten Roehr [EMAIL PROTECTED] 06/16/04 07:37 AM Rick Fletcher [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Has anyone done any PHP unit testing? I've been looking around for a unit testing library to try out. Below are the ones I've found so far: SimpleTest: http://www.lastcraft.com/simple_test.php PHPUnit (dead?): http://phpunit.sourceforge.net/ Pear PHPUnit: http://pear.php.net/package/PHPUnit Generic PHP Framework (dead?): http://gpfr.sourceforge.net/ SimpleTest looks the most complete and the most active, so that's where I'm leaning. Anyone have any experience with any of these libraries, or have any comments on PHP Unit testing in general? Don't have any experience myself but here's a nice article by Harry about SimpleTest: http://www.sitepoint.com/blog-post-view.php?id=175190 Hope it helps. Regards, Torsten Roehr -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Unit Testing
Used all but 'Generic PHP Framework' and everything was quite fine and worked out just as i expected. But i was still missing some features which made me build my own small library to fit into my Framework. -- red Rick Fletcher wrote: Has anyone done any PHP unit testing? I've been looking around for a unit testing library to try out. Below are the ones I've found so far: SimpleTest: http://www.lastcraft.com/simple_test.php PHPUnit (dead?): http://phpunit.sourceforge.net/ Pear PHPUnit: http://pear.php.net/package/PHPUnit Generic PHP Framework (dead?): http://gpfr.sourceforge.net/ SimpleTest looks the most complete and the most active, so that's where I'm leaning. Anyone have any experience with any of these libraries, or have any comments on PHP Unit testing in general? Thanks. Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Unit Testing
Allmost forgot about this one: http://www.phppatterns.com/index.php/article/articleview/33/1/2/ very well written ( phpPatterns() ) covers some other nice topics as well, allways worth a read. -- red Red Wingate wrote: Used all but 'Generic PHP Framework' and everything was quite fine and worked out just as i expected. But i was still missing some features which made me build my own small library to fit into my Framework. -- red Rick Fletcher wrote: Has anyone done any PHP unit testing? I've been looking around for a unit testing library to try out. Below are the ones I've found so far: SimpleTest: http://www.lastcraft.com/simple_test.php PHPUnit (dead?): http://phpunit.sourceforge.net/ Pear PHPUnit: http://pear.php.net/package/PHPUnit Generic PHP Framework (dead?): http://gpfr.sourceforge.net/ SimpleTest looks the most complete and the most active, so that's where I'm leaning. Anyone have any experience with any of these libraries, or have any comments on PHP Unit testing in general? Thanks. Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Unit Testing
Rick Fletcher [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Has anyone done any PHP unit testing? I've been looking around for a unit testing library to try out. Below are the ones I've found so far: SimpleTest: http://www.lastcraft.com/simple_test.php PHPUnit (dead?): http://phpunit.sourceforge.net/ Pear PHPUnit: http://pear.php.net/package/PHPUnit Generic PHP Framework (dead?): http://gpfr.sourceforge.net/ SimpleTest looks the most complete and the most active, so that's where I'm leaning. Anyone have any experience with any of these libraries, or have any comments on PHP Unit testing in general? Don't have any experience myself but here's a nice article by Harry about SimpleTest: http://www.sitepoint.com/blog-post-view.php?id=175190 Hope it helps. Regards, Torsten Roehr -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Unit Testing
Rick Fletcher wrote: Has anyone done any PHP unit testing? I've been looking around for a unit testing library to try out. Below are the ones I've found so far: SimpleTest: http://www.lastcraft.com/simple_test.php PHPUnit (dead?): http://phpunit.sourceforge.net/ Pear PHPUnit: http://pear.php.net/package/PHPUnit Generic PHP Framework (dead?): http://gpfr.sourceforge.net/ SimpleTest looks the most complete and the most active, so that's where I'm leaning. Anyone have any experience with any of these libraries, or have any comments on PHP Unit testing in general? Thanks. Rick PHPUnit in PEAR at least is still in development. PHPUnit2 is really the one that's still being worked on, though, and it's PHP5 only... -- paperCrane Justin Patrin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Unit-testing PHP code
Check out. http://www.phpbuilder.com/columns/reiersol20030126.php3 http://pear.php.net/manual/en/packages.phpunit.tutorial.php Mark Karl Traunmueller [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi all, I've written some small Web applications with PHP and I like it. Since I'm accustomed to thinking OO, I was using the object-oriented language features. This worked quite well, giving a reasonably clean design. However, as with all software projects beyond a certain scope, things start getting hard to maintain/test at some point. Now, the next step for me is to make my code unit-testable. I want to run automatic tests on my local development machine, testing the PHP code without having to upload to the Web Server and manually test something. Most preferrably would be a solution similar to nUnit/jUnit. As far as I understand, a local installation of the ZEND engine could be used to run the PHP code, and some test driver framework would be needed to run all tests through ZEND and verify the results. Has anybody done this yet? I've found the phpunit project, but there seems to be nothing more than the sourceforge project page, which is not very informative. regards Karl -- _ DI Karl Traunmüller [EMAIL PROTECTED] DI Karl Traunmüller Softwareentwicklung Starhembergstr. 44/1, A-4020 Linz, Austria tel +43 732 667950, mobile +43 664 4037084 ICQ 179001232 www.sofascience.com _ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php