class ExampleTest extends PHPUnit_Framework_TestCase { public function testExample() { // Assert that 2 + 2 equals 4 $this->assertEquals(4, 2 + 2); } public function testAnotherExample() { // Assert that a variable is not null $this->assertNotNull($foo); } } $test = new ExampleTest(); $test->runTest('testExample');
class DatabaseTest extends PHPUnit_Framework_TestCase { protected function setUp() { // Create a new database connection $this->db = new PDO('mysql:host=localhost;dbname=test', 'user', 'password'); } public function testInsert() { // Insert a record into the database $this->db->exec("INSERT INTO users (name, email) VALUES ('John Doe', 'jdoe@example.com')"); // Assert that the record was inserted successfully $this->assertEquals(1, $this->db->lastInsertId()); } public function testSelect() { // Select a single record from the database $stmt = $this->db->query("SELECT * FROM users WHERE id = 1"); // Assert that the record was returned successfully $this->assertNotNull($stmt->fetch()); } } $test = new DatabaseTest(); $test->runTest('testInsert');In this example, we define a test case called `DatabaseTest` which contains two test methods. We also override the `setUp()` method to create a new database connection before each test method is executed. We then create a new instance of the `DatabaseTest` class and call the `runTest()` method, passing in the name of the test method we want to run. This will execute the `testInsert()` method and output any assertion failures. Package/Library: PHPUnit