How to write a simple PHPUnit test case?

Member

by dana , in category: PHP General , 2 years ago

How to write a simple PHPUnit test case?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by larissa , 2 years ago

@dana 

Here is an example of how to write a simple PHPUnit test case:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
assertEquals($expected, $value);
    }

    public function testGreaterThan()
    {
        $value = 10;
        $expected = 5;
        $this->assertGreaterThan($expected, $value);
    }
}


In this example, MyTestClass extends the TestCase class provided by PHPUnit. Two test methods are defined: testEquals() and testGreaterThan().


In testEquals(), we set a value of 10 and an expected value of 10. We then use the $this->assertEquals() assertion method provided by PHPUnit to check if the value is equal to the expected value.


In testGreaterThan(), we set a value of 10 and an expected value of 5. We use the $this->assertGreaterThan() assertion method to check if the value is greater than the expected value.


To run this test case, you can use the PHPUnit command-line tool or an integrated development environment (IDE) with PHPUnit support.

Related Threads:

How to write a test suite in PHPUnit?
How to write test case in mocha?
How to write phpunit test?
How to test exceptions in PHPUnit?
How to test performance in PHPUnit?
How to test for equality in PHPUnit?