How to output in CLI during execution of PHP Unit tests?

后端 未结 18 1543
旧时难觅i
旧时难觅i 2020-11-30 18:01

When running a PHPUnit test, I would like to be able to dump output so I can debug one or two things.

I have tried the following (similar to the PHPUnit Manual examp

相关标签:
18条回答
  • 2020-11-30 18:11

    I'm having some luck with VisualPHPUnit, and it does helpfully show output, among other things.

    class TestHello extends PHPUnit_Framework_TestCase 
    {
        public function test_Hello() 
        {
            print "hello world";
        }
    }
    

    TestHello results

    0 讨论(0)
  • 2020-11-30 18:12

    In short, phpunit supresses STDOUT. It writes to STDERR by default, unless you add --verbose or --debug . You can do one of those things:

    • print your debug output to STDERR instead
    • var_dump your debug as usual but add --verbose to the phpunit command line
    • var_dump your debug as usual but add a line ob_flush(); beneath it
    • use the correct commands in phpunit for testing exactly what you're trying to test here

    Obviously, the last thing is the Good Thing to do, and the rest are quick temporary hacks.

    0 讨论(0)
  • 2020-11-30 18:16

    I had to modify source code for this code to work so you need to add URL for this forked repos to composer for this will work

    class TestCase extends \PHPUnit_Framework_TestCase
    {
        /**
         *  Save last response
         * @var Response|null A Response instance
         */
        static $lastResponse;
        /**
         *  Modify to save response
         *
         * @param  string $method
         * @param  string $uri
         * @param  array $parameters
         * @param  array $files
         * @param  array $server
         * @param  string $content
         * @param  bool $changeHistory
         * @return \Illuminate\Http\Response
         */
        final public function call(
            $method,
            $uri,
            $parameters = [],
            $files = [],
            $server = [],
            $content = null,
            $changeHistory = true
        ) {
    
            $response = parent::call($method, $uri, $parameters, $files, $server, $content, $changeHistory);
            static::$lastResponse = $this->client->getResponse();
            return $response;
        }
    
    
        /**
         * Modify message to add response text
         *
         * @param mixed $value
         * @param PHPUnit_Framework_Constraint $constraint
         * @param string $message
         * @since  Method available since Release 3.0.0
         */
        final public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
        {
            $message .= PHP_EOL . static::$lastResponse . PHP_EOL;
            parent::assertThat($value, $constraint, $message);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:21

    For some cases one could use something like that to output something to the console

    class yourTests extends PHPUnit_Framework_TestCase
    {
        /* Add Warnings */
        protected function addWarning($msg, Exception $previous = null)
        {
            $add_warning = $this->getTestResultObject();
            $msg = new PHPUnit_Framework_Warning($msg, 0, $previous);
            $add_warning->addWarning($this, $msg, time());
            $this->setTestResultObject($add_warning);
        }
    
        /* Add errors */
        protected function addError($msg, Exception $previous = null)
        {
            $add_error = $this->getTestResultObject();
            $msg = new PHPUnit_Framework_AssertionFailedError($msg, 0, $previous);
            $add_error->addError($this, $msg, time());
            $this->setTestResultObject($add_error);
        }
    
        /* Add failures */
        protected function addFailure($msg, Exception $previous = null)
        {
            $add_failure = $this->getTestResultObject();
            $msg = new PHPUnit_Framework_AssertionFailedError($msg, 0, $previous);
            $add_failure->addFailure($this, $msg, time());
            $this->setTestResultObject($add_failure);
        }
    
        public function test_messages()
        {
            $this->addWarning("Your warning message!");
            $this->addError("Your error message!");
            $this->addFailure("Your Failure message");
        }
    
        /* Or just mark test states! */
        public function test_testMarking()
        {
            $this->markTestIncomplete();
            $this->markTestSkipped();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:21

    This was taken from PHPUnit Docs about Fixtures.

    This should allow you to dump information at any point durring the phpunit test life cycle.

    Just replace __METHOD__ in the code below with whatever you want to output

    Example 4.2: Example showing all template methods available

    <?php
    class TemplateMethodsTest extends PHPUnit_Framework_TestCase
    {
        public static function setUpBeforeClass()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
        }
    
        protected function setUp()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
        }
    
        protected function assertPreConditions()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
        }
    
        public function testOne()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
            $this->assertTrue(TRUE);
        }
    
        public function testTwo()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
            $this->assertTrue(FALSE);
        }
    
        protected function assertPostConditions()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
        }
    
        protected function tearDown()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
        }
    
        public static function tearDownAfterClass()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
        }
    
        protected function onNotSuccessfulTest(Exception $e)
        {
            fwrite(STDOUT, __METHOD__ . "\n");
            throw $e;
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-11-30 18:22

    In laravel 5 you can use dump(), Dump the content from the last response.

    class ExampleTest extends TestCase{
        public function test1()
        {
            $this->post('/user', ['name' => 'Gema']);
            $this->dump();
        }
    }
    

    gives

    0 讨论(0)
提交回复
热议问题