Chapter 7: Skipping Tests in PestPHP

Chapter 7: Skipping Tests in PestPHP

·

2 min read

In this chapter, we'll explore how PestPHP allows you to skip tests temporarily. There are situations during the development process when you might need to disable specific tests, and PestPHP provides a straightforward way to achieve this without removing the test code.

Understanding Test Skipping

Skipping tests in PestPHP allows you to temporarily exclude certain tests from the test suite. This can be useful when you're working on specific parts of your code, and running certain tests might be unnecessary or problematic during a particular development phase.

Basic Test Skipping

To skip a test, use the skip function:

test('this test is temporarily skipped')
    ->skip()
    ->test(function () {
        // Test logic goes here
    });

In this example, the skip function is used to mark the test as skipped. PestPHP will ignore the test during test execution.

Skipping Tests Conditionally

You can also skip tests based on a condition using the skipIf function:

test('skip this test conditionally')
    ->skipIf(PHP_VERSION_ID < 80000)
    ->test(function () {
        // Test logic goes here
    });

In this case, the test will be skipped if the condition (PHP_VERSION_ID < 80000) evaluates to true. This allows you to skip tests based on specific runtime conditions.

Practical Use Cases

Incomplete Features

When working on a feature that is not yet complete, you can skip tests related to that feature:

test('feature functionality is pending')
    ->skip()
    ->test(function () {
        // Feature-related test logic goes here
    });

Known Issues

If a test is known to be failing due to a known issue that hasn't been addressed yet, you can skip the test until the issue is resolved:

test('known issue with this test')
    ->skip('Known issue: Bug #12345')
    ->test(function () {
        // Test logic goes here
    });

Conclusion

Skipping tests in PestPHP provides a convenient way to temporarily exclude specific tests during the development process. This feature allows you to focus on the tasks at hand without being distracted by tests that are not relevant or are known to be problematic. By using skip and skipIf, you can control which tests are executed, making your development workflow more efficient. In the next chapter, we will explore PestPHP's extensibility through custom macros, enabling you to tailor PestPHP to the specific needs of your project.

Did you find this article valuable?

Support Tresor by becoming a sponsor. Any amount is appreciated!