如何从PHP脚本运行PHPUnit? [英] How to run PHPUnit from a PHP script?

查看:91
本文介绍了如何从PHP脚本运行PHPUnit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPUnit和Goutte创建一个自定义测试应用程序.我想在自己的引导文件中加载Goutte库(以及测试所需的任何文件),然后在全部引导后启动PHPUnit测试运行程序.

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.

我不确定如何在不从外部调用phpunit脚本的情况下执行此操作(这将是一个单独的过程,并且无法看到我的自举程序库).有人做过这样的事吗?最好的方法是什么?

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?

推荐答案

如果您引用,它告诉您关于setup()和teardown()的信息.

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

PHPUnit支持共享安装代码.在运行测试方法之前,将调用一个名为setUp()的模板方法. setUp()是在其中创建要测试的对象的地方.一旦测试方法完成运行(无论成功还是失败),都将调用另一个名为tearDown()的模板方法. 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);      
    }
}

这篇关于如何从PHP脚本运行PHPUnit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆