Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question PHPUnit: Calling methods inside dataprovider

    Is there anyway I can call a method within the dataprovider method? I tried doing it but the value passed from the dataProvider to the testAdd() method won't just pass. How do I do this?

    PS: I do not want to call this from setUp() or setUpBeforeClass(), any way out?


    PHP Code:
    <?php
    class DataTest extends PHPUnit_Framework_TestCase
    {
        
    /**
         * @dataProvider additionProvider
         */
        
    public function testAdd($a$b$expected$someValue)
        {
            echo 
    $someValue;
            
    $this->assertEquals($expected$a $b);
        }

        public function 
    additionProvider()
        {
            
    $someValue $this->doSomething();

            return array(
              array(
    000$someValue),
              array(
    011$someValue),
              array(
    101$someValue),
              array(
    113$someValue)
            );
        }

        protected function 
    doSomething(){

          return 
    6;

        }
    }
    ?>

    Thanks

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,395
    Thanks
    13
    Thanked 353 Times in 349 Posts
    I’m not sure echo is the appropriate method to test a value in PHPUnit. (var_dump() / error_log() should work better). but since it is PHPUnit, why don’t you make an assertion test?
    PHP Code:
        /** 
         * @dataProvider additionProvider 
         */ 
        
    public function testDoSomething($a$b$expected$someValue
        { 
            
    $this->assertEquals($this->doSomething(), $someValue); 
        } 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •