faramka
faramka

Reputation: 2234

Unit testing of functions

I have got a PHP file in which there are some functions (not included in a class). I am using PHPUnit to testing. When I try to generate in a simply way a test file from a file containing functions, the log says:

Could not find class...

Is there any possibility to test functions which are not methods?

Upvotes: 1

Views: 154

Answers (2)

Treffynnon
Treffynnon

Reputation: 21553

Yes, you can with something like this:

includes/functions.php

function my_function() {
    return true;
}

tests/MyFunctionTest.php

require_once '../includes/functions.php';

class MyFunctionTest extends PHPUnit_Framework_TestCase
{
    public function testReturnValue()
    {
        $return_value = my_function();
        $this->assertTrue($return_value);
    }
}

So as long as your function is within scope you can call it from your test method just like any other PHP function in any other PHP framework or project.

Upvotes: 2

Eugene
Eugene

Reputation: 4389

If I'm right then PhpUnit works only with classes hence methods. So just convert them into the methods for testing purpose. Shouldn't be hard.

Upvotes: 0

Related Questions