首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于使用阿图姆for PHP代码的测试用例,我如何对使用require_once的类进行单元测试?

关于使用阿图姆for PHP代码的测试用例,我如何对使用require_once的类进行单元测试?
EN

Stack Overflow用户
提问于 2019-05-06 10:55:57
回答 1查看 67关注 0票数 0

我可以使用阿图姆对类进行单元测试,如下所示:

代码语言:javascript
复制
<?php

namespace vendor2\project2;

class helloWorld2
{
    public function say()
    {
        return 'Hello World!';
    }

     public function add($a,$b)
    {
        return ($a+$b);
        //return 'Hello World!';
    }
}
?>  

一切都很好,我完成了几个测试用例。

但是,当我将require_once添加到我想要测试的类中时,阿图姆就无法测试该类:

代码语言:javascript
复制
<?php

namespace vendor2\project2;

$ServerRoot = realpath(__DIR__. '/..');
require_once $ServerRoot.'/db/dbTables/M4_Warrior.php';

class helloWorld2
{
    public function say()
    {
        return 'Hello World!';
    }

     public function add($a,$b)
    {
        return ($a+$b);
        //return 'Hello World!';
    }
}
?>

当我用require_once评论这一行时,一切都很好

为什么?

test php文件也如下所示:

代码语言:javascript
复制
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace vendor2\project2\tests\units;

$ServerRoot = realpath(__DIR__. '/../..');
require_once $ServerRoot.'/HandleCommands/helloWorld2.php';

use atoum;

/**
 * Test class for helloWorld.
 *
 * @author Vh80705
 */
class helloWorld2 extends atoum {

    // put your code here

    public function test1() {
        $true  = true;
        $false = false;

        $this
            ->boolean($true)
                ->isFalse();     // fails

    }    

    public function test2() {
        $true  = true;
        $false = false;

        $this
            ->boolean($false)
                ->isFalse();     // succeed        
    }    

    public function test3 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->string($this->testedInstance->say())
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo('Hi atoum !')
        ;
    }    

    public function test4 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->string($this->testedInstance->say())
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo('Hello World!')
        ;
    }    

    public function test5 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->integer($this->testedInstance->add(1,2))
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo(3)
        ;
    }        

    public function test6 ()
    {
        $this
            // creation of a new instance of the tested class
            ->given($this->newTestedInstance)

            ->then

                    // we test that the getHiAtoum method returns
                    // a string...
                    ->integer($this->testedInstance->add(1,2))
                        // ... and that this string is the one we want,
                        // namely 'Hi atoum !'
                        ->isEqualTo(4)
        ;
    }        

    public function testSkipped() {
        $this->skip('This test was skipped');
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-07 07:52:40

奇怪,但这真的是真的!

阿图姆关心的是PHP文件末尾的?>

?>之后什么都没有

甚至新线

我用空格替换了所有PHP文件中的所有?>,问题就解决了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56003768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档