Trying to learn pestphp, give me 3 beginners exercices and a quick crash course
- User
Trying to learn pestphp, give me 3 beginners exercices and a quick crash course
- Assistant
Trying to learn pestphp, give me 3 beginners exercices and a quick crash course
Certainly! Pest is a testing framework for PHP that is built on top of PHPUnit, designed to provide a more elegant and intuitive syntax for writing tests. Below is a quick crash course followed by three beginner exercises.
Installation: First, you need to install Pest in your Laravel or PHP project:
composer require pestphp/pest --dev
If you are using Laravel, you can call the command to set it up automatically:
php artisan pest:install
Creating a Test: To create a new test, you can use the Pest command:
php artisan make:test ExampleTest
Alternatively, you can create a test manually in the tests directory.
Writing Your First Test: In Pest, you can write tests using a very simple and expressive syntax. Here’s an example:
test('basic test', function () {
$this->assertTrue(true);
});
Running Tests: You can run your tests using:
./vendor/bin/pest
Or simply:
php artisan test
Assertions: Pest provides a variety of built-in assertions similar to PHPUnit. Here are a few examples:
test('the truth is true', function () {
expect(true)->toBeTrue();
});
test('array has an item', function () {
$array = ['apple', 'banana', 'cherry'];
expect($array)->toContain('banana');
});
Create a test suite for basic arithmetic operations. Write tests to validate these operations:
Example Test Implementation:
// tests/ArithmeticTest.php
test('addition works correctly', function () {
expect(2 + 2)->toEqual(4);
});
test('subtraction works correctly', function () {
expect(5 - 3)->toEqual(2);
});
test('multiplication works correctly', function () {
expect(4 * 3)->toEqual(12);
});
test('division works correctly', function () {
expect(10 / 2)->toEqual(5);
});
Write a test suite for string functions. Here are some test suggestions:
Example Test Implementation:
// tests/StringTest.php
test('uppercase conversion works', function () {
expect(strtoupper('hello'))->toEqual('HELLO');
});
test('string contains substring', function () {
expect('Hello, world')->toContain('world');
});
test('string length is correct', function () {
expect(strlen('Hello'))->toEqual(5);
});
Create tests to validate basic array functionality:
Example Test Implementation:
// tests/ArrayTest.php
test('array has element', function () {
$fruits = ['apple', 'banana', 'cherry'];
expect($fruits)->toContain('banana');
});
test('array count is correct', function () {
$fruits = ['apple', 'banana', 'cherry'];
expect(count($fruits))->toEqual(3);
});
test('item can be removed from array', function () {
$fruits = ['apple', 'banana', 'cherry'];
unset($fruits[array_search('banana', $fruits)]);
expect($fruits)->not()->toContain('banana');
});
These exercises should help you get comfortable with PestPHP and its syntax. Remember to read the official Pest documentation for further details and more advanced topics as you progress. Happy testing!
Not yet included in a published release.