CodeIgniter and SimpleTest — How to make my first test?

送分小仙女□ 提交于 2019-12-06 07:16:48

问题


I'm used to web development using LAMP, PHP5, MySQL plus NetBeans with Xdebug.

Now I want to improve my development, by learning how to use (A) proper testing and (B) a framework. So I have set up CodeIgniter, SimpleTest and the easy Xdebug add-in for Firefox. This is great fun because maroonbytes provided me with clear instructions and a configured setup ready for download. I am standing on the shoulders of giants, and very grateful.

I've used SimpleTest a bit in the past. Here is a the kind of thing I wrote:

<?php
require_once('../simpletest/unit_tester.php');
require_once('../simpletest/reporter.php');

class TestOfMysqlTransaction extends UnitTestCase {
  function testDB_ViewTable() {
    $this->assertEqual(1,1);   // a pseudo-test
  }
}
$test = new TestOfMysqlTransaction();
$test->run(new HtmlReporter())
?>

So I hope I know what a test looks like. What I can't figure out is where and how to put a test in my new setup. I don't see any sample tests in the maroonbytes package, and Google so far has led me to posts that assume unit testing is already functionally available. What do I do?


回答1:


Edit:

If you are following the maroonbytes setup, just follow the instructions:

  1. Download the SimpleTest framework and extract the files into your @codeigniter directory.
  2. In both your main folder and your admin/application folder create a new folder called tests.
  3. Within the new tests folder setup additional folders called ‘models’, ‘views’, ‘controllers’, ‘libraries’ and ‘helpers’.

Any file ending in .php and with a UnitTestCase inside any of those folders, should be run. :)




回答2:


First, tests must be named properly. To test a controller welcome placed in the file welcome.php a test is named welcome_controller_test.php and stored under tests/controllers/. For more, see this post.

Second, Xdebug's GET argument interferes with the test routine. See post just above, also this post.

Third, the stub test I posted needed two four lines deleted:

//require_once('../simpletest/unit_tester.php');
//require_once('../simpletest/reporter.php');
...
//$test = new TestOfMysqlTransaction();
//$test->run(new HtmlReporter())

I am making tests fairly happily now. CodeIgniter lets me create/maintain tests easily, so my goal of TDD looks reachable. My earlier attempts at TDD gave me the idea, but scratch PHP was just too barren for me to be effective (and we won't discuss VBA!).



来源:https://stackoverflow.com/questions/2845340/codeigniter-and-simpletest-how-to-make-my-first-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!