关于Atoum for PHP代码的测试用例,如何对使用require_once的类进行单元测试?

时间:2019-05-06 10:55:58

标签: php atoum

我可以使用Atoum对一个类进行如下单元测试:

<?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时,Atoum无法测试该类:

<?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注释行时,一切都很好
为什么?

测试php文件也如下:

<?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');
    }
}

1 个答案:

答案 0 :(得分:0)

奇怪,但这是真的!

Atoum在PHP文件末尾关心?>
?>
之后应该没有任何内容 甚至换行

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