How to run PHPUnit from a PHP script?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 17:25:50

问题


I am creating a custom testing application using PHPUnit and Goutte. I would like to load the Goutte library (plus any files required for the tests) within my own bootstrap file and then start the PHPUnit test runner once it is all bootstrapped.

I'm not sure how to do this without calling the phpunit script externally (Which would be a seperate process, and won't be able to see my bootstrapped libraries). Has anyone done anything like this before? What is the best way to do it?


回答1:


If you reference the fixtures chapter in the PHPUnit documentation, it tells you about setup() and teardown().

PHPUnit supports sharing the setup code. Before a test method is run, a template method called setUp() is invoked. setUp() is where you create the objects against which you will test. Once the test method has finished running, whether it succeeded or failed, another template method called tearDown() is invoked. tearDown() is where you clean up the objects against which you tested.

This is basically a way of bootstrapping your application prior to running the tests in the test class.

class testMyScript
{
    private $myapp = null;

    public function setup()
    {
       $this->myapp = new My_Application;
       $this->myapp->bootstrap();
    }

    public function testIsMyAppInitialized()
    {
       $this->assertNotNull($this->myapp);      
    }
}


来源:https://stackoverflow.com/questions/9430620/how-to-run-phpunit-from-a-php-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!