Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to unit test, php files other than class file using PHPUnit?

I'm new to PHPUnit. I have php files that don't have any classes in them. What I came to understand from reading documentation, PHPUnit considers a class as a single unit. So Does PHPUnit considers a class as a unit? Is it possible to test php files that don't have any class in them?

like image 264
swapnil mandavkar Avatar asked Mar 15 '16 15:03

swapnil mandavkar


People also ask

What is PHPUnit used for?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

What is the PHPUnit XML configuration file used to organize tests?

The <testsuite> Element.

How do I run a test case in PHP?

From the Menu-bar, select Run | Run As | PHPUnit Test . To debug the PHPUnit Test Case, click the arrow next to the debug button on the toolbar, and select Debug As | PHPUnit Test . From the Main Menu, select Run | Debug As | PHPUnit Test . The unit test will be run and a PHP Unit view will open.


1 Answers

Sure, you can absolutely test other PHP scripts.

class MyScriptTest extends PHPUnit_Framework_TestCase {
    public function testMyFunction() {
        include_once 'path/to/script.php';
        $result = someFunction();

        $this->assertEquals('expected result', $result);
    }
}

So write PHPUnit test classes, and inside a test run whatever code you wish to test and make assertions against it.

like image 174
jszobody Avatar answered Nov 01 '22 18:11

jszobody