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 5949735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:17:32+00:00 2026-05-22T17:17:32+00:00

I would like to use PHP’s assert function in my unit testing framework. It

  • 0

I would like to use PHP’s assert function in my unit testing framework. It has the advantage of being able to see the expression being evaluated (including comments) within the error message.

The problem is that each method containing tests may have more than one assert statement, and I would like to keep track of how many actual assert statements have been run. assert does not give me a way to count how many times it has been run, only how many times it has failed (within the failure callback).

I tried to abstract the assert statement into a function so that I can add a counting mechanism.

private function assertTrue($expression) {
    $this->testCount++;
    assert($expression);
}

This does not work however because any variables within the expression are now out of scope.

$var = true;
$this->assertTrue('$var == true'); // fails

Any advice on how I can use assert in my unit testing while being able to count the number of actual tests?

The two ideas I have come up with are to make users count themselves

$this->testCount++;
assert('$foo');
$this->testCount++;
assert('$bar');

or make users put only one assert in each test method (I could then count the number of methods run). but neither of these solutions is very enforcable, and make coding more difficult. Any ideas on how to accomplish this? Or should I just strip assert() from my testing framework?

  • 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-22T17:17:33+00:00Added an answer on May 22, 2026 at 5:17 pm

    You are restricted by the fact assert() must be called in the same scope the variables you are testing lie. That leaves — as far as I can tell — solutions that require extra code, modify the source before runtime (preprocessing), or a solution that extends PHP at the C-level. This is my proposed solution that involves extra code.

    class UnitTest {
        // controller that runs the tests
        public function runTests() {
            // the unit test is called, creating a new variable holder
            // and passing it to the unit test.
            $this->testAbc($this->newVarScope());
        }
    
        // keeps an active reference to the variable holder
        private $var_scope;
    
        // refreshes and returns the variable holder
        private function newVarScope() {
            $this->var_scope = new stdClass;
            return $this->var_scope;
        }
    
        // number of times $this->assert was called
        public $assert_count = 0;
    
        // our assert wrapper
        private function assert($__expr) {
            ++$this->assert_count;
            extract(get_object_vars($this->var_scope));
            assert($__expr);
        }
    
        // an example unit test
        private function testAbc($v) {
            $v->foo = true;
            $this->assert('$foo == true');
        }
    }
    

    Downfalls to this approach: all variables used in unit testing must be declared as $v->* rather than $*, whereas variables written in the assert statement are still written as $*. Secondly, the warning emitted by assert() will not report the line number at which $this->assert() was called.

    For more consistency you could move the assert() method to the variable holder class, as that way you could think about each unit test operating on a test bed, rather than having some sort of magical assert call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For class, I would like a to use PHP MVC framework very similar to
I have a function that I use on index.php page and I would like
I would like to know If I can use a php function on a
I would like to use the php function glob() to get those file which
I have the following string: keyword|title|http://example.com/ I would like to use the PHP function
I would like to use php to pass a keyword phrase to a function
I would like to use my laptop as a web development (PHP, Python, etc.)
Hello I would like to use preg_match in PHP to parse the Desired text
I would like to change this: // use appropiate lang.xx.php file according to the
I would like to use something like CLR Profiles on .Net 2.0 to see

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.