开发者

Automated Unit Testing with CodeIgniter

开发者 https://www.devze.com 2023-02-08 10:57 出处:网络
Anyone using CodeIgniter use automated unit testing? Seems the way CodeIgniter\'s guy do unit test need to have a controller which run in browser,

Anyone using CodeIgniter use automated unit testing?

Seems the way CodeIgniter's guy do unit test need to have a controller which run in browser,

e.g.

  1. http://codeigniter.com/user_guide/libraries/unit_testing.html
  2. or this: http://jensroland.com/projects/toast/

However, I only want to test from command line, I want automated test and 开发者_运维技巧build.

Any recommendation?

Thanks.


You can use this integration with phpunit - http://www.foostack.com/foostack/

It would then run from the command line.


Here's what I do:

  • Download Test.php
  • Create a 't' directory at the root of my CI app
  • Put Test.php in the t directory
  • Create a bootstrap.php file (see below) to initialize testing
  • require_once the bootstrap.php file in my tests.
  • Run prove on tests in the t directory

bootstrap.php:

<?php

// Initialize CodeIgniter, suppressing output.
ob_start();
require_once __DIR__ . '/../index.php';
ob_end_clean();

require_once __DIR__ . '/Test.php';

An example of a test:

t/000-sanity.t:

#!/usr/bin/env php
<?php

require_once 'bootstrap.php';

plan(1);

is(true, true, 'Test.php works');

All CodeIgniter stuff is available. For example, you can do $ci =& get_instance();

This setup works great in CI 1.7.x and 2.x. Test.php is really easy to use, as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号