Welcome Guest, Not a member yet? Register   Sign In
unit test / SimpleTest
#1

[eluser][email protected][/eluser]
Hey guys,

I have tried migrating SimpleTest with CI, and am having issues of where test files should go.

I am trying to Unit Test a CI application, and am not sure how to set the paths so everything is picked up. I am having issues of where files should reside, and how paths should be set??

Any guidance will help. Thank you.
#2

[eluser]Don Pinkster[/eluser]
Discovered this thread while searching how to unittest with CodeIgniter.

Because I really dislike the CI's included UnitTest I implemented SimpleTest in my application.
Currently Im only testing my models yet, but going to write tests for my whole application when I have the time.

I have one file, which is small but oh so important:
Code:
<?PHP

require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';

class TestHelper extends UnitTestCase
{
  protected $CI;
  
  public function setUp()
  {
    $this->CI =& get_instance();
  }
}

This is my "test" controller which executes the tests:

Code:
<?php
require_once 'intranet.php';
/**
* Everything regarded the Remote Support goes in here
*
* @package intranet
* @author Don Pinkster
**/
class Test extends Intranet {
  
  protected $sTestDirectory;
  protected $oTestSuite;

  /**
   * Constructor
   *
   * @return void
   * @author Don Pinkster
   **/
    function Test()
    {
        parent::Intranet();
        $this->load->database('testing', false, true);
        $this->sTestDirectory = dirname(__FILE__).'/../test/';
        
        require_once $this->sTestDirectory . 'test_helper.php';
        $this->oTestSuite = new TestSuite();
    }
    
    function index()
    {      
    
    $this->all_tests();
    
    }
    
    function all_tests()
    {
      $this->oTestSuite->addTestFile($this->sTestDirectory . 'models/contacts_model.php');
      $this->oTestSuite->addTestFile($this->sTestDirectory . 'models/remote_support_model.php');
      $this->run();
    }
    
    function contacts_model()
    {
      $this->oTestSuite->addTestFile($this->sTestDirectory . 'models/contacts_model.php');
      $this->run();
    }
    
    function remote_support_model()
    {
      $this->oTestSuite->addTestFile($this->sTestDirectory . 'models/remote_support_model.php');
      $this->run();
    }
    
    protected function run()
    {
      $this->oTestSuite->run(new HtmlReporter());
    }
}

And inside my test cases I have things like this:
Code:
<?PHP

class Remote_SupportTest extends TestHelper
{
  public function setUp()
  {
    parent::setUp();
    $this->CI->load->model('remote_support_model', 'remote');
  }
  
  public function testHeadCategories()
  {
    $aResult = $this->CI->remote->getHeadCategories();
      $this->assertEqual(2, count($aResult));
  }
  
  public function testExistingSubCategorie()
  {
    $aResult = $this->CI->remote->getSubCategories(1);
      $this->assertEqual(2, count($aResult));
  }
  
  public function testEmptyHeadCategory()
  {
    $aResult = $this->CI->remote->getSubCategories(2);
      $this->assertEqual(0, count($aResult));
  }
  
  public function testTemplateFields()
  {
    $aResult = $this->CI->remote->getTemplateFields(2);
    $this->assertEqual(0, count($aResult));
    $aResult = $this->CI->remote->getTemplateFields(4);
    $this->assertEqual(4, count($aResult));
    $aResult = $this->CI->remote->getTemplateFields(5);
    $this->assertEqual(2, count($aResult));
  }
}
#3

[eluser][email protected][/eluser]
Hey Don,

This looks great, and is exactly what I was referring to, nice job with the code I understand 99% of what is going on. There a few things I'm not understanding though.

1) In TestHelper.php, how doest $this->CI =& get_instance() get a reference to the actual CI instance.
2) In the Test controller, what is intranet.php and how can I get it?
3) What urls do you hit to test this? ( I am assuming 'http://localhost/test/Test' will hit all the tests.

Please respond whenever you get a chance. Thank you for your help thus far.
#4

[eluser]Don Pinkster[/eluser]
Hi,

1. get_instance(); is a method provided by CI itself. Its in the Base5.php or something like that in the system folder.

2. Intranet is just a abstract controler I innerhit every controller form.
Code:
<?php
/**
* Main Application Controller
*
* @package intranet
* @author Don Pinkster
**/
class Intranet extends Controller {

  /**
   * Constructor
   *
   * @return void
   * @author Don Pinkster
   **/
    public function Intranet()
    {
        parent::Controller();
        $this->layout->addJavascript('prototype');
        $this->layout->addJavascript('scriptaculous');
        $this->layout->addJavascript('effects');
        $this->layout->addStylesheet('style');
        
        $this->connect_database();
    }
    
    private function connect_database()
    {
      $this->load->database('default');
    }
}

I just hit http://intranet/test for all tests or http://intranet/test/contacts_model to execute the contacts_model tests.

Yesterday I modified my own code a bit. I have now a .yml file with my fixtures in it which will be loaded in the database prefore testing and get deleted after testing.
#5

[eluser][email protected][/eluser]
Don,

This is great. Thank you very much for your help.
#6

[eluser][email protected][/eluser]
Hey Don,

I started using Simpletests's WebTestCase to test the controllers in the system. Everything was working fine until I got to a section on our site where the user must be logged in. I can't seem to figure out a way how to log the user in, and continue testing with it.

In other words, every request I hit with WebTestCase is looked as a separate request and there is not state saved. I was just wondering if you have any advice?

Thank you.
Kamil




Theme © iAndrew 2016 - Forum software by © MyBB