I'm taking over a project and am trying to write some tests for CodeIgniter to run on the command line with continuous integration and there seem to be very little documentation online on testing with CodeIgniter and getting tests to run for controllers seem particularly difficult.

There's some previous answers suggesting to use FooStack, but FooStack is for CodeIgniter 1.x as best as I can gather.

I'm at the point not where I'm considering running selenium over the whole site. Has anyone managed to test controllers with CodeIgniter?

Related answers:

有帮助吗?

解决方案

Using my-ciunit.

  1. Grab a copy of https://bitbucket.org/kenjis/my-ciunit
  2. Install PHPUnit 3.7.x
  3. Run the installation instructions.
  4. Presumably you didn't name the dadabase on the command line so go to application/config/testing.database.php and name your database with _test
  5. Create your database.
  6. Go to the tests folder and delete all the folders except controllers. All these files are examples and obviously you won't have those fixtures.
  7. You only need the SomeControllerTest.php as a starting point. Edit it to hit a controller you actually have. I wanted to just add a basic access test.
class HomepageControllerTest extends CIUnit_TestCase {
  public function setUp() {
    $this->CI = set_controller('homepage');
  }

  public function testIndexController() {
    $this->CI->index();
    $out = output();
    $this->assertNotEmpty($out);
  }
}

其他提示

I wrote two articles on testing Codeigniter both Unit and Acceptance tests. I use PHPUnit and Selenium for that.

Here are the links:

I'm planning to write an article on tests at all (with testing theory) but my two first articles should help!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top