Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:24:02+00:00 2026-05-23T14:24:02+00:00

I noticed that PHPUnit ignores exceptions thrown in the setUp() method, and simply runs

  • 0

I noticed that PHPUnit ignores exceptions thrown in the setUp() method, and simply runs tests even when the setup function throws an exception.

In the below code the exception will be ignored, the code below it will not run, and the test_method will fail because it is using an undefined variable.

protected $a;

public function setUp() {
    parent:setUp();
    throw new Exception(); // setup now exits silently.
    $this->a = new A(); // will never run
}

public function testA() {
    $this->assertTrue($this->a->something()); // will exit tests with PHP error, because $this->a === null
}

I’m running phpunit through the CLI with an phpunit.xml configuration file.

Does anyone know a way to make PHPunit report on the exception, and then stop the execution of the testCase?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:24:02+00:00Added an answer on May 23, 2026 at 2:24 pm

    Can’t repoduce

    Running the script (full example below) produces an error output with the exception.

    I’d assume you have a problem elsewhere or maybe a old phpunit version? Even so I’m not aware of any changes in that piece of code.

    You might also be running phpunit from trunk? (“3.6”) In that case the handling of the "Exception" class its self changed, can’t test that case right now but if that applies to you try using a InvalidArgumentException() (just for testing) and see if that changes things.

    phpunit test.php
    PHPUnit 3.5.13 by Sebastian Bergmann.
    
    E
    
    Time: 0 seconds, Memory: 3.00Mb
    
    There was 1 error:
    
    1) FooTest::testA
    Exception: hi
    
    /home/.../test.php:10
    
    FAILURES!
    Tests: 1, Assertions: 0, Errors: 1.
    

    Your Code made runable:

    <?php
    
    class FooTest extends PHPUnit_Framework_TestCase {
    
    
        protected $a;
    
        public function setUp(){
            parent::setUp();
            throw new Exception('hi'); //setup now exits silently.
            $this->a = new A(); //will never run
        }
    
        public function testA(){
            $this->assertTrue($this->a->something()); //will exit tests with PHP error, because $this->a === null
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed that i can set a return type on a function to 'Void'
I noticed that calling the printf function (with cl_intel_printf extension), the same variable of
I noticed that itertools does not (it seems to me) have a function capable
I noticed that there are two debug perspectives in Eclipse that is setup for
I noticed that scala.xml.Atom takes a type parameter A , even though all of
I noticed that if some MessageBox appears and you even just have a OK
I noticed that an index view is routed correctly even if there isn't a
I noticed that an application I was running was throwing exceptions upon simultaneous decryption.
I noticed that Google maps is providing directions in my local language (hungarian) when
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.