簡體   English   中英

關於使用Atoum for PHP代碼的測試用例,我如何對使用require_once的類進行單元測試?

[英]Concerning test cases with Atoum for PHP code , how can I unit test a class which uses require_once?

我可以使用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');
    }
}

奇怪,但這是真的!

Atou關心PHP文件末尾的?>
之后什么都沒有?>
甚至新線

我用空格替換了所有PHP文件中的所有?> ,問題解決了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM