1

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?

1
  • Edit your qeustion. I will take -1 from it. Commented Dec 13, 2011 at 13:16

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Treffynnon. Your solution is excellent for me :) I'll do this as you proposed.
Yes, well like this is better then making method out of the function for testing purpose. :)
Why has this been downvoted? Perhaps the downvoter could fill us all in on their greater wisdoms in this area?
LOL! Even procedural programming is object oriented. What you hate is non-useage of object oriented language constructs. If I remember correctly object oriented was originally written in C as a library that became C++.
0

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.

1 Comment

I just don't want to create another file containing the same. But you're right: it's easy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.