Welcome Guest, Not a member yet? Register   Sign In
Someone please explain how I can use unit testing or test driven development w/ codeigniter
#1

[eluser]tomdelonge[/eluser]
I understand the main idea of writing a test and then writing code to make the test succeed. I've even setup Eric Barnes simpletest for codeigniter library.

I think the trouble I'm having stems from the simplicity of my website. It's rather simple, more like an ecommerce store with some minor differences. So I understand I could write an auth library and test logging in and such, but how do I test other aspects? What else am I supposed to test?

I'm trying to learn how to use unit testing - is it possible that my website is so simple that it doesn't need it?
#2

[eluser]Eric Barnes[/eluser]
I haven't gotten into test first development. I seem to always write a method and then test it.

But the biggest areas you would want to test in what you described would be order processing. IE: calculating discounts, taxes, etc. Then work from there.

As a rule of thumb any method especially in libraries or models can be tested and it is a good to get in the habit of testing them. But don't just test for what you know, test for any weird situation you can through at it.

Here is an example:
Code:
function subtract($one, $two)
{
    return $one - $two;
}

// Actual test
public function test_subtract()
{
    $this->assertEqual(subtract(1,1), 0);
    $this->assertEqual(subtract(5,4), 1);
    $this->assertEqual(subtract(4,5), '-1');
    $this->assertEqual(subtract(4,''), '1');
    $this->assertEqual(subtract('',''), '1');
    $this->assertEqual(subtract('one','two'), '1');
    $this->assertEqual(subtract(array(),'two'), '1');
    // etc...
}

As you can see with that test I am using that will fail and your method should then account for it.
#3

[eluser]toopay[/eluser]
In more strict approach, you must have 1 unit testing for every function in your application. It is best practice, to make the unit testing right after you write some function.
#4

[eluser]Unknown[/eluser]
Under my controllers directory I created a folder called testcases.
I am using the codeigniter cli interface for running my tests.

I just started with codeigiter, I assume I can place phpunit tests in this directory and run the tests via a script.

But while boot straping into a new project I have something to get going.




Theme © iAndrew 2016 - Forum software by © MyBB